web analytics

MongoDB backup/restore

Want to know how the backup of mongo database can be taken. Read below…

Four tools are described here.

1) mongoexport

mongoexport can be used to take the backup of a mongo database. It takes the backup to a CSV, TSV or JSON files. The syntax is as follows.

mongoexport -d db_name -c test -o db_name.csv

where -d is the database name
-c is the collection name
-o is the file name to which the backup is to be taken

2) mongoimport

mongoimport can be used to import a backup to a database. You can import from the backup file as follows.

mongoimport -d db_restore -c test –file db_name.csv

where -d is the name of the database to which the file is to be restored
-c is the collection name
–file is the backup file

3) mongodump

For those who need a complete backup of the database, you can use mongodump command to do so. Unlike mongoexport, the backup will be done into a folder and not file.

mongodump -d db_test -o /backuptest

where -d is the database name
-o is the folder to which data is to be backed up

Once completed, the backup will be present in /backuptest/db_test/

4) mongorestore

To restore an entire database, you can use the mongorestore command. Let’s restore the backup that we took in the previous step.

mongorestore -d db_test_restore /backuptest/db_test/

where -d is the existing or new database to which the data is to be restored
and the folder /backuptest/db_test/ is where the backup of db_test exists.

Try it and see, it works 🙂

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>