How to Backup and Restore MongoDB Database
1. Backup MongoDB Database – mongodump
There are various options to take backups of mongodb database. Use mongodump command to take all databases backup or a single database backup of backup of single collection.
Backup Single Database
Use this command to take backup of a single database (named mydb) only. Backup will be created in /backup/mongo/ directory.
–db – database name to backup
–out – database backup location. This will create folder with database name.
You can specify host, port, username and password for remote databases connections backups like below.
Backup All Databases
To back up all databases you just need to run the following command. Here /data/db/ is the location of your MongoDB data directory and /backup/mongo is the location of the backup directory.
You can specify host, port for remote databases.
Backup Single Collection
This command will take backup of single collection from a database. Backup files will be created in /backup/mongo/mydb/ directory.
2. Restore MongoDB Database – mongorestore
–drop – Will remove database if already exist.
You can simply move backup files to a remote server and run the same command there to restore backups.
3. MongoDB Backup Shell Script
You can easily schedule the below script in the scheduler to backup databases regularly. Create a file as follows
Add below content to file. Update database hostname, database name , username and password accordingly.
Now configure this in crontab to run daily.
1 | 0 2 * * * /backup/mongo-backup.sh |
0 Comments
CAN FEEDBACK
Emoji