MongoDB - How to backup and restore a database

This is a quick example to show how to easily backup and restore a MongoDB database from the command line, using the current date as the backup folder name (YYYYMMDD).

Backup MongoDB database using date for the folder name

The mongodump command will create a backup / dump of the MongoDB database with the name specified by the --db [DB NAME] argument.

The --out /var/backups/`date +"%Y%m%d" argument specifies the output directory as /var/backups/[TODAY'S DATE] e.g. /var/backups/20190903.

sudo mongodump --db [DB NAME] --out /var/backups/`date +"%Y%m%d"`

Restore MongoDB database from backup

The mongorestore command restores a database to the destination --db [DB NAME] from the specified directory, e.g. /var/backups/20190903.

sudo mongorestore --db [DB NAME] /var/backups/[BACKUP FOLDER NAME]


Subscribe or Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter or GitHub to be notified when I post new content.


Supported by