mongodb使用备份后的oplog做时间点恢复
环境:
OS:Centos 7
DB:mongodb 5.0
1.数据库备份
/opt/mongodb-database-tools/bin/mongodump -h 192.168.56.101:29001 -u test -p test123 --authenticationDatabase admin --db=db_pushmsg -o /tmp/bak
记录下备份的完成时间:
2024-04-28T14:39:24.628+0800 done dumping db_pushmsg.user (847999 documents)
2024-04-28T14:39:24转换成UTC时间:1714286364
该时间点用于导出oplog的开始时间点
2.模拟删除表
myrepl:PRIMARY> db.user.drop();
3.导出备份完成时候之后的oplog
mkdir /tmp/oplog
/opt/mongodb-database-tools/bin/mongodump -h 192.168.56.101:29001 -u test -p test123 --authenticationDatabase admin --db=local -c oplog.rs --query '{"ts":{"$gt": {"$timestamp":{"t":1714286364, "i":1}}}}' -o /tmp/oplog
解析:
/opt/mongodb-database-tools/bin/bsondump /tmp/oplog/local/oplog.rs.bson>/tmp/oplog.txt
找到删除表的时间点
[root@dsc1 tmp]# cat /tmp/oplog.txt|grep drop
{"op":"c","ns":"db_pushmsg.$cmd","ui":{"$binary":{"base64":"rco6crFcRn+aE24ikQd51w==","subType":"04"}},"o":{"drop":"user"},"o2":{"numRecords":{"$numberInt":"1000000"}},"ts":{"$timestamp":{"t":1714286707,"i":1}},"t":{"$numberLong":"3"},"v":{"$numberLong":"2"},"wall":{"$date":{"$numberLong":"1714286707948"}}}
删除的时间点为:1714286707
4.备份文件和oplog备份拷贝到异机
[root@dsc1 tmp]# scp -r bak root@192.168.56.103:/tmp/
[root@dsc1 tmp]# scp -r oplog root@192.168.56.103:/tmp/
5.异机恢复数据库
/opt/mongodb-database-tools/bin/mongorestore -h 192.168.56.103:27001 -u test -p test123 --authenticationDatabase admin -d db_pushmsg /tmp/bak/db_pushmsg
6.日志应用
/opt/mongodb-database-tools/bin/mongorestore -h 192.168.56.103:27001 -u test -p test123 --authenticationDatabase admin --oplogReplay --oplogLimit "1714286707:1" /tmp/oplog/local/oplog.rs.bson