Mongodb database Backup and Restore

1.Open cmd with run as admin.
2.Run following command to take backup of database named analytics in mongodb.

mongodump –host XXX.XXX.XXX.XXX –port 27021 –db analytics –out E:\MongoDB\dumps

#mongodump –host <source host ip or hostname> –port <port at which mongodb is running> –db <database of which backup is required> –out <Directory path where backup would be taken>

Note this will create a folder named after your given database. It will create backup files consisting of bson and json files.

If face any issue like below.

‘mongodump’ is not recognized as an internal or external command, operable program or batch file.

Then please do provide following path or path where you have installed mongodb in Environment Variables. Provide bin folder path.

C:\Program Files\MongoDB\Server\3.0\bin

Or if you do not have time then Go to this folder from cmd as below.

cd C:\Program Files\MongoDB\Server\3.0\bin

Then run mongodump command.

3. Run following command to restore backup of database analytics.

mongorestore –host XXX.XXX.XXX.XXX –port 27021 –db analytics_new_db E:\MongoDB\dumps\analytics

#mongorestore –host <destination host ip or hostname> –port <port at which mongodb is running> –db <new or target database name> <Directory path containing bson and json files>

To confirm whether database is restored.Run following from cmd.

mongo <target hostname or hostip>:<port>
show dbs

You will see your new database. Now run following commands to see collections.

use analytics_new_db
show collections

You will see your collections of your database. Hope you enjoyed this.

Happy Database Backup and Restoring 🙂