MongoDB stores all its data in the data directory
mentioned by --dbpath during startup. Taking
backup of MongoDB is as simple as taking a copy of all data files in the data
directory.
Creating a copy of datafile is not recommended while MongoDB is
running as it can create a corrupt or useless backup. It is always good to take a copy of datafile after safely shutting down the MongoDB server, but this will surely
require downtime. In order to avoid downtime, we can use the below methods to take
backup.
MONGODUMP
One of the methods of taking MongoDB backing while the database is running
is to use the mongodump utility. mongodump
generally works on query mechanism, it will cause slight performance
degradation on the database during the backup time.
It can be run against a live instance, even one handling other
requests and performing writes.
We can see the options with mongodump using mongodump –help
C:\Program
Files\MongoDB\Server\3.6\bin>mongodump --help
Usage:mongodump
/help print usage
/v,
/verbose: more detailed
log output (include multiple times for more verbosity, e.g. - vvvvv, or specify a numeric value, e.g. --verbose=N)
/h,
/host: mongodb host to connect
to (setname/host1,host- 2 for replica sets)
/u, /username: username for authentication
/p, /password: password for authentication
/d, /db: database to
use
/c, /collection: collection to use
/uri:mongodb-uri mongodb uri connection string
/q, /query: query filter, as a JSON string, e.g., '{x:{$gt:1}}'
/o, /out: output directory,
/uri:mongodb-uri mongodb uri connection string
/q, /query: query filter, as a JSON string, e.g., '{x:{$gt:1}}'
/o, /out:
gzip compress
archive our collection output with Gzip
Below is an example for taking the backup of database "techon_db" in directory D:\mongodb\backup_dump
mongodump -d techon_db -o
D:\mongodb\backup_dump
MONGORESTORE
Along with mongodump,
there comes mongorestore for importing data in MongoDB which was taken using
mongodump, backup taken using mongodump can be restored in different or new
database as well.
Example
mongorestore
-d techon1_db D:\mongodb\backup_dump\techon_db
In the above example, we have restored backup taken
using mongodump for database techon_db into techon1_db. all steps are clearly
displayed in the screenshot.
all options can be displayed using mongorestore --help
fsync and Lock
As we already discussed, the above
methods allow us to perform its operations while the database is running, but here
we tend to lose some ongoing activity backup as we will miss on point in
time recovery backups. MongoDB's fsync command allows us to copy the data directory
of the running MongoDB server while the database is running.
fsync command forces to flush all
pending writes to disk. It also, optionally holds a lock preventing any further
writes to the database until it is unlocked.
Example of fsync
>db.runCommand ({"fsync" :
1, "lock" : 1});
{
"info" : "now locked against
writes, use db.$cmd.sys.unlock.findOne() to unlock",
"ok" : 1
}
This will allow us to take
consistent backup. Once the backup complete, we need to unlock the database
>db.$cmd.sys.unlock.findOne();
{"ok" : 1, "info" :
"unlock requested"}
>db.current0p ();
{"inprog" : []}
The meaning of db.current0p is
confirmation of the lock has been released.
Pros for fsync are only putting a lock on reads or write on the database. The only solution for this is Slave Backup
Slave Backup
All methods discussed above
provide enough flexible backups, but the most flexible backup is taking backup
from a slave server. When running MongoDB with replication, above-all methods can
be applied to take a backup from the slave server instead of the primary one
Slave backup of the recommended way
to backup MongoDB database
0 comments:
Post a Comment