--配置复制集 /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