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.
To backup MongoDB, run the following code on Mongo command prompt.
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.
>mongodump --host mongodb.example.net --port 27017
To backup only specified database at specified path, use
mongodump --out /data/backup/
To backup only specified collection of specified database, use the following command
mongodump --collection myCollection --db test
To restore backup data MongoDB’s
mongorestore command is used. This command restores all of the data from the backup directory.
To restore from the different location, go to the directory where the backup is located, and execute the restore command as shown below.
>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.
>mongorestore --host host_ip --port port_number --db mongodevdb --username mongodevdb --password YourSecretPwd --drop /backup/dump