Back Up and Restore MongoDB

MongoDB is a popular database program. It has become especially popular among nodejs users, replacing mySQL as the preferred database program for more nodejs users.

Creating Backups and restoring mongoDB databases is simple. The command used to create a mongoDB backup is


Note that the mongod server must be running for this command to work. This command will backup all the databases present in the current mongoDB instance. The output of this command will be a ./dump/ directory.

To specify the output use:

mongodump -o MONGO_DIR

where MONGO_DIR is the location you want the backups to be placed.

To remotely backup a mongodb database you can use

mongodump --host HOST_NAME --port PORT_NUMBER

Where HOST_NAME is the host name or IP address of the server and the PORT_NUMBER is the mongod port.

To backup a specific database use:

mongodump -d DATABASE_NAME

Where DATABASE_NAME is the database to backup.

To backup a specific collection

mongodump --collection COLLECTION --db DB_NAME

where COLLECTION is the collection name and DB_NAME is the name of the database containing that collection.

Restoring MongoDB Backups

Restoring backups is also easy. To restore a mongoDB database you should use the 'mongorestore' command.

All you need is the path of the backup you are restoring.

mongorestore BACK_UP_DIR

Where BACK_UP_DIR is the path to the backup file (ie '~/mongo-backups').