首页 > 数据库 >mongodb异机做时间点恢复(异机建议部署免密登录)

mongodb异机做时间点恢复(异机建议部署免密登录)

时间:2024-04-26 17:44:44浏览次数:25  
标签:tmp 免密 04 pushmsg mongodb db oplog 0800 异机

环境:
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

相关文章

  • MongoDB基础操作
    目录一、什么是MongoDB二、MongoDB与关系型数据库对比三、数据类型四、部署MongoDB1、下载二进制包2、下载安装包并解压3、创建用于存放数据和日志的目录,并修改权限4、启动MongoDB4.1前台启动4.2后台启动4.3、配置文件启动服务4.4、配置systemd服务4.5、systemctl启动MongoDB5、......
  • Shell 脚本:创建用户、创建同名用户组、配置密码、免密提权
    #!/usr/bin/envbashset-xeuopipefail#需要以root运行,或者sudoif[`id-u`-ne0]thenechoPleaserunthisscriptasrootorusingsudo!exitfiUserID=3001#TODO自行修改UserName=hello#TODO自行修改UserPwd=world#TODO自行......
  • scp实现免密传输
    目的:需要从A主机免密码输入复制文件或文件夹至B主机,或者将B主机的文件或文件夹免密码复制到A主机操作过程:1、在A主机上执行以下命令来生成配对密钥ssh-keygen-trsa连续按三次回车2、将A主机的.ssh目录中的id_rsa.pub文件复制到主机B的~/.ssh/目录中,并改名为au......
  • MongoDB db.collection.explain()
    db.collection.explain()封装了explain命令,推荐使用db.collection.explain()来执行explain命令。 db.collection.explain()返回以下方法的查询计划:aggregate()、count()、find()、remove()、distinct()、findAndModify() 因此,db.collection.explain()的使用方法就是......
  • mongodb 分片创建使用汇总
    5.Mongodb1.查看分片:db.runComand({listshards:1})2.查看数据存储情况:showdbs3.查看分片状态:sh.status()4.开启数据库分片配置:db.runComand({enablesharding:"testdb"})db.enableSharding("testdb")db.shardCollection("testdb.persons",{id:1})5.创建索引(如果有......
  • 6.prometheus监控--监控redis/rabbitmq/mongodb
    1.监控redis1.1安装方式1.1.1二进制源码安装方式参考nginx二进制安装方法redis_exporter下载地址:https://github.com/oliver006/redis_exporter/releases系统服务:cat>/etc/systemd/system/redis_exporter.service<<"EOF"[Unit]Description=PrometheusRedisExport......
  • 使用Docker搭建MongoDB 5.0版本副本集集群
    1、mongodb集群首先我们需要了解mongodb的集群模式,mongodb安装分为单机安装和集群安装。集群安装分为:主从复制(Master-Slaver)集群、副本集(ReplicaSet)集群和分片集群(ShardedCluster)。MongoDB的主从复制(Master-SlaveReplication)已不再是官方推荐的特性,并且在未来的版本中可能会被......
  • python监控MongoDB服务进程,故障钉钉告警
     服务器1xx.168.8x.77#!/usr/bin/python#!_*_coding:utf-8_*_importosimportsysimporttimemongo_ip='192.168.xx.77'ports=['x001','x002']defport(ip,port):  response=os.popen("tcping %s%s|grepopen|awk-F'&......
  • 在macOS上管理MongoDB:服务和手动后台进程
    MongoDB是一个功能强大的开源NoSQL数据库,因其可扩展性和性能而受到青睐。macOS用户可以将MongoDB配置为服务运行,或者手动将其作为后台进程运行。本文将详细介绍如何在macOS上使用MongoDB7.0版本进行这两种操作。将MongoDB作为macOS服务运行为了便捷性和确保MongoDB持续运行,macO......
  • postgresql数据定时转存mongodb方案
    案例背景很多事件记录在最初一段时间读写比较频繁,存储在postgresql比较合适,后期数据量变大,且仅作为历史记录查询,更适合存储在mongodb中,可能需要定期将postgresql中的数据转存到mongodb。案例分析postgresql数据定时转存mongodb,可以采用jdbc方式将postgresql读入内存,对每条......