MongoDB data file backup and recovery

Source: Internet
Author: User
Tags mongodb collection mongodb server mongodump mongorestore

Backing up and recovering data is important for managing any data storage system.

1, cold backup and recovery-Create a copy of the data file (if you want to stop the MongoDB server), that is, the direct copy

MongoDB stores all the data in the data directory, the default is/data/db/(under Windows C:\data\db\), when starting MongoDB can also use--dbpath to specify our own set of data storage directory.
Back up MongoDB data: Simply create a copy of the data store directory and copy it directly.
Recover MongoDB Data: Specify the location of the data store directory copy with--dbpath at MongoDB startup.

In the case of the server running the direct copy is risky, may be copied out, the data has been destroyed, this way to create a copy of the data directory needs to close the MongoDB server, the data directory is to close that moment of data snapshot, You can replicate the directory as a backup before the server restarts.

2, hot backup and recovery--mongodb bin directory with the Mongodump and Mongorestore tools

Mongodump is a method that can be backed up at run time. The command detailed parameters are as follows:

Mongorestore gets the output of the Mongodump and inserts the backed-up data into the running MongoDB instance for data recovery. The command detailed parameters are as follows:


Mongodump (Backup) and Mongorestore (recovery) Example:
1. Create a MongoDB database:


2. Mongodump back up the MongoDB database:

-d Specifies the database that needs to be backed up,-o Specifies the backup location, and the above represents a backup of the MongoDB database to the same location as the current Mongodump command

3. Mongorestore Restore the MongoDB database and rename it to the MONGODB2 database:

-d Specifies the database to revert to, which restores the backed-up database to a database that does not have the same name as the original, where mongodb2,--drop means to delete the collection (if any) before recovery. Otherwise, the data is merged with the existing collection data and may overwrite some documents.

Data in the data store directory after recovery:

At this point, you can view the MONGODB2 database and the MongoDB collection and data documents in the MongoDB shell.


3, Fsync and lock mode backup

The above 1, 22 points are not guaranteed to get the real-time data back-up, because we may also have data in the memory buffer when the backup is not written to disk, MongoDB provides us with a fsync+lock mechanism to meet our needs.

Fsync (note that only the 1.3+ version is supported): This command forces the server to write the contents of all buffers to disk.
Allows us to get data in real time.
Lock (Write lock): A write lock to the database through lock to prevent further write operations to the database, all other instances of write operations are blocked
Plug until the lock is released. Write locks are the key to making fsync useful in backup.

1. Enforce Fsync in the shell and obtain lock (write lock), Fsync and lock before backup:
Fsync and Lock: Db.runcommand ({"Fsync": 1, "Lock": 1}), which is used for admin db.

This action forces the server to write the contents of all buffers to disk and lock the database, and does not allow write data operations, which are generally useful in performing database backups.

2, Db. $cmd. Sys.unlock.findOne (): Release the lock, the backup is ready to unlock, the command for the admin db.


3, Db.currentop (): View the current lock status

The status has been unlocked:

Lock Status:

Where Fsynclock is 1 indicates that MongoDB's fsync process (responsible for synchronizing write changes to disk) does not allow other processes to perform write data operations

The Fsync command enables very flexible backups without stopping the server or sacrificing the real-time nature of the backup. The price to pay is that some write operations are temporarily blocked. The only way to ensure that a live snapshot is backed up by a "slave server" is to not delay read and write.

4. Slave backup

Backing up from the server is the recommended backup method for MongoDB

Come from:http://www.open-open.com/lib/view/open1340532912749.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.