In this blog post, we’ll look at MongoDB point-in-time backups, and work with them.
Mongodump is the base logical backup tool included with MongoDB. It takes a full BSON copy of database/collections, and optionally includes a log of changes during the backup used to make it consistent to a point in time. Mongorestore is the tool used to restore logical backups created by Mongodump. I’ll use these tools in the steps in this article to restore backed-up data. This article assumes a mongodump-based backup that was taken consistently with oplog changes (by using the command flag “–oplog”), and the backup is being restored to a MongoDB instance.
In this example, a mongodump backup is gathered and restored for the base collection data, and separately the oplogs/changes necessary to restore the data to a particular point-in-time are collected and applied to this data.
Note: Percona developed a backup tool named mongodb_consistent_backup, which is a wrapper for ‘mongodump’ with added cluster-wide backup consistency. The backups created by mongodb_consistent_backup (in Dump/Mongodump mode) can be restored using the same steps as a regular “mongodump” backup.
Required, even if you’re using the default host/port (localhost:27017). If authorization is enabled, add –user/–password flags also.
Required for any replset member! Causes “mongodump” to capture the oplog change log during the backup for consistent to one point in time.
Optional. For mongodump >= 3.2, enables inline compression on the backup files.
|
1 |
$ mongodump --host localhost --port 27017 --oplog --gzip<br><br>2016-08-15T12:32:28.930+0200 writing wikipedia.pages to<br>2016-08-15T12:32:31.932+0200 [#########...............] wikipedia.pages 674/1700 (39.6%)<br>2016-08-15T12:32:34.931+0200 [####################....] wikipedia.pages 1436/1700 (84.5%)<br>2016-08-15T12:32:37.509+0200 [########################] wikipedia.pages 2119/1700 (124.6%)<br>2016-08-15T12:32:37.510+0200 done dumping wikipedia.pages (2119 documents)<br>2016-08-15T12:32:37.521+0200 writing captured oplog to<br>2016-08-15T12:32:37.931+0200 [##......................] .oplog 44/492 (8.9%)<br>2016-08-15T12:32:39.648+0200 [########################] .oplog 504/492 (102.4%)<br>2016-08-15T12:32:39.648+0200 dumped 504 oplog entries |
In this stage, we will gather the changes needed to roll the data forward from the time of backup to the time/oplog-position to which we would like to restore.
In this example below, let’s pretend someone accidentally deleted an entire collection at oplog timestamp: “Timestamp(1470923942, 3)” and we want to fix it. If we decrement the Timestamp increment (2nd number) of “Timestamp(1470923942, 3)” we will have the last change before the accidental command, which in this case is: “Timestamp(1470923942, 2)“. Using the timestamp, we can capture and replay the oplogs from when the backup occurred to just before the issue/error.
A start and end timestamp are required to get the oplog data. In all cases, this will need to be gathered manually, case-by-case.
|
1 |
#!/bin/bash<br>#<br># This tool will dump out a BSON file of MongoDB oplog changes based on a range of Timestamp() objects.<br># The captured oplog changes can be applied to a host using 'mongorestore --oplogReplay --dir /path/to/dump'.<br><br>set -e<br><br>TS_START=$1<br>TS_END=$2<br>MONGODUMP_EXTRA=$3<br><br>function usage_exit() {<br> echo "Usage $0: [Start-BSON-Timestamp] [End-BSON-Timestamp] [Extra-Mongodump-Flags (in quotes for multiple)]"<br> exit 1<br>}<br><br>function check_bson_timestamp() {<br> local TS=$1<br> echo "$TS" | grep -qP "^Timestamp(d+,sd+)$"<br> if [ $? -gt 0 ]; then<br> echo "ERROR: Both timestamp fields must be in BSON Timestamp format, eg: 'Timestamp(########, #)'!"<br> usage_exit<br> fi<br>}<br><br>if [ -z "$TS_START" ] || [ -z "$TS_END" ]; then<br> usage_exit<br>else<br> check_bson_timestamp "$TS_START"<br> check_bson_timestamp "$TS_END"<br>fi<br><br>MONGODUMP_QUERY='{ "ts" : { "$gte" : '$TS_START' }, "ts" : { "$lte" : '$TS_END' } }'<br>MONGODUMP_FLAGS='--db=local --collection=oplog.rs'<br>[ ! -z "$MONGODUMP_EXTRA" ] && MONGODUMP_FLAGS="$MONGODUMP_FLAGS $MONGODUMP_EXTRA"<br><br>if [ -d dump ]; then<br> echo "'dump' subdirectory already exists! Exiting!"<br> exit 1<br>fi<br><br>echo "# Dumping oplogs from '$TS_START' to '$TS_END'..."<br><br>mkdir dump<br>mongodump $MONGODUMP_FLAGS --query "$MONGODUMP_QUERY" --out - >dump/oplog.bson<br><br>if [ -f dump/oplog.bson ]; then<br> echo "# Done!"<br>else<br> echo "ERROR: Cannot find oplog.bson file! Exiting!"<br> exit 1<br>fi<br> |
|
1 |
$ ./dump_oplog_range.sh<br>Usage ./dump_oplog_range.sh: [Start-BSON-Timestamp] [End-BSON-Timestamp] [Extra-Mongodump-Flags (in quotes for multiple)] |
Example:
|
1 |
$ wget -q https://raw.githubusercontent.com/percona/MongoToolsAndSnippets/master/rdba/dump_oplog_range.sh<br>$ bash ./dump_oplog_range.sh 'Timestamp(1470923918, 0)' 'Timestamp(1470923942, 2)' '--username=secret --password=secret --host=mongo01.example.com --port=27024'<br># Dumping oplogs from 'Timestamp(1470923918, 0)' to 'Timestamp(1470923942, 2)'...<br>2016-08-12T13:11:17.676+0200 writing local.oplog.rs to stdout<br>2016-08-12T13:11:18.120+0200 dumped 22 documents<br># Done! |
Note: all additional mongodump flags (optional 3rd field) must be in quotes!
|
1 |
$ ls -alh dump/oplog.bson<br>-rw-rw-r--. 1 tim tim 168M Aug 12 13:11 dump/oplog.bson |
In this stage, we apply the time-range-based oplogs gathered in Stage 3 to the restored data set to bring it from the time of the backup to a particular point in time before a problem occurred.
Required, even if you’re using the default host/port (localhost:27017). If authorization is enabled, add –user/–password flags also.
Required. This is needed to replay the oplogs in this step.
Required. The path to the mongodump data.
|
1 |
$ mongorestore --host localhost --port 27017 --oplogReplay --dir ./dump<br>2016-08-12T13:12:28.105+0200 building a list of dbs and collections to restore from dump dir<br>2016-08-12T13:12:28.106+0200 replaying oplog<br>2016-08-12T13:12:31.109+0200 oplog 80.0 MB<br>2016-08-12T13:12:34.109+0200 oplog 143.8 MB<br>2016-08-12T13:12:35.501+0200 oplog 167.8 MB<br>2016-08-12T13:12:35.501+0200 done |
Resources
RELATED POSTS