1:创建测试数据
> use maxiangqian switched to db maxiangqian > db.oplogtest.insert({"id":"1"}) WriteResult({ "nInserted" : 1 }) > db.oplogtest.insert({"id":"2"}) WriteResult({ "nInserted" : 1 }) > db.oplogtest.insert({"id":"3"}) WriteResult({ "nInserted" : 1 })
2:做一个全备份
/home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/mongodump --host=10.103.16.34 --port=27017 --db=maxiangqian --out=/home/mongodb/backup/bkm
3:再插入几条测试数据
rsmxqtest:PRIMARY> db.oplogtest.insert({"id":"4"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.oplogtest.insert({"id":"5"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.oplogtest.insert({"id":"6"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.oplogtest.insert({"id":"7"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.oplogtest.insert({"id":"8"}) WriteResult({ "nInserted" : 1 })
3:备份特定时间点的日志
mongodump --port 27017 -d local -c oplog.rs -q '{ts:{$lt:Timestamp(1415928580, 1),$gt: Timestamp(1415928529, 1000)}}' -o /home/mongodb/backup/opm
从某个时间点的一个oplog
mongodump --port 27017 -d local -c oplog.rs -q '{ts:{$gt: Timestamp(1489135578, 1)}}' -o /home/mongodb/backup/opm
4:将日志导出文件
/home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/bsondump /home/mongodb/backup/opm/local/oplog.rs.bson >optest
5:查看相关maxiangqian.oplogtest相关记录:
cat optest|grep maxiangqian.op
5:查找日志点恢复到6的记录:
时间点是:
1489135569
6:先恢复整库:
/home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/mongorestore --port=27017 /home/mongodb/backup/bkm/
7:验证数据:
oplogtest > db.oplogtest.find() { "_id" : ObjectId("58c267a24bbb77d479a0cb40"), "id" : "1" } { "_id" : ObjectId("58c267a54bbb77d479a0cb41"), "id" : "2" } { "_id" : ObjectId("58c267a84bbb77d479a0cb42"), "id" : "3" }
8:恢复oplog到时间戳1489135569
/home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/mongorestore -h 127.0.0.1 -p 27017 --oplogReplay --oplogLimit "1489139135:1" /home/mongodb/backup/opm/local/oplog.rs.bson
9:验证数据
rsmxqtest:PRIMARY> db.oplogtest.find() { "_id" : ObjectId("58c275344bbb77d479a0cb48"), "id" : "1" } { "_id" : ObjectId("58c275384bbb77d479a0cb49"), "id" : "2" } { "_id" : ObjectId("58c2753b4bbb77d479a0cb4a"), "id" : "3" } { "_id" : ObjectId("58c275b84bbb77d479a0cb4b"), "id" : "4" } { "_id" : ObjectId("58c275bc4bbb77d479a0cb4c"), "id" : "5" }
试试证明,恢复到了5的时候,所以要恢复到6的话,时间点应该是7开始的时间戳