0% found this document useful (0 votes)
27 views12 pages

Module9 Backup Recovery

The document discusses various backup strategies for MongoDB databases, including: 1) Copying the underlying data files, taking filesystem snapshots, or using tools like mongodump and mongoexport to backup specific databases, collections, or subsets of data. 2) mongodump can backup an entire database, collection, or query-filtered subset of a collection, while mongoexport can export data to JSON or CSV formats. 3) mongorestore and mongoimport can be used to restore backups created with mongodump and mongoexport respectively. 4) Oplog backups with mongodump can create point-in-time snapshots when used with mongorestore and oplog replay. 5)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Module9 Backup Recovery

The document discusses various backup strategies for MongoDB databases, including: 1) Copying the underlying data files, taking filesystem snapshots, or using tools like mongodump and mongoexport to backup specific databases, collections, or subsets of data. 2) mongodump can backup an entire database, collection, or query-filtered subset of a collection, while mongoexport can export data to JSON or CSV formats. 3) mongorestore and mongoimport can be used to restore backups created with mongodump and mongoexport respectively. 4) Oplog backups with mongodump can create point-in-time snapshots when used with mongorestore and oplog replay. 5)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MODULE 9 – 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.

• There are several ways to back up MongoDB clusters


• Backup by Copying Underlying Data Files
• Filesystem snapshot backups
• Backup a Database with mongodump
• MongoDB Management Service (MMS) Cloud Backup
• MongoDB Management Service (MMS) Ops Manager Backup Software
COPYING THE DATA FILES
• You can create a backup by copying MongoDB’s underlying data files.

• 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

• The mechanics of snapshots depend on the underlying storage system.

• On Linux the LVM manager can create a snapshot

• 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.

mongoexport --db users --collection contacts --type=csv --fields name,address --out


/opt/backups/contacts.csv

• Then, using the --fieldFile option, specify the fields to export with the file:

mongoexport --db users --collection contacts --type=csv --fieldFile fields.txt --out


/opt/backups/contacts.csv

• Export in JSON Format

mongoexport --db sales --collection contacts --out contacts.json


MONGOIMPORT
• mongoimport restores a database from a backup taken with mongoexport.

• Most of the arguments to mongoexport also exist for mongoimport.

• 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 --db users --collection contacts --file contacts.json

Import JSON to Remote Host Running with Authentication:

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.

• mongodump excludes the content of the local database in its output.

• 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.

• By default, mongorestore looks for a database backup in the dump/ directory.

• 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 --port <port number> <path to the backup>

• Consider the following example:

 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:

 mongorestore –oplogReplay <timestamp>


FILE SYSTEM SNAPSHOT BACKUP
• Flush writes to disk and lock the database to prevent further writes.
 db.fsyncLock();

• 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.

• After the snapshot completes, unlock the database,


 db.fsyncUnlock();
FILE SYSTEM SNAPSHOT RESTORE

• 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

You might also like