There are three common methods to take the backup in mongodb.
- Take the backup using the filesystem snapshot :
This is the easiest method to take the backup. For taking the snapshot backup two requirements are there.
a. Filesystem should support the snapshot.
b. The database should have journaling enabled.
Restoring the backup:-
Stop the mongod service if it is running.
Restore the snapshot.
Start the mongod service , this will use the journal logs and recover the database to consistent state.
Another way of creating backups is to make a copy of everything in the data directory. Copying will happen file by file so there may be a chance that one file will be modified while copying another which will create inconsistency. So we will lock all the data files before copying using the below command.
“info” : “now locked against writes, use db.fsyncUnlock() to unlock”,
“ok” : NumberInt(1)
This command will flush the data in buffer to the data files before locking the files. All the databases write operations in the cluster are in locked state. The writes will be in queue. Once the command returns star copying files.
cp -R /data/db/ /backup/
Once the copy is completed execute the below command to unlock the files.
“info” : “unlock completed”,
“ok” : NumberInt(1)
Mongodump is a tool to take backup in binary format, like taking dump in other databases (In oracle expdp and postgres pgdump). This is slow in taking backup and doing restorations but we can take the backups at collection level also using this.
mongodump only captures the documents in the database in its backup data and does not include index data. mongorestore or mongod must then rebuild the indexes after restoring data.
mongodump will create a dump directory in the current directory, which contains a dump of all of your data.
To back up all the collections in the database:
C:\BACKUP>mongodump -h localhost:27017
2016-04-26T19:25:39.359+0530 writing test.employee to
2016-04-26T19:25:39.360+0530 writing test.emp to
2016-04-26T19:25:39.362+0530 done dumping test.emp (1 document)
2016-04-26T19:25:39.362+0530 done dumping test.employee (2 documents)
This command backed up all the collections(employee and emp) in the test database.
Some useful options of command:
–db , -d
Take the backup of this database (all collections)
–collection , -c
Specify the collection to take the backup.
C:\BACKUP>mongodump -h localhost:27017 -d test -c emp
2016-04-26T19:41:53.114+0530 writing test.emp to
2016-04-26T19:41:53.116+0530 done dumping test.emp (1 document)
–query , -q
Query to include only some documents in the backup based on query.
File containing the query
Forces mongodump to scan the data store directly: typically, mongodump saves entries as they appear in the index of the _id field.
Zip the backup file.
–out , -o
Location of directory where the backup files will be created.
Write the data to single archive file.
This will write only valid data. Do not write objects which are in invalid state.
Creates a file named oplog.bson as part of the mongodump output. The oplog.bson file, located in the top level of the output directory, contains oplog entries that occur during the mongodump operation. This file provides an effective point in-time snapshot of the state of a mongod instance. To restore to a specific point-in-time backup, use the output created with this option in conjunction with mongorestore –oplogReplay.
Without –oplog, if there are write operations during the dump operation, the dump will not reflect a single moment in time. Changes made to the database during the update process can affect the output of the backup.
Number of parallel collections to be exported by default 4.
mongorestore command is used to restore the data from backup created by mongodump.