mongo2.4_point_in_time_recovery

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: --配置复制集/opt/mongodb/2.4/bin/mongod --replSet=rs1 --journal --port 27018 --dbpath=/data/mongodb/27018 --logpath=/data/mongodb/log/27018/mongo.
--配置复制集
/opt/mongodb/2.4/bin/mongod --replSet=rs1 --journal --port 27018 --dbpath=/data/mongodb/27018 --logpath=/data/mongodb/log/27018/mongo.log --fork --logappend
/opt/mongodb/2.4/bin/mongod --replSet=rs1 --journal --port 27019 --dbpath=/data/mongodb/27019 --logpath=/data/mongodb/log/27019/mongo.log --fork --logappend
/opt/mongodb/2.4/bin/mongod --replSet=rs1 --journal --port 27020 --dbpath=/data/mongodb/27020 --logpath=/data/mongodb/log/27020/mongo.log --fork --logappend
--初始化复制集,连接一台mongo
use admin
db.runCommand({"replSetInitiate":{
"_id":"rs1",
"members":[{
"_id":1,
"host":"192.168.250.17:27018",
"priority":3
},
{
"_id":2,
"host":"192.168.250.17:27019",
"priority":2
},
{
"_id":3,
"host":"192.168.250.17:27020",
"arbiterOnly":true
}
]}})

--同样可执行如下语句
rs.initiate()


--在主上插入测试数据
 for (var i=0;i<100;i++) {db.userinfo.insert({"id":i,"name":"rudy"+i,password:i+"password"}); }

--在从上执行数据库备份
 mongodump --port 27019 -o /tmp/backup/dump`date +%Y%m%d` --host=192.168.250.17

--继续在主上执行数据
 for (var i=100;i<200;i++) {db.userinfo.insert({"id":i,"name":"rudy"+i,password:i+"password"}); }

 --强制primary和standard节点角色互换
rs.stepDown() 

--在新主上做数据变更
 for (var i=200;i<300;i++) {db.userinfo.insert({"id":i,"name":"rudy"+i,password:i+"password"}); }
 db.userinfo.update({},{$inc:{id:10000},$set:{password:"123456"}},{multi:true});

 --现在需要恢复到第二次插入数据之后时间点

 --查询在指定时间段内的操作的时间点,注意时区
db.oplog.rs.find({
    "ts": {
        $gt: Timestamp(new Date("2016-03-01T03:20:00Z").getTime() / 1000, 1),
        $lt: Timestamp(new Date("2016-03-01T03:31:00Z").getTime() / 1000, 1)
    },
    "op": "i",
    "ns": "test.userinfo",
    "o.id": {$lt:201}
}).sort({
    $natural: -1
}).limit(3)

{ "ts" : Timestamp(1456802915, 1), "h" : NumberLong("2333357293330368223"), "v" : 2, "op" : "i", "ns" : "test.userinfo", "o" : { "_id" : ObjectId("56d50c6306abfb157a0a2f36"), "id" : 200, "name" : "rudy200", "password" : "200password" } }
{ "ts" : Timestamp(1456802551, 100), "h" : NumberLong("1328300502301838340"), "v" : 2, "op" : "i", "ns" : "test.userinfo", "o" : { "_id" : ObjectId("56d50af7a7edfd6396d2f679"), "id" : 199, "name" : "rudy199", "password" : "199password" } }
{ "ts" : Timestamp(1456802551, 99), "h" : NumberLong("-6657180556193796443"), "v" : 2, "op" : "i", "ns" : "test.userinfo", "o" : { "_id" : ObjectId("56d50af7a7edfd6396d2f678"), "id" : 198, "name" : "rudy198", "password" : "198password" } }

--备份指定时间点之前的oplog操作
mongodump -h 192.168.250.17 --port 27018 -d local -c "oplog.rs" -q '{ts:{$lt:Timestamp(1456802915, 1)}}' -o /tmp/backup/oplog

--启动一个实例进行数据恢复
/opt/mongodb/2.4/bin/mongod --journal --port 27021 --dbpath=/data/mongodb/27021 --logpath=/data/mongodb/log/27021/mongo.log --fork --logappend
/opt/mongodb/2.4/bin/mongorestore -dir /tmp/backup/dump`date +%Y%m%d` --host 192.168.250.17 --port 27021
--可以发现已经恢复了前100条数据
[root@localhost local]# mongo --port 27021
MongoDB shell version: 2.4.10
connecting to: 127.0.0.1:27021/test
> db.userinfo.count()
100
> db.userinfo.find()
{ "_id" : ObjectId("56d50a2ca7edfd6396d2f5b2"), "id" : 0, "name" : "rudy0", "password" : "0password" }
{ "_id" : ObjectId("56d50a2ca7edfd6396d2f5b3"), "id" : 1, "name" : "rudy1", "password" : "1password" }
{ "_id" : ObjectId("56d50a2ca7edfd6396d2f5b4"), "id" : 2, "name" : "rudy2", "password" : "2password" }
{ "_id" : ObjectId("56d50a2ca7edfd6396d2f5b5"), "id" : 3, "name" : "rudy3", "password" : "3password" }

--把oplog恢复到一个实例中,注意此处为了方便直接恢复到实例27021了,但在生产环境中不建议,其会与实例27021的相混
/opt/mongodb/2.4/bin/mongorestore  --host 192.168.250.17 --port 27021 -dir /tmp/backup/oplog/local/
--实用oplog到恢复数据库27021
mongooplog -h 192.168.250.17 --port 27021  --from 192.168.250.17:27021 --oplogns "local.oplog.rs"
--至此可发现数据恢复已完成
[root@localhost local]# mongo --port 27021
> db.userinfo.count()
200

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
2月前
|
负载均衡 Java 应用服务中间件
Client not connected, current status:STARTING
Client not connected, current status:STARTING
80 1
|
11月前
|
Oracle 关系型数据库 数据库
change backup ... for db_unique_name不同步到control file
change backup … for db_unique_name 可以改变备份集所属的db_unique_name,但oracle官方文档里面没有说会不会同步到db_unique_name对应的数据库的control file。我自己测试发现不会同步到control file。
|
关系型数据库 MySQL
Mysql current_time,current_date()与now()区别
Mysql current_time,current_date()与now()区别
144 0
|
关系型数据库 数据库 PostgreSQL
pg_ctl: server did not start in time
postgresql 启动超时异常,可怎么解决
pg_ctl: server did not start in time
|
关系型数据库 分布式数据库 PolarDB
Remote Recovery for data guarding
PolarDB-for-PostgreSQL特性文档
|
SQL 关系型数据库 PostgreSQL
Enable point-in-time-recovery in PostgreSQL
转载地址:https://dev.to/pythonmeister/enable-point-in-time-recovery-in-postgresql-4d19 I really love PostgreSQL.
1129 0