问题
I was given the task to setup incremental backups for mongodb replicaset, as start point of course I googled about it and could not find anything on mongodb docs, I did find however this question which encouraged to develop my own solution as didn't find Tayra very active.
I read about oplog and realized it was very easy to develop something to replay the log, but it turns out that I didn't have to as mongorestore
does that for me.
Now I have a working solution with bash scripts and it was quite easy, that's the reason I am asking here if there is any flaw in my logic, or maybe something that will bite me in the future.
Below how I implemented that:
Full backup procedure:
- lock writes on a secondary member
db.fsyncLock()
- Take snapshot
- Record last position from oplog
db.oplog.rs.find().sort({$natural:-1}).limit(1).next().ts
- Unlock writes
db.fsyncUnlock()
Incremental backup procedure:
- lock writes on a secondary member
- Dump oplog from the 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 as for full backups)
- Unlock writes
Full backup restore procedure:
- stop all instances of mongod
- copy snapshot to data dir of the box which will be the primary, but make sure to exclude all
local*
andmongod.lock
this restore technique is called reconfigure by breaking mirror - Start primary
- reconfigure replicaset
- start secondaries without any data, let them perform initial sync. Or copy the data from the new primary with fresh
local
database
Restore incremental backup:
When we created incremental backup it stored it like 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 but we will have to rename it, so here are the steps:
- change directory to the backup:
cd /mnt/mongo-test_backup/1/local
- delete the json file
rm *.json
- rename the bson file
mv oplog.rs.bson oplog.bson
- restore it :
mongorestore -h <primary> --port <port> --oplogReplay /mnt/mongo-test_backup/1/local
I have it all scripted, I may commit it on github later.
Question is if there is any flaw in the logic. I am bit suspicious as the procedure is quite straight forward and still I couldn't find it documented anywhere.
来源:https://stackoverflow.com/questions/31605963/mongodb-incremental-backups