MongoDB USES its own command line tools for backup and restore tutorials

  • 2020-05-30 21:15:07
  • OfStack

To back up a database,


mongorestore -d db /path/to/back_up

Such as:


mongodump -d bookstore -o /data01/db_backup/

This command will dump out all collection of the DB
Restore data from the backup folder


mongorestore -d bookstore /data01/db_backup/bookstore

Backup or restore only the specified collection
Take the statistics table in bookstore DB for example


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

Then specify the bson file for this collection when restore


mongorestore -d bookstore -c statistics /data01/db_backup/bookstore/statistics.bson

Query dump by condition
You can also access some of the eligible records in 1 collection of dump through 1 query, for example


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

Note that if there is a $sign in query, it needs to be escaped, for example


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

Matters needing attention
mongorestore does not overwrite existing records, but adds them repeatedly (if possible).
When the amount of data is large, this method takes a lot of time.
Backup 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 the two commands on lines 1 and 3, the documentation for db.fsyncLock (), mongodb


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.
Write the incomplete write operation of mongod to the data file and block the new write operation until the db.fsyncUnlock () command is run, so execute both before and after copying the data file.

Comparison with other backup and restore tools

mongodb also comes with backup tools bsondump, mongoexport, and recovery tools mongoimport. The differences are as follows:

Comparison of bsondump, mongoexport and mongodump backup tools:

1. bsondump can specify the backup formats as json and debug.

2. mongoexport can export files in json or csv format, and can specify query filters or output fields. However, json and csv exported by this tool may not be compatible with some data types, so they may not export all data.

3. mongodump supports filtering, and mongodump is the fastest and best in terms of export speed and compression rate. Therefore, if there is no backup requirement for csv or debug, 1 generally USES mongodump as the backup tool.

Comparison of mongorestore and mongoimport recovery tools:

1. mongoimport can accept files in json,csv and tsv formats, with 1 object for each behavior. Like mongoexport1, it also has compatibility problems in the recovery process, so there is a probability of incomplete recovery.

2. The speed of mongorestore is slow, about 2.5 times slower than that of mongoimport. However, according to the data exported by mongodump, the data can be imported completely. During the restore process, the index is recreated from the previous dump results.


Related articles: