bash - Mongodb incremental backups -
i given task setup incremental backups mongodb replicaset, start point of course googled , not find on mongodb docs, did find question encouraged develop own solution didn't find tayra active.
i read oplog , realized easy develop replay log, turns out didn't have mongorestore
me.
now have working solution bash scripts , quite easy, that's reason asking here if there flaw in logic, or maybe bite me in future.
below how implemented that:
full backup procedure:
- lock writes on secondary member
db.fsynclock()
- take snapshot
- record last position oplog
db.oplog.rs.find().sort({$natural:-1}).limit(1).next().ts
- unlock writes
db.fsyncunlock()
incremental backup procedure:
- lock writes on secondary member
- dump oplog recorded oplog position on full (or latest incremental ) backup:
mongodump --host <secondary> -d local -c oplog.rs -o /mnt/mongo-test_backup/1 --query '{ "ts" : { $gt : timestamp(1437725201, 50) } }'
- record latest oplog position (same way full backups)
- unlock writes
full backup restore procedure:
- stop instances of mongod
- copy snapshot data dir of box primary, make sure exclude
local*
,mongod.lock
restore technique called reconfigure breaking mirror - start primary
- reconfigure replicaset
- start secondaries without data, let them perform initial sync. or copy data new primary fresh
local
database
restore incremental backup:
when created incremental backup stored this:
/mnt/mongo-test_backup/1/local/oplog.rs.bson /mnt/mongo-test_backup/1/local/oplog.rs.metadata.json
we're instered on oplog.rs.bson have rename it, here steps:
- change directory backup:
cd /mnt/mongo-test_backup/1/local
- delete json file
rm *.json
- rename bson file
mv oplog.rs.bson oplog.bson
- restore :
mongorestore -h <primary> --port <port> --oplogreplay /mnt/mongo-test_backup/1/local
i have scripted, may commit on github later.
question if there flaw in logic. bit suspicious procedure quite straight forward , still couldn't find documented anywhere.
This blog share good information on MongoDB incremental backup and its procedure. Thanks for sharing
ReplyDelete