Monday, August 10, 2015

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.
# ls -l dump
drwxr-xr-x. 2 root root 4096 Sep 7 09:59 admin
drwxr-xr-x. 2 root root 4096 Sep 7 09:59 mongodevdb
drwxr-xr-x. 2 root root 4096 Sep 7 09:59 mongoproddb
If you look inside one of the database backup directory, you’ll see that it contains all the objects from the database that was backed-up (for example: employee and department collections) .
# ls -l dump/mongodevdb
total 40172
-rw-r--r--. 1 root root     4848 Sep 7 09:59 employee.bson
-rw-r--r--. 1 root root      106 Sep 7 09:59 employee.metadata.json
-rw-r--r--. 1 root root     2840 Sep 7 09:59 department.bson
-rw-r--r--. 1 root root      108 Sep 7 09:59 department.metadata.json

2. Backup without Shutting down Mongod Instance

The following example will connect to a running mongod instance, and take backup of a specific database.
First, make sure mongod is up and running.
# service mongod start
Next, go to the backup directory, and execute the mongodump command, and pass the database name, username and password parameters as shown below.
# cd /backup

# mongodump --db mongodevdb --username mongodevdb --password YourSecretPwd
connected to: 127.0.0.1
Tue Sep 7 11:47:06.868 DATABASE: mongodevdb to dump/mongodevdb
Tue Sep 7 11:47:06.873 mongodevdb.system.indexes to dump/mongodevdb/system.indexes.bson
Tue Sep 7 11:47:06.890 5 objects
Tue Sep 7 11:47:06.890 mongodevdb.system.users to dump/mongodevdb/system.users.bson
Tue Sep 7 11:47:06.931 1 objects
Tue Sep 7 11:47:06.931 Metadata for mongodevdb.system.users to dump/mongodevdb/system.users.metadata.json
Tue Sep 7 11:47:06.931 mongodevdb.employee to dump/mongodevdb/employee.bson
Tue Sep 7 11:47:06.933 4 objects
Tue Sep 7 11:47:06.933 Metadata for mongodevdb.employee to dump/mongodevdb/employee.metadata.json
Tue Sep 7 11:47:06.937 71 objects
...
..
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.
# ls -l dump/mongodevdb
total 48
-rw-r--r--. 1 root root  4871 Sep 7 11:47 employee.bson
-rw-r--r--. 1 root root   106 Sep 7 11:47 employee.metadata.json
-rw-r--r--. 1 root root   425 Sep 7 11:47 system.indexes.bson
-rw-r--r--. 1 root root    94 Sep 7 11:47 system.users.bson
-rw-r--r--. 1 root root   239 Sep 7 11:47 system.users.metadata.json
..
If the mongo instance has multiple database (for example, mongodev and mongoprod), execute the mongodump command couple of time as shown below to backup both the database.
# mongodump --db mongodevdb --username mongodevdb --password YourSecretPwd

# mongodump --db mongoproddb --username mongoproddb --password YourSecretProdPwd
If you don’t want to specify the password in the mongodump command line, you can also enter the password interactively.
# mongodump --db mongodevdb --username mongodevdb --password
Enter password:
Please note that you’ll get the following error message if you don’t pass any parameters to the mongodump command.
# mongodump
Fri Sep 7 09:54:27.639 ERROR: output of listDatabases isn't what we expected, no 'databases' field: { ok: 0.0, errmsg: "unauthorized" }

3. Backup a specific Collection

Instead of backing up all the collections in a particular database, you can also backup specific collections.
The following example connects to mongodevdb database and does a backup of only employee collection.
# cd /backup

# mongodump --collection employee --db mongodevdb --username mongodevdb --password YourSecretPwd
connected to: 127.0.0.1
Fri Sep 7 10:13:45.927 DATABASE: mongodevdb to dump/mongodevdb
Fri Sep 7 10:13:45.927 mongodevdb.employee to dump/mongodevdb/employee.bson
Fri Sep 7 10:13:45.928 4 objects
Fri Sep 7 10:13:45.928 Metadata for mongodevdb.employee to dump/mongodevdb/employee.metadata.json
Also, if you are trying to execute mongodump when the mongoDB instance is not up and running, you’ll get the following error message.
#  mongodump --db mongodevdb --username mongodevdb --password YourSecretPwd
couldn't connect to [127.0.0.1] couldn't connect to server 127.0.0.1:27017

4. Backup to a specific Location

In all the above examples, mongodump created a dump directory under the current directory from where the command was executed.
Instead, if you want to backup mongoDB to a specific location, specify the –out parameter as shown below.
The following example takes a backup of employee collection and stores it under /dbbackup directory.
# mongodump --collection employee --db mongodevdb --username mongodevdb --password YourSecretPwd --out /dbbackup
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.
# ls -ltr /dbbackup/mongodevdb
-rw-r--r--. 1 root root 4848 Sep 7 10:19 employee.bson
-rw-r--r--. 1 root root  106 Sep 7 10:19 employee.metadata.json
..

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.
[local-host]# mongodump --host 192.168.1.2 --port 37017 --db mongodevdb --username mongodevdb --password YourSecretPwd

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.
cd /backup

mongorestore --dbpath /var/lib/mongo --db mongodevdb dump/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.
# cd /backup

# mongorestore --dbpath /var/lib/mongo --db mongodevdb dump/mongodevdb
Tue Sep 7 11:27:32.454 [tools] dump/mongodevdb/employee.bson
Tue Sep 7 11:27:32.454 [tools] going into namespace [mongodevdb.employee]
Tue Sep 7 11:27:32.465 [tools] warning: Restoring to mongodevdb.employee without dropping. Restored data will be inserted without raising errors; check your server log
7184 objects found
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.
# mongorestore --dbpath /var/lib/mongo --db mongodevdb --drop dump/mongodevdb
Tue Sep 7 11:34:04.946 [tools] dump/mongodevdb/employee.bson
Tue Sep 7 11:34:04.946 [tools]   going into namespace [mongodevdb.employee]
Tue Sep 7 11:34:04.946 [tools]   dropping
Tue Sep 7 11:34:04.946 [tools] CMD: drop mongodevdb.employee
Tue Sep 7 11:34:05.022 [tools] build index mongodevdb.employee { _id: 1 }
Tue Sep 7 11:34:05.028 [tools] build index done.  scanned 0 total records. 0.006 secs
7184 objects found

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.
mongorestore --host 192.168.1.2 --port 3017 --db mongodevdb --username mongodevdb --password YourSecretPwd --drop /backup/dump

1 comment:

  1. MongoDB backup tools is really very useful to take backup and restore. I found this blog content very helpful. Thanks for sharing

    ReplyDelete