Skip to content

Instantly share code, notes, and snippets.

@sethigoldy
Last active December 5, 2023 14:30
Show Gist options
  • Save sethigoldy/e241da174be63170e628a588a4be49ff to your computer and use it in GitHub Desktop.
Save sethigoldy/e241da174be63170e628a588a4be49ff to your computer and use it in GitHub Desktop.
Incremental backups with mongodump and mongorestore
Note:
Oplog should be big enough from the last full backup till the next backup so that mongodump can copy all the changes happened from the last backup.
Also, this is an experimental way of doing backup and restore, not thouroughly tested.
Mongorestore:
mongorestore -h localhost --oplogReplay --port 27017 --oplogFile=backups/incremental/oplog.bson --dir backups/base
#!/bin/bash
New_DATE=$(date +%s)
mongodump -h localhost --port 27017 -o backups/base
Old_DATE=$(cat query.js | awk -F: '{print $5}' | cut -c 1-10)
awk -v old="$Old_DATE" -v new="$New_DATE" '{gsub("\"t\":" old ",", "\"t\":" new ",")} 1' query.js > temp.js && mv temp.js query.js
#!/bin/bash
rm -rf backups/incremental
mkdir -p backups/incremental/
mongodump -h localhost -d local -c oplog.rs --queryFile query.js --port 27017 -o - > backups/incremental/oplog.bson
{"ts":{"$gte":{"$timestamp":{"t":0000000000,"i":1}}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment