MongoDB backup and restore

database mongodb nosql

Backup and restore are essential 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 excellent command-line tools for taking backups and restoring backups, i.e., mongodump and mongorestore.

This post shows MongoDB’s backup/restore methods; you can refer to 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 MongoDB would be to 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 to avoid any changes to the files while copying them. If you don’t want to stop the 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 shut down the MongoDB server if I want to go with this method.


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

mongodump –-help

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


The above command creates 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 a remote server, then you can pass the host as a command-line option. Assuming that the firewall allows connection, you can do something like this –

mongodump –-host

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

mongodump –-db mydatabase

If you want to a backup of a 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. To fix this issue, you can use the –-oplog option. It ensures that you get point-in-time backup. The command would look like this –

mongodump --oplog

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


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

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

mongorestore dump/

The above command restores the databases taking the dump from the dump folder in the current working directory. Mongorestore command merges the databases with the existing ones. If you don’t want to merge databases, use the --drop option to drop the database first, and then restore it. Like this –

mongorestore –-drop dump/

If you want to restore a specific database, then you can use the option for the 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 a mongorestore. You can take a look at the options by running the following command.

mongorestore --help


comments powered by Disqus