MongoDB a tutorial on backup and recovery with the command line tools that come with it _mongodb

Source: Internet
Author: User
Tags eval mongodb mongodb documentation mongodump mongorestore

To back up a database,

Mongorestore-d db/path/to/back_up

For example:

mongodump-d Bookstore-o/data01/db_backup/

This command will dump all of the DB collection
recover data from a backup folder

Mongorestore-d Bookstore/data01/db_backup/bookstore

Back up or reply to the specified collection only
take the Statistics table in bookstore DB as an example

mongodump-d bookstore-c Statistics-o/data01/db_backup/

Then specify the collection corresponding Bson file on restore

Mongorestore-d bookstore-c Statistics/data01/db_backup/bookstore/statistics.bson

Dump by conditional query
You can also use a query to dump certain records in a collection that meet the criteria, such as

mongodump-d bookstore-c novel_sources-q "{\" tag\ ": \" tag_11\ "}"-o/data01/db_backup/

Note that if you have the $ symbol in query, you need to escape, for example

mongodump-d qunimei-c collection_name-q "{\" date\ ": {\" \ $gte \ ": \" 2015-03-25\ "}}"-o/path/to/dump

Attention matters
Mongorestore does not overwrite existing records, but instead repeats them (if possible).
This method is time-consuming when the volume of data is large.
backing up data files directly

MONGO 127.0.0.1:27017/db_to_back--eval "Db.fsynclock ()"
RSYNC-AVH--delete/path/to/your/mongofile/path/to/ backup/folder/
MONGO 127.0.0.1:21001/turbo--eval "Db.fsyncunlock ()"

The key is two commands in the first and third lines, and for Db.fsynclock (), the MongoDB documentation

Db.fsynclock ()

Forces the mongod to flush all pending write operations to the disk and locks the entire mongod instance to prevent additional writes until the user releases the lock with the db.fsyncUnlock() command. db.fsyncLock() is an administrative command.
Writes an Mongod write operation to the data file and blocks the new write operation, knowing that the Db.fsyncunlock () command is running, so the two commands are executed before and after copying the data file.

Comparison with other backup recovery tools

The MongoDB backup tools include Bsondump, Mongoexport, recovery tools, and Mongoimport, and the specific differences between them are:

Comparison of Bsondump, Mongoexport, mongodump backup tools:

1, Bsondump can specify a backup format for the JSON and debug mode, although this command is incidental, but rarely used;

2, Mongoexport can export JSON or CSV format files, you can specify a query filter or specify the output of the domain, but the tool exported json,csv may be incompatible with some data types, so may not all data export, Mongodump can be all compatible;

3, Mongodump support filtration, and in the export speed and compression rate aspects of Mongodump is the fastest and best. Therefore, if you do not have a special format such as CSV or debug backup requirements, generally use mongodump as a backup tool.

Mongorestore vs. mongoimport Recovery tool:

1, Mongoimport can accept JSON,CSV,TSV format files, each behavior of an object. Like Mongoexport, it has the same compatibility problem in the process of recovery, so it has the probability of recovering incomplete;

2, Mongorestore, slower, slower than Mongoimport 2.5 times times, but according to the Mongodump exported data, you can import the data completely. During the restore process, the index is created again based on the result of the previous dump.

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.