How to Backup and Restore MongoDB database

In this article, we will see the steps to Backup and Restore MongoDB database. It is important to take regular backups of our system. Backups are good protection against most types of failures.

mongodump tool is used to take backup of MongoDB database or collection or subset of collection.
mongorestore tool is used to restore database or collection on the target database.

mongodump Syntax:

mongodump [options]

Few options are:

–authenticationDatabase
–db
–username
–password
–out
–host
–uri
–collection
–query
–archive
–oplog
–dumpDbUsersAndRoles
–excludeCollection

MongoDB backup examples:

1. To backup all databases just type mongodump without any parameters. It creates a database backup named dump/ in the current directory. In dump directory there is separate directory for database. bson and json files will be there in the specific database directory.

mongodump

To see the list of directory in dump directory,

cd dump/ ls cd test ls

2. To backup single database to specified path.

create a directory for dumps: mkdir backups

root@mongodb1:/home/r2schools/dump/test# mongodump --db=myDb --out=/home/r2schools/backups/ 2019-11-20T21:40:48.515-0800 writing myDb.users to 2019-11-20T21:40:48.523-0800 writing myDb.test1 to 2019-11-20T21:40:48.525-0800 writing myDb.hello to 2019-11-20T21:40:48.533-0800 done dumping myDb.hello (0 documents) 2019-11-20T21:40:48.534-0800 done dumping myDb.test1 (1 document) 2019-11-20T21:40:48.863-0800 done dumping myDb.users (100000 documents)

3. Backup databases with host or uri name.

Using uri connection string

mongodump --uri="mongodb://127.0.0.1:27017/?db=flights" --out=/home/r2schools/backups/

Using host connection string

mongodump --db=m201 --host="/127.0.0.1" --port=27017 --out=/home/r2schools/backups/

4. Backup database with compressed output.

mongodump --gzip --db=test

or

mongodump --archive=test.`date +"%m-%d-%y"`.gz --gzip --db=test

MongoDB Restore examples:

mongorestore Syntax:

mongorestore [options] [/]

MongoDB backup examples:

1. To restore from a dump directory to a local mongod instance running on port 27017.

mongorestore dump/

2. We can restore only specific collections which are taken using mongodump.

mongorestore --nsInclude=flights.flightData /home/r2schools/dump/

root@mongodb1:/home/r2schools/dump/flights# mongorestore --nsInclude=flights.flightData /home/r2schools/dump/ 2019-11-20T22:49:35.400-0800 reading metadata for flights.flightData from /home/r2schools/dump/flights/flightData.metadata.json 2019-11-20T22:49:35.446-0800 restoring flights.flightData from /home/r2schools/dump/flights/flightData.bson 2019-11-20T22:49:35.451-0800 no indexes to restore 2019-11-20T22:49:35.451-0800 finished restoring flights.flightData (2 documents, 0 failures) 2019-11-20T22:49:35.452-0800 2 document(s) restored successfully. 0 document(s) failed to restore.

3. To restore from a dump directory that contains compressed files

mongorestore –gzip –nsInclude=”test.*” /home/r2schools/dump/test/

4. Drop database before restore.

root@mongodb1:/home/r2schools/dump/test# mongorestore --drop --nsInclude="test.*" /home/r2schools/dump/ 2019-11-20T23:03:20.077-0800 preparing collections to restore from 2019-11-20T23:03:20.081-0800 don't know what to do with subdirectory "test/backups", skipping... 2019-11-20T23:03:20.082-0800 don't know what to do with file "/home/r2schools/dump/test/test.11-20-19.gz", skipping... 2019-11-20T23:03:20.095-0800 reading metadata for test.test from /home/r2schools/dump/test/test.metadata.json 2019-11-20T23:03:20.109-0800 restoring test.test from /home/r2schools/dump/test/test.bson 2019-11-20T23:03:22.658-0800 restoring indexes for collection test.test from metadata 2019-11-20T23:03:23.038-0800 finished restoring test.test (99999 documents, 0 failures) 2019-11-20T23:03:23.038-0800 99999 document(s) restored successfully. 0 document(s) failed to restore.

5. Restore MongoDB database to remote mongodb server from current mongod instance.

mongorestore –db=flights –host=”/192.168.152.141/” –port=27017 /home/r2schools/dump/flights/

2019-11-20T23:09:34.173-0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead 2019-11-20T23:09:34.174-0800 building a list of collections to restore from /home/r2schools/dump/flights dir 2019-11-20T23:09:34.176-0800 reading metadata for flights.flightData from /home/r2schools/dump/flights/flightData.metadata.json 2019-11-20T23:09:34.189-0800 restoring flights.flightData from /home/r2schools/dump/flights/flightData.bson 2019-11-20T23:09:34.195-0800 no indexes to restore 2019-11-20T23:09:34.196-0800 finished restoring flights.flightData (2 documents, 0 failures) 2019-11-20T23:09:34.196-0800 2 document(s) restored successfully. 0 document(s) failed to restore.

Notice the changes before and after database restore.

What is good time to take backup?
For standard servers, at an off time.
For replication servers, take on secondary servers.

So in this article, we have covered how to backup and restore MongoDB database locally, remotely and single or multiple databases at a time.

Leave a Reply

Your email address will not be published. Required fields are marked *