How To Backup and Restore MongoDB

By | 2015/01/30

MongoDB has great management tools for an admin. Here are some tips for backing up mongo!


Backup MongoDB

The command to backup mongo is mongodump with tack d and the name of your database to backup.

$ mongodump -d database_name

By default, this will put the dump into a directory called dump.

The output will look similar to the following:

stmiller@lilybin:~$ mongodump -d LilyPond
connected to: 127.0.0.1
DATABASE: LilyPond       to     dump/LilyPond
        LilyPond.system.indexes to dump/LilyPond/system.indexes.bson
                 2 objects
        LilyPond.sessions to dump/LilyPond/sessions.bson
                 36390 objects
        LilyPond.scores to dump/LilyPond/scores.bson
                 603 objects
stmiller@lilybin:~$ 

You can also customize the output destination. Here is an example backing up the database name of ‘LilyPond’ to my backup location:

sudo mongodump -d LilyPond -o /var/backups/mongodb


Backup a collection

To backup a single collection from a database, use tack c to specify the collection name.

In this example I am backing up the collection called ‘scores’ from the database ‘LilyPond’:

$ mongodump -d LilyPond -c scores


Create a Backup Script

Here is my backup script, /usr/local/bin/mongobackup which is used to backup an entire database called LilyPond:

#!/bin/bash

/usr/bin/mongodump -d LilyPond -o /var/backups/mongodb

I set this in root’s cronjob to run once at night.

$ sudo crontab -e
# mongodb backups!
0 22 * * * /usr/local/bin/mongobackup >/dev/null


Restore MongoDB

To restore from a backup, the command used is mongorestore.

On the same system, the backup can be restored with simply:

$ mongorestore -d database_name my_database_backup

Here I am restoring from a backup:

smiller@bruckner:~$ mongorestore -d LilyPond dump/LilyPond
connected to: 127.0.0.1
Fri Jan 30 14:39:57.699 dump/LilyPond/sessions.bson
Fri Jan 30 14:39:57.699         going into namespace [LilyPond.sessions]
Fri Jan 30 14:39:57.699 dump/LilyPond/sessions.metadata.json not found. Skipping.
Fri Jan 30 14:39:57.699 warning: Restoring to LilyPond.sessions without dropping. Restored data will be inserted without raising errors; check your server log
36390 objects found
Fri Jan 30 14:39:59.006 dump/LilyPond/scores.bson
Fri Jan 30 14:39:59.006         going into namespace [LilyPond.scores]
Fri Jan 30 14:39:59.006 dump/LilyPond/scores.metadata.json not found. Skipping.
Fri Jan 30 14:39:59.957 warning: Restoring to LilyPond.scores without dropping. Restored data will be inserted without raising errors; check your server log
603 objects found
Fri Jan 30 14:39:59.973 dump/LilyPond/system.indexes.bson
Fri Jan 30 14:39:59.973         going into namespace [LilyPond.system.indexes]
Fri Jan 30 14:40:00.001 warning: Restoring to LilyPond.system.indexes without dropping. Restored data will be inserted without raising errors; check your server log
Fri Jan 30 14:40:00.001         Creating index: { key: { _id: 1 }, ns: "LilyPond.sessions", name: "_id_" }
Fri Jan 30 14:40:00.001         Creating index: { key: { _id: 1 }, ns: "LilyPond.scores", name: "_id_" }
2 objects found
smiller@bruckner:~$ 


Restore to development database

To restore to a different database, specify a different database with tack d. Example:

$ mongorestore -d development dump/LilyPond


Restore single collection

To restore a single collection, specify the database (tack d) and the specific .bson collection backup file to restore:

$ mongorestore -d database_name collection_file.bson


For more, check of the friendly manual and Administration section:

http://docs.mongodb.org/manual/