1:34服務(wù)器登錄 use maxiangqian show collections 插入三條數(shù)據(jù): 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服務(wù)器27017端口的mongodb數(shù)據(jù)庫(kù) /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服務(wù)器: 繼續(xù)插入數(shù)據(jù) 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 }) 刪除一條數(shù)據(jù): db.maxiangqian.remove({"name":"oplog1"})
4:備份數(shù)據(jù)庫(kù)的oplog嘴高,主要是考慮能夠進(jìn)行數(shù)據(jù)恢復(fù)窑眯,日志鏈還是很重要的: /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
備份出特定時(shí)間點(diǎn)的oplog: mongodump --port 27017 -d local -c oplog.rs -q '{ts:{$lt:Timestamp(1415928580, 1),$gt: Timestamp(1415928529, 1000)}}' -o /tmp/oplog1/ 備份以前多少時(shí)間段的一些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"}}} 也可以把備份的日志導(dǎo)出文件: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"d"" >/home/mongodb/backup/maxiangqiantest 然后通過(guò)其他方式查找到時(shí)間點(diǎn): cat tail more head等
6:然后進(jìn)行恢復(fù): /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:恢復(fù)完成
bsondump對(duì)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 查找一些類似創(chuàng)建等數(shù)據(jù)庫(kù)操作: bsondump /home/mongodb/backup/oplog/local/oplog.rs.bson | grep ""op":"c"" | head