Mongodb backup and restore

database mongodb nosql

Backup and restore are important routine jobs. If you are working with MongoDB then you must know how to take backups as well as how to restore them. MongoDB comes with wonderful command line tools for backup and restore, i.e. mongodump and mongorestore.

I will not be explaining all the backup/restore method here; you can refer MongoDB documentation for backup and restore strategies for more insight. But what I am going to show you are, the most common and simple ways to backup and restore MongoDB.

Copying data Files/Folder

The simplest way to backup or restore MongoDB would be to simply copy the database folder/files. That means if you are taking backup then copy the database folder/files to your backup location. And if you are restoring then copy the content of your backup folder to the database folder.

To get the most reliable backup or restore by this method, you have to stop the MongoDB server first so as to avoid any changes to the files while copying them. If you don’t want to stop MongoDB server then you can use the following command to pause writes to the database.


Once you are done copying the files, run the following command to resume writes to the database.


Though db.fsyncLock() and db.fsyncUnlock() would work, my personal choice would be to shutdown MongoDB server if I want to go with this method.


First thing I want you to do is, run the following command and go through all options you have with is tool.

mongodump –-help

Let’s see some of the common usage of mongodump. If you want to take backup of all the databases and your MongoDB server runs on default port, which 27017, then run the mongodump command with no option.


The above command will create a dump folder in the current location and put the dump files in it.

If you are running MongoDB server on some other port, let’s say 30000, then you can provide the port as an option.

mongodump –-port 30000

If you have MongoDB server running on remote server then you can pass host as command line option. Assuming that the firewall allows connection, you can do something like this –

mongodump –-host

If you want to take backup of a specific database then it would like this –

mongodump –-db mydatabase

If you want to backup specific collection of a database then you can do like this –

mongodump –-db mydatabase –-collection mycollection

You can use whatever option is needed. I hope the above examples gave you a pretty clear idea about how to use the options available.

It might happen that when the mongodump tool is doing the backup, writes are also going on. In this case, it’s difficult to trust the integrity of the backup. So you can use the –-oplog option. This makes sure that you get point-in-time backup. The command would look like this –

mongodump --oplog

When using –-oplog option, remember that you must use –-oplogReplay option when restoring the backup.


This tool is used for restoring the backup taken by mongodump tool. The usage of command line options is similar for both mongodump and mongorestore. So, you can refer mongodump section to see how the available options can be used. Let’s see some of the common ways to use it.

If you want to restore databases from dump folder. Run the following command –

mongorestore dump/

The above command will restore the databases. Mongorestore command will merge the databases with the existing ones. If you don’t want to merge databases, use –-drop option to drop the database first and then restore it. Like this –

mongorestore –-drop dump/

If you want to restore specific database then you can use option for database name.

mongorestore –-db mydb –-drop dump/mydb

If you want to restore specific collection from a database then it goes like this –

mongorestore –-db mydb –-collection mycol –-drop dump/mydb/mycol.bson

There are plenty of other options available with mongorestore. You can take a look at the options by running following command.

mongorestore --help


comments powered by Disqus