How to MongoDB- Backup & Restore MongoDB

This tutorial describes the process for creating backup & restore data using the utilities provided with MongoDB. The mongodump and mongorestore utilities work with BSON data dumps.

When backing up your data with MongoDB’s tools, consider the following guidelines:

Label files so that you can identify the contents of the backup as well as the point in time that the backup reflects.
Use an alternative backup strategy such as Filesystem Snapshots or MongoDB Cloud Manager
Use –oplog to capture incoming write operations during the mongodump operation to ensure that the backups reflect a consistent data state.
Ensure that your backups are usable by restoring them to a test MongoDB deployment.

 

Backup MongoDB

To backup MongoDB, run the following code on Mongo command prompt.

Syntax
    >mongodump

 

Example
    C:\Program files\mongodb\bin>mongodump

 

The above example works only if it connected to localhost. For connecting to a remote server, You must specify the –host and –port of the MongoDB instance that the mongodump should connect to.

Remote connection
    >mongodump --host mongodb.example.net --port 27017

 

To backup only specified database at specified path, use

Syntax
    mongodump --out /data/backup/

 

To backup only specified collection of specified database, use the following command

Syntax
    mongodump --collection myCollection --db test

 

Restore Records

To restore backup data MongoDB’s mongorestore command is used. This command restores all of the data from the backup directory.

Syntax
    >mongorestore

 

Example
    C:\Program files\mongodb\bin>mongorestore

 

To restore from the different location, go to the directory where the backup is located, and execute the restore command as shown below.

Restore from diffrent location
     >mongorestore --dbpath /var/lib/mongo dump

 

You can also restore a mongo backup to a MongoDB instance running on a remote server using following command.

Syntax
    >mongorestore --host host_ip --port port_number --db mongodevdb --username mongodevdb --password YourSecretPwd --drop /backup/dump

 

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...