Mongodump and Mongorestore: Easy Guide for MongoDB Backup & Recovery

Feature image of mongodump and mongorestore
As a Mongo DBA, we must learn how to use mongodump and mongorestore to back up MongoDB and restore the databases efficiently. This step-by-step guide covers best practices with detailed examples and troubleshooting tips for MongoDB backup and MongoDB restore. Let’s go through this step by step :

What is Mongo dump?

Mongodump is a MongoDB utility for creating a binary backup of a database. Mongodump creates BSON data for later restoration with mongorestore.

Key Features:

  • It exports MongoDB collections as BSON files.
  • It supports collection backups.
  • Works with remote and local MongoDB instances.

Example :

mongodump creates a binary backup of a MongoDB database

1. BAckup of a database :

Syntax : 
mongodump --host <hostname> --port <port> --db <database_name> --out <backup_directory>
Example :
mongodump --db sample_new --out /u01/backup/mongodb/

This will take MongoDB backup (sample_new) and create a folder /u01/backup/mongodb/sample_new containing BSON and metadata files.

Output :

Image 11

2. Backup of All the Database :

Syntax :
mongodump --out <directory_name>
Example :
mongodump --out /u01/all_backup/mongodump/

Output :

Image 14

3. BAckup of a Collection :

Syntax : 
mongodump --db <db_name> --collection <collection_name> --out /backup/mongodb/mongo_collection
Example :
mongodump --db sample_new --collection test --out /u01/backup/mongodb/mongo_collection

It backs up only the test collection from sample_new database.

Output :

Image 15

4. Backup with Compression :

Syntax :
mongodump --db <db_name> --archive=<directpry_name>/mongodump.gz --gzip
Example :
mongodump --db sample_new --archive=/u01/backup/mongodump.gz --gzip

Output :

Image 21

What is MongoRestore ?

Mongorestore is the counterpart of mongodump that allows users to restore MongoDB databases from BSON backup files.

Key Features:

  • It restores entire databases or specific collections.
  • It supports restoration to a different database.
  • It works with authentication and remote instances.
Syntax :
mongorestore --host <hostname> --port <port> --db <database_name> <backup_directory>

1.Restore Single Database :

mongorestore --db sample_new /u01/backup/mongodb/sample_new

It restores the sample_new database from the /backup/mongodb/sample_new directory.

Output :

First, drop the sample_new database

Image 17

Now, restore from the backup that was taken .

Image 18

2. Restore All the Database :

mongorestore /backup/mongodump/

3. Restore a Collection from Mongodump :

mongorestore --db sample_new --collection test /u01/backup/mongodb/mongo_collection/sample_new/test.bson

Output :

First, drop the collection.

Image 19

Now, restore from the backup taken :

Image 20

4. Restore from a Compressed Backup :

mongorestore --gzip --archive=/backup/mongodump.gz

5. Restore to a Different database :

mongorestore --db sample /u01/backup/mongodb/sample_new

Restores sample_new backup into sample.

Hope this helps!! Also, you can get more information from the Official DOc as well.

Other MongoDB Related Articles :

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top