It's a general need in MongoDB development, to take backups or restore DB with an old backup. DB backup can be done at both DB level and individual Collection level. Let's see how we can perform different backup/restore operations..
Another way is to take backup as bson documents.
In case you have multiple instances running on same server, you can use --host and --port options.
DB Backup
Complete DB
Assuming database to backup is "mydb", the best thing is to back it as gzip to save some space on your Server.mongodump --archive=mydb.2017-09-21.gz --gzip --db mydb
This will create the archive in the directory where you're executing the command.Another way is to take backup as bson documents.
mongodump -h localhost -p 27017 -d mydb -o C:\mongobackup\20170921
A directory with db name will be created under C:\mongobackup\20170921In case you have multiple instances running on same server, you can use --host and --port options.
Individual Collection
mongodump --collection myCollection --db mydb
This will create backup with same name as that of collection (myCollection in this case).
Restore Database
Restoring from an archive
mongorestore --gzip --archive=mydb.2017-09-21.gz --db mydb
Restoring from bson
Assuming you took backup in C:\mongobackup\20170921
mongodump -h localhost -p 27017 -db mydb C:\mongobackup\20170921\mydb
Completely agree... I found very useful information on restore database MongoDB. Thanks for sharing.
ReplyDeleteHi there, I enjoy reading through your article post. Thank you for sharing.
ReplyDeleteMern stack online training
Mern stack training in hyderabad
good one.thanks for shared this post
ReplyDeleteFull Stack Training in Chennai | Certification | Online Training Course | Full Stack Training in Bangalore | Certification | Online Training Course | Full Stack Training in Hyderabad | Certification | Online Training Course | Full Stack Training in Pune | Certification | Online Training Course | Full Stack Training | Certification | Full Stack Online Training Course