Introduction :
Database backup is always required either you are developing an application or it is running on production. In this blog post, I will show you how to take the backup of a mongodb database using mongodump and how to restore that database using mongorestore.
mongodump :
mongodump is a command line utility for taking database dump. It can be used with mongod or mongos. In this tutorial, I will show you how to take the database dump of a local mongod or from a remote host using mongodump.
mongorestore :
mongorestore is used to load data from a binary database dump generated by mongodump or from standard input. It can load data to a mongod or mongos instance.
Both mongodump and mongorestore can be used to syncing the database between different environments like production and development. You can use these commands directly from the system command line.
mongodump overwrites the output data. Make sure to use a different folder or rename the existing folder before running it.
mongodump backup for local interface on port 27017 :
Backup of a collection :
mongodump --db DB_NAME --collection COLLECTION_NAME
Compress the output :
mongodump --gzip --db DB_NAME
Full database backup :
mongodump -d DB_NAME -o DIRECTORY_PATH
backup for a remote database :
mongodump --host HOST --port PORT --username USER --password PASSWORD --out PATH
mongorestore for local interface :
Restore a collection :
mongorestore --collection COLLECTION_NAME --db DB_NAME BACKUP_PATH
Restore from a archive backup :
mongorestore --archive=ARCHIVE_FILE --db DB_NAME
Restore from compressed data :
mongorestore --gzip --archive=COMPRESSED_FILE --db DB_NAME
Full restore :
mongorestore -d DB_NAME BACKUP_PATH
Restore to a remote database :
mongorestore --host HOST --port PORT --username USER_NAME --password PASSWORD BACKUP_PATH