环境:
OS:Centos 7
DB:mongodb 5
原环境:1主2从,异机恢复的环境:单机(oplog.rs是否开启都可以,开启可以参考:https://www.cnblogs.com/hxlasky/p/17979922)
1.查看当前的表数据情况
myrepl:PRIMARY> use db_pushmsg; switched to db db_pushmsg myrepl:PRIMARY> show tables; app_message_all tb_test myrepl:PRIMARY> db.app_message_all.find().count() 100000
2.备份
在主库上进行备份,备份整个库
[root@dsc1 bin]# /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-26T15:38:47.244+0800 writing db_pushmsg.tb_test to /tmp/bak/db_pushmsg/tb_test.bson 2024-04-26T15:38:47.247+0800 writing db_pushmsg.app_message_all to /tmp/bak/db_pushmsg/app_message_all.bson 2024-04-26T15:38:47.249+0800 done dumping db_pushmsg.tb_test (1 document) 2024-04-26T15:38:47.843+0800 done dumping db_pushmsg.app_message_all (100000 documents)
3.模拟删除表
myrepl:PRIMARY> db.app_message_all.drop()
true
4.导出oplog.rs
用于异机数据恢复
[root@dsc1 bin]# mkdir /tmp/oplog [root@dsc1 bin]# /opt/mongodb-database-tools/bin/mongodump -h 192.168.56.101:29001 -u test -p test123 --authenticationDatabase admin --db=local -c oplog.rs -o /tmp/oplog 2024-04-26T15:43:39.241+0800 writing local.oplog.rs to /tmp/oplog/local/oplog.rs.bson 2024-04-26T15:43:39.889+0800 done dumping local.oplog.rs (101776 documents)
该导出会导出整个oplog.rs日志,不单是需要恢复的表app_message_all的日志而已,恢复的时候也会把其他的表一起恢复的.
5.找到需要恢复的时间点
将oplog.rs日志导出文件
/opt/mongodb-database-tools/bin/bsondump /tmp/oplog/local/oplog.rs.bson>/tmp/oplog.txt
从文件 /tmp/oplog.txt 查找删除表前的记录
{"op":"n","ns":"","o":{"msg":"periodic noop"},"ts":{"$timestamp":{"t":1714117210,"i":1}},"t":{"$numberLong":"2"},"v":{"$numberLong":"2"},"wall":{"$date":{"$numberLong":"1714117210904"}}} {"op":"c","ns":"db_pushmsg.$cmd","ui":{"$binary":{"base64":"AC6b29lJS6m2yN7KavT6jA==","subType":"04"}},"o":{"drop":"app_message_all"},"o2":{"numRecords":{"$numberInt":"100000"}},"ts":{"$timestamp":{"t":1714117217,"i":1}},"t":{"$numberLong":"2"},"v":{"$numberLong":"2"},"wall":{"$date":{"$numberLong":"1714117217783"}}}
我们这里需要恢复删除之前的时间点:1714117210
6.把备份文件和oplog文件拷贝到需要恢复的服务器
[root@dsc1 tmp]# tar -cvf bak.tar ./bak
[root@dsc1 tmp]# tar -cvf oplog.tar ./oplog
[root@dsc1 tmp]#scp bak.tar [email protected]:/tmp/
[root@dsc1 tmp]#scp oplog.tar [email protected]:/tmp/
7.异机解压备份文件
[root@localhost tmp]# cd /tmp
[root@localhost tmp]# tar -xvf bak.tar
[root@localhost tmp]# tar -xvf oplog.tar
8.恢复
异机当前是没有需要恢复的库的
/opt/mongodb/bin/mongo 192.168.56.103:27001 > use admin switched to db admin > db.auth("test","test123"); 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB
恢复数据库
[root@localhost ~]# /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 2024-04-26T16:23:18.690+0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION} 2024-04-26T16:23:18.690+0800 building a list of collections to restore from /tmp/bak/db_pushmsg dir 2024-04-26T16:23:18.690+0800 reading metadata for db_pushmsg.app_message_all from /tmp/bak/db_pushmsg/app_message_all.metadata.json 2024-04-26T16:23:18.690+0800 reading metadata for db_pushmsg.tb_test from /tmp/bak/db_pushmsg/tb_test.metadata.json 2024-04-26T16:23:18.713+0800 restoring db_pushmsg.app_message_all from /tmp/bak/db_pushmsg/app_message_all.bson 2024-04-26T16:23:18.797+0800 restoring db_pushmsg.tb_test from /tmp/bak/db_pushmsg/tb_test.bson 2024-04-26T16:23:18.810+0800 finished restoring db_pushmsg.tb_test (1 document, 0 failures) 2024-04-26T16:23:20.416+0800 finished restoring db_pushmsg.app_message_all (100000 documents, 0 failures) 2024-04-26T16:23:20.416+0800 no indexes to restore for collection db_pushmsg.app_message_all 2024-04-26T16:23:20.416+0800 no indexes to restore for collection db_pushmsg.tb_test 2024-04-26T16:23:20.416+0800 100001 document(s) restored successfully. 0 document(s) failed to restore.
8.恢复oplog到时间戳(建议异机免密登录)
/opt/mongodb-database-tools/bin/mongorestore -h 192.168.56.103:27001 -u test -p test123 --authenticationDatabase admin --oplogReplay --oplogLimit "1714117210:1" /tmp/oplog/local/oplog.rs.bson
这里恢复需要指定具体的文件名:/tmp/oplog/local/oplog.rs.bson,而不是目录
报错误:
2024-04-26T16:55:03.941+0800 Failed: restore error: error applying oplog: applyOps: (Unauthorized) command applyOps requires authentication
但第二次执行的时候就正常了,应该是我这里的oplog有其他的操作.
标签:tmp,免密,04,pushmsg,mongodb,db,oplog,0800,异机 From: https://www.cnblogs.com/hxlasky/p/18160571