mongo 误操做恢复数据

场景:我往同一个集合里面插入 三条数据  aa:aa  bb:bb  cc:cc 。后来我后悔了,不想插入 bb:bb,经过oplog重放过滤好 bb:bb这条数据。数据库

原理:vim

  1.经过 oplog.rs 找到后悔那条命令的操做时间(ts)。 1521094621, 1spa

  2.恢复全量备份的数据,而后恢复从全量备份最后时间戳即到:1521094621, 0 前面。debug

  3.再恢复 1521094621, 1 后面的数据。便可成功绕过 bb:bb 执行这条命令。达到过滤的目录rest

 

笔者:code

  在实现源库和目标库为同一个库的时候回复失败了。(有多是不够细心操做出错,下次有空再细究)blog

  后来源库和目标库分开恢复的时候成功了。get

 

实战备份篇:io

1.导出oplog.rs 经过关键字 找出 ts 关键字为:{"t":1521094621,"i":1}

mongodump --authenticationDatabase admin -u root -p root --port 3717 -h 127.0.0.1 -d local -c oplog.rs -o /opt/gettime/

bsondump /opt/gettime/local/oplog.rs.bson > alloplog.log

vim alloplog.log
{"ts":{"$timestamp":{"t":1521094616,"i":1}},"t":{"$numberLong":"1"},"h":{"$numberLong":"-8782812939665643170"},"v":2,"op":"i","ns":"debug.tiaos","o":{"_id":{"$oid":"5aaa0fd64a1c2a03e381218e"},"aaa":"aaa"}}
{"ts":{"$timestamp":{"t":1521094621,"i":1}},"t":{"$numberLong":"1"},"h":{"$numberLong":"5308280040949638880"},"v":2,"op":"i","ns":"debug.tiaos","o":{"_id":{"$oid":"5aaa0fdb4a1c2a03e381218f"},"bbb":"bbb"}} {"ts":{"$timestamp":{"t":1521094625,"i":1}},"t":{"$numberLong":"1"},"h":{"$numberLong":"-3595455147224912749"},"v":2,"op":"i","ns":"debug.tiaos","o":{"_id":{"$oid":"5aaa0fdf4a1c2a03e3812190"},"ccc":"ccc"}}

2.获取错误操做前的数据

笔者是用来实验环境,即直接获取误操做前的无限值:mongodump --authenticationDatabase admin -u root -p root --port 3717 -h 127.0.0.1 -d local -c oplog.rs -q '{"ts" : {$lte : Timestamp(1521094621, 0)}}' -o /opt/start/ 若是有全备份,即从全备的时间戳开始:mongodump --authenticationDatabase admin -u root -p root --port 3717 -h 127.0.0.1 -d local -c oplog.rs -q '{"ts" : {$gte : Timestamp(所有最后的时间戳, 0), $lte : Timestamp(1521094621, 0)}}' -o /opt/start/

3..获取错误操做后的数据
mongodump --authenticationDatabase admin -u root -p root --port 3717 -h 127.0.0.1 -d local -c oplog.rs -q '{ts:{$gt:Timestamp(1521094621, 1)}}' -o /opt/end/

实战恢复篇:把数据恢复到另一个新的数据库。class

1.恢复误操做前的数据

若是有所有,先把天天的全备数据恢复一下。

mongorestore -h 192.168.0.2:3717 --authenticationDatabase admin -u root -p root --oplogReplay /opt/start/

2.恢复删除后的数据

mongorestore -h 192.168.0.2:3717 --authenticationDatabase admin -u root -p root --oplogReplay /opt/end/
相关文章
相关标签/搜索