MongoDB利用oplog恢复数据的方法

目录

  • 数据全备
  • 模拟故障
    • 写入数据
    • 模拟误操作
  • 恢复步骤
    • 备份oplog
    • 解析oplog
    • 将oplog备份和全备复制到standalone机
    • 查找误操作时间点
    • 进行数据恢复
    • 检查恢复结果
当我们对数据出现误操作的时候,可以利用oplog恢复数据。
使用前提:
  • 1、环境是副本集
  • 2、必须有全备
  • 2、全备后oplog没有被覆盖

数据全备
mongodump -h 172.16.254.133 --port 27017 -o /mongodb/backup/backup

【MongoDB利用oplog恢复数据的方法】
模拟故障
写入数据
handong1:PRIMARY> for (var i = 1; i <= 100; i++) {...db.test.insert( { id : i , name: "handong" , name1:"handong", name2:"handong", name3:"handong"} )... }WriteResult({ "nInserted" : 1 })handong1:PRIMARY> handong1:PRIMARY> db.test.count()100


模拟误操作
handong1:PRIMARY> db.test.remove({})WriteResult({ "nRemoved" : 100 })handong1:PRIMARY> db.test.count()0

所有文档被误删除。

恢复步骤
备份oplog
mongodump -h 172.16.254.133 --port 27017 -d local -c oplog.rs -o /mongodb/backup2021-04-30T18:32:29.077+0800 writing local.oplog.rs to /mongodb/backup/local/oplog.rs.bson2021-04-30T18:32:32.039+0800 local.oplog.rs71082021-04-30T18:32:35.038+0800 local.oplog.rs179122021-04-30T18:32:38.041+0800 local.oplog.rs282262021-04-30T18:32:41.039+0800 local.oplog.rs386422021-04-30T18:32:44.042+0800 local.oplog.rs506792021-04-30T18:32:47.040+0800 local.oplog.rs640012021-04-30T18:32:50.040+0800 local.oplog.rs772652021-04-30T18:32:53.038+0800 local.oplog.rs897392021-04-30T18:32:56.038+0800 local.oplog.rs1024492021-04-30T18:32:57.697+0800 local.oplog.rs1324592021-04-30T18:32:57.697+0800 done dumping local.oplog.rs (132459 documents)


解析oplog
bsondump /mongodb/backup/local/oplog.rs.bson> /mongodb/backup/local/local.log2021-04-30T18:34:27.612+0800 132459 objects found


将oplog备份和全备复制到standalone机
scp -r backup/ 172.16.254.130:/mongodb/backup/

scp -r local/ 172.16.254.130:/mongodb/backup/backup


查找误操作时间点
通过查看解析完的日志local.log发现误操作的时间点在1619778429。

