How to Backup and Restore MongoDB using mongodump and mongorestore

mongodump is an useful tool to backup Mongo database. Apart from taking a cold backup, it can also take hot backup. i.e You can connect to a running instance of MongoDB and take a backup even when users are using the database.

mongorestore is an useful tool to restore the MongoDB backup that was taken using mongodump utility.
This tutorial provides several mongodump and mongorestore command examples that you can use to backup and restore a MongoDB using both .

I. Backup Mongo Database

1. Backup by Shutting down Mongod Instance

This is equivalent to the cold backup you would typically take in other database systems. You should shutdown the mongodb instance before taking backup using this method. In this example, you are not really connecting to the mongod instance to take this backup.

If you don’t want to shutdown the mongod to take backup, see the examples in the later section of this tutorial.

First stop the mongod instance:

service mongod stop

Go to the backup directory (or any directory where you want to store the mongodb backup), and execute the following mongodump command. The –dbpath indicates the location of the mongodb database files.

cd /backup
mongodump --dbpath /var/lib/mongo/

Finally, start the mongod instance:

service mongod start

The above mongodump directory will create a dump sub-directory under the current directory. As you see below, it has taken a backup of both mongodevdb and mongoproddb database.

Under the /backup directory. i.e From where you executed the mongodump command, it will create a dump directory as shown below. The dump directory will have a sub-directory for the database that was just backed-up.

# ls -l dump/
drwxr-xr-x. 2 root root 4096 Sep 7 10:08 mongodevdb

If you do a ls on this dump/mongodevdb, you’ll see all the collections from this database that was backed-up by mongodump command.

In this case, under the /dbbackup directory, mongodump command will create a sub-directory for the database that it getting backed-up and all the collections will be backed-up under that sub-directory as shown below.

5. Backup a Remote Mongodb Instance

In all the previous example we executed the mongodump command from the same server where the mongo database instance was running.

However, you can also connect to a mongodb instance running on a different server, and take a backup of that.

In the following example, the mongodump command is executed on a server called “local-host”, but it connects to the mongodb instance running on 192.168.1.2 and takes the backup of that instance and stores it in the local-host.

II. Restore Mongo Database

Once you’ve taken the backup of a MongoDB database using mongodump, you can restore it using mongorestore command. In case of an disaster where you lost your mongoDB database, you can use this command to restore the database. Or, you can just use this command to restore the database on a different server for testing purpose.

1. Restore All Database without Mongod Instance

If you’ve taken a backup without mongod instance, use this method to restore the same backup without running the mongod instance.

First, stop the mongod

service mongod stop

Next, go to the directory where the backup is located, and execute the restore command as shown below.

cd /backup
mongorestore --dbpath /var/lib/mongo dump

Note: In the above command, the last parameter “dump” is the directory name where the backup are stored. In this example, since we did a “cd /backup”, before executing the mongorestore, and specified “dump” as the directory name, this will take the backup from /backup/dump directory, and restore it.

2. Restore a specific Database without Mongod Instance

If you’ve backedup several mongodb database and like to restore only a specify database (instead of all the database), you can specify the database that you like to restore using the –db argument as shown below. The following example will restore only the mongodevdb.

3. Drop the old Database before Restoring

In the above two examples, mongorestore will perform a merge if it sees that the database already exists. If you don’t understand how the merge works, the above two restore will give you unexpected results. As you see below, it is giving a warning message for every collection that it is trying to restore, but is already present in the destination database.

If you want a clean restore, use the –drop option. If a collection that exist in the backup also exist in the destination database, mongorestore command will now drop that collection, and restore the one from the backup. In this example, as you see below, it is dropping the objects before restoring it.

4. Restore to a Remote Database.

In all the previous examples we executed the mongorestore command from the same server where the mongo database instance was running.

However you can also restore a mongo backup to a mongodb instance running on a different server.

In the following example, the mongorestore command is executed on a server called “local-host”, but it restores the mongo database backup located on the local-host to the mongodb instance running on 192.168.1.2 server.

In my opinion the most important part of creating backups with mongodump is user permissions which are pain in a** to configure, at the moment i have 4 users none of them can make full dump of a database 🙁

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Contact Us

Email Me :
Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!.