All the below commands will run database server on local system. If you want to backup databases from remote server, add following switches –host db.remotehost.net –port 27017 –username myuser –password mypasswd
1. Backup MongoDB Database
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.
1.1 Backup All Databases
To backup all databases you just need to run following command. Here /data/db/ is location of your mongodb data directory and /backup/db is location of backup directory.
# mongodump --dbpath /data/db/ --out /backup/db/
1.2 Backup Single Database
Use this command to take backup of a single database (named dummydb) only. Backup will be created in /backup/db/ directory.
# mongodump --db dummydb --dbpath /data/db/ --out /backup/db/
1.3 Backup Single Collection
This command will take backup of single collection from a database. Backup file will be created in dump/mydb/ directory
# mongodump --collection mycollection --db dummydb
2. Restore MongoDB Database
# mongorestore --dbpath /data/db/ /backup/db/