基于时间点的恢复.md

1:34服务器登录 use maxiangqian show collections 插入三条数据: rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog1"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog2"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog3"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.find()


2:备份34服务器27017端口的mongodb数据库 /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/bk2


3:回到34服务器: 继续插入数据 rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog4"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog5"}) WriteResult({ "nInserted" : 1 }) rsmxqtest:PRIMARY> db.maxiangqian.insert({"name":"oplog6"}) WriteResult({ "nInserted" : 1 }) 删除一条数据: db.maxiangqian.remove({"name":"oplog1"})


4:备份数据库的oplog,主要是考虑能够进行数据恢复,日志链还是很重要的: /home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/mongodump --host=10.103.16.34 --port=27017 --db=local --collection=oplog.rs --out=/home/mongodb/backup/bkoplog


备份出特定时间点的oplog: mongodump --port 27017 -d local -c oplog.rs -q '{ts:{$lt:Timestamp(1415928580, 1),$gt: Timestamp(1415928529, 1000)}}' -o /tmp/oplog1/ 备份以前多少时间段的一些oplog: mongooplog --from mongodb0.example.net --seconds 43200


5:查找删除的记录: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"d"" | head 给出一行删除操作的oplog记录: {"ts":{"$timestamp":{"t":1489116805,"i":1}},"t":{"$numberLong":"5"},"h":{"$numberLong":"9163517542676168219"},"v":2,"op":"d","ns":"test3.a","o":{"_id":{"$oid":"58c21ddc4bbb77d4799f40bf"}}} 也可以把备份的日志导出文件: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"d"" >/home/mongodb/backup/maxiangqiantest 然后通过其他方式查找到时间点: cat tail more head等


6:然后进行恢复: /home/maxiangqian/mongodb-linux-x86_64-rhel62-3.4.2/bin/mongorestore -h 127.0.0.1 -p 27017 --oplogReplay --oplogLimit "1489129593:1" /home/mongodb/backup/oplog/local/oplog.rs.bson


7:恢复完成


bsondump对oplog的操作: 查找删除的记录: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"d"" | head 查找插入的记录: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"i"" | head 查找一些类似创建等数据库操作: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"c"" | head

你可能感兴趣的