Module9 Backup Recovery
Module9 Backup Recovery
SESSION 1
OBJECTIVES
• Mongoexport & Mongoimport
• Mongodump & Restore
• Oplog backups
• Filesystem snapshot backup & restore
• Backup using MMS & Ops Manager
INTRODUCTION TO BACKUPS
• Taking backups is an important administrative task in MongoDB as with any data storage system
• You should have a strategy for capturing and restoring backups in the case of data loss events.
• If the volume where MongoDB stores data files supports point in time snapshots, you can use these snapshots
to create backups of a MongoDB system at an exact moment in time
• To get a correct snapshot of a running mongod process, you must have journaling enabled and the journal
must reside on the same logical volume as the other MongoDB data files
• Without journaling enabled, there is no guarantee that the snapshot will be consistent or valid.
• To get a consistent snapshot of a sharded system, you must disable the balancer and capture a snapshot from
every shard and a config server at approximately the same moment in time.
• If your storage system does not support snapshots, you can copy the files directly using cp, rsync, or a similar
tool.
• you must stop all writes to the mongod before copying the files.
MONGOEXPORT
• In the following example, mongoexport exports data from the collection contacts collection in the users
database in CSV format to the file /opt/backups/contacts.csv.
• The mongod instance that mongoexport connects to is running on the localhost port number 27017.
• When you export in CSV format, you must specify the fields in the documents to export. The operation
specifies the name and address fields to export.
• Then, using the --fieldFile option, specify the fields to export with the file:
• In the following example, mongoimport imports the data in the JSON data from the contacts.json file into the
collection contacts in the users database.
mongoimport --host mongodb1.example.net --port 37017 --username user --password pass --collection
contacts --db marketing --file /opt/backups/mdb1-examplenet.json
CSV Import
mongoimport --db users --collection contacts --type csv --headerline --file /opt/backups/contacts.csv
MONGODUMP BACKUP
• The mongodump utility backs up data by connecting to a running mongod or mongos instance.
• To run mongodump against a MongoDB deployment that has access control enabled, you must have privileges
that grant find action for each database to back up.
• The built-in backup role provides the required privileges to perform backup of any and all databases.
• The utility can create a backup for an entire server, database or collection, or can use a query to backup just
part of a collection.
• To backup data from a mongod or mongos instance running on the same machine and on the default port of
27017, use the following command:
mongodump
mongodump --host mongodb1.example.net --port 3017 --username user --password pass --out
/opt/backup/mongodump-2013-10-24
MONGORESTORE
• The mongorestore utility restores a binary backup created by mongodump.
• The mongorestore utility restores data by connecting to a running mongod or mongos directly.
• mongorestore can restore either an entire database backup or a subset of the backup.
• To use mongorestore to connect to an active mongod or mongos, use a command with the following prototype
form:
mongorestore dump-2013-10-25/
• Here, mongorestore imports the database backup in the dump-2013-10-25 directory to the mongod instance
running on the localhost interface on the default port 27017.
OPLOG BACKUP & RESTORE
• Point in Time Operation Using Oplogs
• Use the --oplog option with mongodump to collect the oplog entries to build a point-in-time snapshot of a
database within a replica set.
• With --oplog, mongodump copies all the data from the source database as well as all of the oplog entries from
the beginning to the end of the backup procedure.
mongodump --oplog
• This operation, in conjunction with mongorestore --oplogReplay, allows you to restore a backup that reflects
the specific moment in time that corresponds to when mongodump completed creating the dump file.
• If you created your database dump using the --oplog option to ensure a point-in-time snapshot, call
mongorestore with the --oplogReplay option, as in the following example:
• Create a Snapshot,
lvcreate --size 100M --snapshot --name mdb-snap01 /dev/vg0/mongodb
umount /dev/vg0/mdb-snap01
dd if=/dev/vg0/mdb-snap01 | gzip > mdb-snap01.gz
• Ensures that the /dev/vg0/mdb-snap01 device is not mounted. Never take a block level copy of a filesystem or
filesystem snapshot that is mounted.
• Performs a block level copy of the entire snapshot image using the dd command and compresses the result in a gzipped
file in the current working directory.
• To restore a snapshot created with LVM, issue the following sequence of commands:
lvcreate --size 1G --name mdb-new vg0
gzip -d -c mdb-snap01.gz | dd of=/dev/vg0/mdb-new
mount /dev/vg0/mdb-new /srv/mongodb
• Creates a new logical volume named mdb-new, in the /dev/vg0 volume group. The path to the new device will
be /dev/vg0/mdb-new.
• Uncompresses and unarchives the mdb-snap01.gz into the mdb-new disk image.
• Mounts the mdb-new disk image to the /srv/mongodb directory. Modify the mount point to correspond to
your MongoDB data file location, or other location as needed.
THANK YOU