进行数据恢复
mongorestore -h 172.16.254.130 --port 27017 --oplogReplay --oplogLimit 1619778429:1 /mongodb/backup/backup2021-04-30T19:00:11.099+0800 preparing collections to restore from2021-04-30T19:00:11.100+0800 don't know what to do with file "/mongodb/backup/backup/local/111.log", skipping...2021-04-30T19:00:11.100+0800 don't know what to do with file "/mongodb/backup/backup/local/local.log", skipping...2021-04-30T19:00:11.116+0800 reading metadata for db4.rcmd_1_tag_li_liao from /mongodb/backup/backup/db4/rcmd_1_tag_li_liao.metadata.json2021-04-30T19:00:11.117+0800 reading metadata for ycsb.usertable from /mongodb/backup/backup/ycsb/usertable.metadata.json2021-04-30T19:00:11.119+0800 reading metadata for db3.db3 from /mongodb/backup/backup/db3/db3.metadata.json2021-04-30T19:00:11.119+0800 reading metadata for ycsb1.usertable from /mongodb/backup/backup/ycsb1/usertable.metadata.json2021-04-30T19:00:11.170+0800 restoring ycsb.usertable from /mongodb/backup/backup/ycsb/usertable.bson2021-04-30T19:00:11.187+0800 restoring ycsb1.usertable from /mongodb/backup/backup/ycsb1/usertable.bson2021-04-30T19:00:11.391+0800 restoring db4.rcmd_1_tag_li_liao from /mongodb/backup/backup/db4/rcmd_1_tag_li_liao.bson2021-04-30T19:00:11.580+0800 restoring db3.db3 from /mongodb/backup/backup/db3/db3.bson2021-04-30T19:00:11.661+0800 no indexes to restore2021-04-30T19:00:11.661+0800 finished restoring db3.db3 (6 documents, 0 failures)2021-04-30T19:00:11.662+0800 reading metadata for db5.test from /mongodb/backup/backup/db5/test.metadata.json2021-04-30T19:00:12.545+0800 restoring db5.test from /mongodb/backup/backup/db5/test.bson2021-04-30T19:00:12.548+0800 no indexes to restore2021-04-30T19:00:12.548+0800 finished restoring db5.test (0 documents, 0 failures)2021-04-30T19:00:12.548+0800 reading metadata for db4.db4 from /mongodb/backup/backup/db4/db4.metadata.json2021-04-30T19:00:13.784+0800 no indexes to restore2021-04-30T19:00:13.784+0800 finished restoring ycsb1.usertable (30370 documents, 0 failures)2021-04-30T19:00:13.785+0800 reading metadata for db4.test1 from /mongodb/backup/backup/db4/test1.metadata.json2021-04-30T19:00:14.099+0800 [###################.....]ycsb.usertable47.8MB/58.9MB(81.2%)2021-04-30T19:00:14.099+0800 [........................]db4.rcmd_1_tag_li_liao79.8MB/3.32GB(2.3%)2021-04-30T19:00:14.099+0800 2021-04-30T19:00:14.843+0800 [########################]ycsb.usertable58.9MB/58.9MB(100.0%)2021-04-30T19:00:14.843+0800 no indexes to restore2021-04-30T19:00:14.843+0800 finished restoring ycsb.usertable (43458 documents, 0 failures)2021-04-30T19:00:15.339+0800 restoring db4.db4 from /mongodb/backup/backup/db4/db4.bson2021-04-30T19:00:17.077+0800 restoring db4.test1 from /mongodb/backup/backup/db4/test1.bson2021-04-30T19:00:17.103+0800 [#.......................]db4.rcmd_1_tag_li_liao167MB/3.32GB(4.9%)2021-04-30T19:00:17.104+0800 [#.......................]db4.db48.11MB/137MB(5.9%)2021-04-30T19:00:17.104+0800 [........................]db4.test10B/104MB(0.0%)2021-04-30T19:00:17.104+0800 2021-04-30T19:00:20.099+0800 [#.......................]db4.rcmd_1_tag_li_liao204MB/3.32GB(6.0%)2021-04-30T19:00:20.099+0800 [####....................]db4.db424.5MB/137MB(18.0%)2021-04-30T19:00:20.099+0800 [#.......................]db4.test14.47MB/104MB(4.3%)2021-04-30T19:00:20.099+0800 2021-04-30T19:00:23.099+0800 [#.......................]db4.rcmd_1_tag_li_liao272MB/3.32GB(8.0%)2021-04-30T19:00:23.099+0800 [######..................]db4.db439.7MB/137MB(29.1%)2021-04-30T19:00:23.099+0800 [####....................]db4.test120.1MB/104MB(19.3%)2021-04-30T19:00:23.099+0800 2021-04-30T19:00:26.102+0800 [##......................]db4.rcmd_1_tag_li_liao355MB/3.32GB(10.4%)2021-04-30T19:00:26.102+0800 [##########..............]db4.db458.0MB/137MB(42.5%)2021-04-30T19:00:26.102+0800 [########................]db4.test138.1MB/104MB(36.7%)2021-04-30T19:00:26.102+0800 2021-04-30T19:00:29.098+0800 [##......................]db4.rcmd_1_tag_li_liao401MB/3.32GB(11.8%)2021-04-30T19:00:29.098+0800 [############............]db4.db473.1MB/137MB(53.5%)2021-04-30T19:00:29.098+0800 [###########.............]db4.test151.8MB/104MB(49.8%)2021-04-30T19:00:29.098+0800 2021-04-30T19:00:32.097+0800 [###.....................]db4.rcmd_1_tag_li_liao494MB/3.32GB(14.5%)2021-04-30T19:00:32.097+0800 [###############.........]db4.db490.8MB/137MB(66.5%)2021-04-30T19:00:32.097+0800 [###############.........]db4.test167.3MB/104MB(64.7%)2021-04-30T19:00:32.097+0800 2021-04-30T19:00:35.100+0800 [###.....................]db4.rcmd_1_tag_li_liao556MB/3.32GB(16.3%)2021-04-30T19:00:35.100+0800 [###################.....]db4.db4110MB/137MB(80.5%)2021-04-30T19:00:35.100+0800 [###################.....]db4.test186.1MB/104MB(82.8%)2021-04-30T19:00:35.100+0800 2021-04-30T19:00:38.097+0800 [####....................]db4.rcmd_1_tag_li_liao620MB/3.32GB(18.2%)2021-04-30T19:00:38.097+0800 [#####################...]db4.db4124MB/137MB(91.1%)2021-04-30T19:00:38.097+0800 [#######################.]db4.test1101MB/104MB(96.7%)2021-04-30T19:00:38.097+0800 2021-04-30T19:00:39.023+0800 [########################]db4.test1104MB/104MB(100.0%)2021-04-30T19:00:39.023+0800 no indexes to restore2021-04-30T19:00:39.023+0800 finished restoring db4.test1 (1000000 documents, 0 failures)2021-04-30T19:00:40.386+0800 [########################]db4.db4137MB/137MB(100.0%)2021-04-30T19:00:40.386+0800 no indexes to restore2021-04-30T19:00:40.386+0800 finished restoring db4.db4 (1313657 documents, 0 failures)2021-04-30T19:00:41.097+0800 [####....................]db4.rcmd_1_tag_li_liao684MB/3.32GB(20.1%)2021-04-30T19:00:44.097+0800 [#####...................]db4.rcmd_1_tag_li_liao760MB/3.32GB(22.3%)2021-04-30T19:00:47.097+0800 [#####...................]db4.rcmd_1_tag_li_liao836MB/3.32GB(24.6%)2021-04-30T19:00:50.098+0800 [######..................]db4.rcmd_1_tag_li_liao906MB/3.32GB(26.6%)2021-04-30T19:00:53.098+0800 [#######.................]db4.rcmd_1_tag_li_liao994MB/3.32GB(29.2%)2021-04-30T19:00:56.098+0800 [#######.................]db4.rcmd_1_tag_li_liao1.03GB/3.32GB(31.0%)2021-04-30T19:00:59.098+0800 [########................]db4.rcmd_1_tag_li_liao1.11GB/3.32GB(33.3%)2021-04-30T19:01:02.097+0800 [########................]db4.rcmd_1_tag_li_liao1.18GB/3.32GB(35.5%)2021-04-30T19:01:05.101+0800 [#########...............]db4.rcmd_1_tag_li_liao1.26GB/3.32GB(38.0%)2021-04-30T19:01:08.097+0800 [#########...............]db4.rcmd_1_tag_li_liao1.32GB/3.32GB(39.7%)2021-04-30T19:01:11.100+0800 [#########...............]db4.rcmd_1_tag_li_liao1.37GB/3.32GB(41.1%)2021-04-30T19:01:14.098+0800 [##########..............]db4.rcmd_1_tag_li_liao1.43GB/3.32GB(43.2%)2021-04-30T19:01:17.097+0800 [##########..............]db4.rcmd_1_tag_li_liao1.50GB/3.32GB(45.0%)2021-04-30T19:01:20.098+0800 [###########.............]db4.rcmd_1_tag_li_liao1.54GB/3.32GB(46.3%)2021-04-30T19:01:23.098+0800 [###########.............]db4.rcmd_1_tag_li_liao1.58GB/3.32GB(47.6%)2021-04-30T19:01:26.098+0800 [###########.............]db4.rcmd_1_tag_li_liao1.64GB/3.32GB(49.3%)2021-04-30T19:01:29.097+0800 [############............]db4.rcmd_1_tag_li_liao1.71GB/3.32GB(51.4%)2021-04-30T19:01:32.097+0800 [############............]db4.rcmd_1_tag_li_liao1.77GB/3.32GB(53.2%)2021-04-30T19:01:35.098+0800 [#############...........]db4.rcmd_1_tag_li_liao1.85GB/3.32GB(55.7%)2021-04-30T19:01:38.097+0800 [#############...........]db4.rcmd_1_tag_li_liao1.90GB/3.32GB(57.2%)2021-04-30T19:01:41.097+0800 [##############..........]db4.rcmd_1_tag_li_liao1.98GB/3.32GB(59.5%)2021-04-30T19:01:44.827+0800 [##############..........]db4.rcmd_1_tag_li_liao2.00GB/3.32GB(60.3%)2021-04-30T19:01:47.097+0800 [##############..........]db4.rcmd_1_tag_li_liao2.05GB/3.32GB(61.8%)2021-04-30T19:01:50.098+0800 [###############.........]db4.rcmd_1_tag_li_liao2.12GB/3.32GB(63.9%)2021-04-30T19:01:53.097+0800 [###############.........]db4.rcmd_1_tag_li_liao2.19GB/3.32GB(65.9%)2021-04-30T19:01:56.097+0800 [################........]db4.rcmd_1_tag_li_liao2.26GB/3.32GB(67.9%)2021-04-30T19:01:59.099+0800 [################........]db4.rcmd_1_tag_li_liao2.32GB/3.32GB(69.8%)2021-04-30T19:02:02.098+0800 [#################.......]db4.rcmd_1_tag_li_liao2.39GB/3.32GB(72.0%)2021-04-30T19:02:05.097+0800 [#################.......]db4.rcmd_1_tag_li_liao2.47GB/3.32GB(74.4%)2021-04-30T19:02:08.097+0800 [##################......]db4.rcmd_1_tag_li_liao2.52GB/3.32GB(76.0%)2021-04-30T19:02:11.097+0800 [##################......]db4.rcmd_1_tag_li_liao2.59GB/3.32GB(77.8%)2021-04-30T19:02:14.097+0800 [###################.....]db4.rcmd_1_tag_li_liao2.66GB/3.32GB(80.0%)2021-04-30T19:02:17.097+0800 [###################.....]db4.rcmd_1_tag_li_liao2.72GB/3.32GB(81.9%)2021-04-30T19:02:20.097+0800 [####################....]db4.rcmd_1_tag_li_liao2.78GB/3.32GB(83.7%)2021-04-30T19:02:23.097+0800 [####################....]db4.rcmd_1_tag_li_liao2.85GB/3.32GB(85.7%)2021-04-30T19:02:26.098+0800 [#####################...]db4.rcmd_1_tag_li_liao2.94GB/3.32GB(88.4%)2021-04-30T19:02:29.097+0800 [#####################...]db4.rcmd_1_tag_li_liao3.00GB/3.32GB(90.4%)2021-04-30T19:02:32.097+0800 [######################..]db4.rcmd_1_tag_li_liao3.06GB/3.32GB(92.1%)2021-04-30T19:02:35.099+0800 [######################..]db4.rcmd_1_tag_li_liao3.12GB/3.32GB(93.9%)2021-04-30T19:02:38.097+0800 [######################..]db4.rcmd_1_tag_li_liao3.15GB/3.32GB(95.0%)2021-04-30T19:02:41.098+0800 [#######################.]db4.rcmd_1_tag_li_liao3.21GB/3.32GB(96.7%)2021-04-30T19:02:44.167+0800 [#######################.]db4.rcmd_1_tag_li_liao3.26GB/3.32GB(98.0%)2021-04-30T19:02:47.097+0800 [#######################.]db4.rcmd_1_tag_li_liao3.32GB/3.32GB(99.9%)2021-04-30T19:02:47.392+0800 [########################]db4.rcmd_1_tag_li_liao3.32GB/3.32GB(100.0%)2021-04-30T19:02:47.393+0800 no indexes to restore2021-04-30T19:02:47.393+0800 finished restoring db4.rcmd_1_tag_li_liao (379143 documents, 0 failures)2021-04-30T19:02:47.393+0800 restoring users from /mongodb/backup/backup/admin/system.users.bson2021-04-30T19:02:50.655+0800 admin.tempusers2.05KB2021-04-30T19:02:50.655+0800 admin.tempusers2.05KB2021-04-30T19:02:51.905+0800 replaying oplog2021-04-30T19:02:53.097+0800 oplog483KB2021-04-30T19:02:56.097+0800 oplog20.2MB2021-04-30T19:02:59.100+0800 oplog36.9MB2021-04-30T19:03:02.097+0800 oplog50.1MB2021-04-30T19:03:05.098+0800 oplog69.0MB2021-04-30T19:03:08.097+0800 oplog90.6MB2021-04-30T19:03:11.097+0800 oplog124MB2021-04-30T19:03:14.097+0800 oplog159MB2021-04-30T19:03:17.098+0800 oplog185MB2021-04-30T19:03:20.097+0800 oplog219MB2021-04-30T19:03:23.098+0800 oplog256MB2021-04-30T19:03:26.097+0800 oplog290MB2021-04-30T19:03:29.097+0800 oplog323MB2021-04-30T19:03:32.097+0800 oplog357MB2021-04-30T19:03:35.097+0800 oplog391MB2021-04-30T19:03:38.098+0800 oplog427MB2021-04-30T19:03:41.098+0800 oplog464MB2021-04-30T19:03:44.097+0800 oplog501MB2021-04-30T19:03:47.097+0800 oplog544MB2021-04-30T19:03:50.098+0800 oplog580MB2021-04-30T19:03:53.098+0800 oplog619MB2021-04-30T19:03:56.098+0800 oplog650MB2021-04-30T19:03:59.099+0800 oplog687MB2021-04-30T19:04:02.097+0800 oplog722MB2021-04-30T19:04:05.097+0800 oplog758MB2021-04-30T19:04:08.098+0800 oplog795MB2021-04-30T19:04:11.097+0800 oplog826MB2021-04-30T19:04:14.098+0800 oplog858MB2021-04-30T19:04:17.102+0800 oplog893MB2021-04-30T19:04:20.097+0800 oplog929MB2021-04-30T19:04:23.098+0800 oplog968MB2021-04-30T19:04:26.098+0800 oplog1001MB2021-04-30T19:04:29.097+0800 oplog1002MB2021-04-30T19:04:32.097+0800 oplog1003MB2021-04-30T19:04:35.097+0800 oplog1004MB2021-04-30T19:04:38.097+0800 oplog1004MB2021-04-30T19:04:38.597+0800 applied 130029 oplog entries2021-04-30T19:04:38.597+0800 oplog1005MB2021-04-30T19:04:38.614+0800 2766634 document(s) restored successfully. 0 document(s) failed to restore.


检查恢复结果
> show dbsadmin0.000GBconfig0.000GBdb30.000GBdb43.355GBdb50.000GBlocal0.000GBycsb0.060GBycsb10.041GB> > > use db5switched to db db5> > db.test.count()100

可以看到我们前期插入的100记录以及恢复成功。
到此这篇关于MongoDB利用oplog恢复数据介绍的文章就介绍到这了,更多相关oplog数据恢复内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读