一、場景描述
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比蕭縣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式蕭縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋蕭縣地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。公司某工程師執(zhí)行db.giveget_card.drop(),誤將線上表刪除。
幸好每天都有做備份,這個時候就體現(xiàn)了備份的重要性了,哈哈哈。。。
二、模擬故障過程
備份數(shù)據(jù)大?。?/p>rs_test01:PRIMARY> use ycsb switched to db ycsb rs_test01:PRIMARY> db.giveget_card.count(); 3173391
刪除之前,此表有更新。
rs_test01:PRIMARY> db.giveget_card.insert({id:1}); WriteResult({ "nInserted" : 1 }) rs_test01:PRIMARY> db.giveget_card.insert({id:2}); WriteResult({ "nInserted" : 1 }) rs_test01:PRIMARY> db.giveget_card.insert({id:3}); WriteResult({ "nInserted" : 1 }) rs_test01:PRIMARY> db.giveget_card.insert({id:4}); WriteResult({ "nInserted" : 1 })其他表也有更新操作。
rs_test01:PRIMARY> db.tab.find(); { "_id" : ObjectId("59354ba202d9a99ab2f879c6"), "name" : "a" } { "_id" : ObjectId("59354ba602d9a99ab2f879c7"), "name" : "b" } { "_id" : ObjectId("59354ba802d9a99ab2f879c8"), "name" : "c" } { "_id" : ObjectId("59354baa02d9a99ab2f879c9"), "name" : "d" }刪除操作之后,此表和其他表都有更新。
rs_test01:PRIMARY> db.giveget_card.find(); { "_id" : ObjectId("59354c28d905432aeaccd53c"), "id" : 5 } { "_id" : ObjectId("59354c2bd905432aeaccd53d"), "id" : 6 } rs_test01:PRIMARY> db.tab.find(); { "_id" : ObjectId("59354ba202d9a99ab2f879c6"), "name" : "a" } { "_id" : ObjectId("59354ba602d9a99ab2f879c7"), "name" : "b" } { "_id" : ObjectId("59354ba802d9a99ab2f879c8"), "name" : "c" } { "_id" : ObjectId("59354baa02d9a99ab2f879c9"), "name" : "d" } { "_id" : ObjectId("59354ccfd905432aeaccd542"), "name" : "e" } { "_id" : ObjectId("59354cd2d905432aeaccd543"), "name" : "f" }三、恢復(fù)步驟
1、將備份中 tab 表的 giveget_card.bson 及 giveget_card.metadata.json 文件拷貝到 /tmp/restore/ycsb 目錄(自建目錄),ycsb 為庫名。
# cp /data/backup/rs07/ycsb/giveget_card.* /tmp/restore/ycsb2、將備份時間之后,誤刪操作之前的 oplog 導(dǎo)出,用于恢復(fù)表
# mongodump --port 2203 -d local -c oplog.rs -q '{"ts" : {$gte : Timestamp(1496664480, 10430), $lte : Timestamp(1496665113, 10430)}}' -o /tmp/oplog--時間戳 是使用轉(zhuǎn)換工具轉(zhuǎn)換之后的結(jié)果。
3、使用 bsondump 查看 oplog 日志,找到 drop 操作的時間戳 1496665069
# bsondump /tmp/oplog/local/oplog.rs.bson {"ts":{"$timestamp":{"t":1496664760,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"7079172056815894727"},"v":2,"op":"i","ns":"ycsb.giveget_card","o":{"_id":{"$oid":"59354ab8c5308d8c7a9da8b5"},"id":1.0}} {"ts":{"$timestamp":{"t":1496664762,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"-1797107728294067016"},"v":2,"op":"i","ns":"ycsb.giveget_card","o":{"_id":{"$oid":"59354abac5308d8c7a9da8b6"},"id":2.0}} {"ts":{"$timestamp":{"t":1496664765,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"8604646791509150392"},"v":2,"op":"i","ns":"ycsb.giveget_card","o":{"_id":{"$oid":"59354abdc5308d8c7a9da8b7"},"id":3.0}} {"ts":{"$timestamp":{"t":1496664768,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"9018614066505371436"},"v":2,"op":"i","ns":"ycsb.giveget_card","o":{"_id":{"$oid":"59354ac0c5308d8c7a9da8b8"},"id":4.0}} {"ts":{"$timestamp":{"t":1496664994,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"-4471524661347063602"},"v":2,"op":"c","ns":"ycsb.$cmd","o":{"create":"tab"}} {"ts":{"$timestamp":{"t":1496664994,"i":2}},"t":{"$numberLong":"12"},"h":{"$numberLong":"-4215905958456607246"},"v":2,"op":"i","ns":"ycsb.tab","o":{"_id":{"$oid":"59354ba202d9a99ab2f879c6"},"name":"a"}} {"ts":{"$timestamp":{"t":1496664998,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"6170506962401844481"},"v":2,"op":"i","ns":"ycsb.tab","o":{"_id":{"$oid":"59354ba602d9a99ab2f879c7"},"name":"b"}} {"ts":{"$timestamp":{"t":1496665000,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"-8071456063660489895"},"v":2,"op":"i","ns":"ycsb.tab","o":{"_id":{"$oid":"59354ba802d9a99ab2f879c8"},"name":"c"}} {"ts":{"$timestamp":{"t":1496665002,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"4387884836668659146"},"v":2,"op":"i","ns":"ycsb.tab","o":{"_id":{"$oid":"59354baa02d9a99ab2f879c9"},"name":"d"}} {"ts":{"$timestamp":{"t":1496665069,"i":1}},"t":{"$numberLong":"12"},"h":{"$numberLong":"-6913449254950935781"},"v":2,"op":"c","ns":"ycsb.$cmd","o":{"drop":"giveget_card"}} 2017-06-05T20:27:25.552+0800 10 objects found4、將 oplog 的 bson 文件拷貝到相應(yīng)目錄下
# cp /tmp/oplog/local/oplog.rs.bson /tmp/restore/oplog.bson此時恢復(fù)的目錄結(jié)構(gòu):
# pwd /tmp/restore # ls oplog.bson ycsb5、至此,所有的準(zhǔn)備操作已經(jīng)做完,恢復(fù)數(shù)據(jù)。
[root@ops-db-test02 restore]# mongorestore --port 2203 --oplogReplay --oplogLimit=1496665069:1 /tmp/restore 2017-06-05T20:36:45.361+0800 building a list of dbs and collections to restore from /tmp/restore dir 2017-06-05T20:36:45.364+0800 reading metadata for ycsb.giveget_card from /tmp/restore/ycsb/giveget_card.metadata.json 2017-06-05T20:36:45.364+0800 restoring ycsb.giveget_card from /tmp/restore/ycsb/giveget_card.bson 2017-06-05T20:36:48.362+0800 [........................] ycsb.giveget_card 15.4MB/475MB (3.2%) 2017-06-05T20:36:51.362+0800 [#.......................] ycsb.giveget_card 31.1MB/475MB (6.6%) 2017-06-05T20:36:54.362+0800 [##......................] ycsb.giveget_card 46.6MB/475MB (9.8%) 2017-06-05T20:36:57.362+0800 [###.....................] ycsb.giveget_card 62.1MB/475MB (13.1%) 2017-06-05T20:37:00.362+0800 [###.....................] ycsb.giveget_card 76.4MB/475MB (16.1%) 2017-06-05T20:37:03.362+0800 [####....................] ycsb.giveget_card 90.7MB/475MB (19.1%) 2017-06-05T20:37:06.362+0800 [#####...................] ycsb.giveget_card 105MB/475MB (22.0%) 2017-06-05T20:37:09.362+0800 [######..................] ycsb.giveget_card 120MB/475MB (25.2%) 2017-06-05T20:37:12.362+0800 [######..................] ycsb.giveget_card 133MB/475MB (28.0%) 2017-06-05T20:37:15.362+0800 [#######.................] ycsb.giveget_card 146MB/475MB (30.8%) 2017-06-05T20:37:18.363+0800 [########................] ycsb.giveget_card 163MB/475MB (34.3%) 2017-06-05T20:37:21.362+0800 [########................] ycsb.giveget_card 178MB/475MB (37.4%) 2017-06-05T20:37:24.362+0800 [#########...............] ycsb.giveget_card 196MB/475MB (41.3%) 2017-06-05T20:37:27.362+0800 [##########..............] ycsb.giveget_card 214MB/475MB (45.0%) 2017-06-05T20:37:30.362+0800 [###########.............] ycsb.giveget_card 231MB/475MB (48.6%) 2017-06-05T20:37:33.362+0800 [############............] ycsb.giveget_card 245MB/475MB (51.5%) 2017-06-05T20:37:36.362+0800 [#############...........] ycsb.giveget_card 261MB/475MB (54.8%) 2017-06-05T20:37:39.362+0800 [##############..........] ycsb.giveget_card 279MB/475MB (58.7%) 2017-06-05T20:37:42.362+0800 [###############.........] ycsb.giveget_card 297MB/475MB (62.5%) 2017-06-05T20:37:45.362+0800 [###############.........] ycsb.giveget_card 312MB/475MB (65.8%) 2017-06-05T20:37:48.362+0800 [################........] ycsb.giveget_card 328MB/475MB (69.0%) 2017-06-05T20:37:51.362+0800 [#################.......] ycsb.giveget_card 341MB/475MB (71.8%) 2017-06-05T20:37:54.362+0800 [#################.......] ycsb.giveget_card 356MB/475MB (74.9%) 2017-06-05T20:37:57.362+0800 [##################......] ycsb.giveget_card 373MB/475MB (78.5%) 2017-06-05T20:38:00.362+0800 [###################.....] ycsb.giveget_card 388MB/475MB (81.7%) 2017-06-05T20:38:03.362+0800 [####################....] ycsb.giveget_card 405MB/475MB (85.2%) 2017-06-05T20:38:06.362+0800 [#####################...] ycsb.giveget_card 419MB/475MB (88.2%) 2017-06-05T20:38:09.362+0800 [#####################...] ycsb.giveget_card 434MB/475MB (91.4%) 2017-06-05T20:38:12.362+0800 [######################..] ycsb.giveget_card 442MB/475MB (93.1%) 2017-06-05T20:38:15.362+0800 [#######################.] ycsb.giveget_card 459MB/475MB (96.6%) 2017-06-05T20:38:18.362+0800 [#######################.] ycsb.giveget_card 475MB/475MB (99.9%) 2017-06-05T20:38:18.427+0800 [########################] ycsb.giveget_card 475MB/475MB (100.0%) 2017-06-05T20:38:18.427+0800 restoring indexes for collection ycsb.giveget_card from metadata 2017-06-05T20:38:44.680+0800 finished restoring ycsb.giveget_card (3173391 documents) 2017-06-05T20:38:44.680+0800 replaying oplog 2017-06-05T20:38:44.739+0800 done6、查看恢復(fù)的結(jié)果
rs_test01:PRIMARY> db.giveget_card.find({id : {$gte : 1 }}); { "_id" : ObjectId("59354cb9d905432aeaccd540"), "id" : 5 } { "_id" : ObjectId("59354cc0d905432aeaccd541"), "id" : 6 } { "_id" : ObjectId("59354ab8c5308d8c7a9da8b5"), "id" : 1 } { "_id" : ObjectId("59354abac5308d8c7a9da8b6"), "id" : 2 } { "_id" : ObjectId("59354abdc5308d8c7a9da8b7"), "id" : 3 } { "_id" : ObjectId("59354ac0c5308d8c7a9da8b8"), "id" : 4 }數(shù)據(jù)內(nèi)容相同,但存儲順序與之前數(shù)據(jù)的存儲順序不同了。
rs_test01:PRIMARY> db.giveget_card.count(); 3173397結(jié)果 count= 備份表數(shù)據(jù) 3173391+ 之后的更新數(shù)據(jù) 6 。
7、因為 dump 出來的 oplog 也包含了其他表的操作。查看恢復(fù)過程中有沒有對其他表產(chǎn)生影響。
rs_test01:PRIMARY> db.tab.find(); { "_id" : ObjectId("59354ba202d9a99ab2f879c6"), "name" : "a" } { "_id" : ObjectId("59354ba602d9a99ab2f879c7"), "name" : "b" } { "_id" : ObjectId("59354ba802d9a99ab2f879c8"), "name" : "c" } { "_id" : ObjectId("59354baa02d9a99ab2f879c9"), "name" : "d" } { "_id" : ObjectId("59354ccfd905432aeaccd542"), "name" : "e" } { "_id" : ObjectId("59354cd2d905432aeaccd543"), "name" : "f" }--查看 tab 表的數(shù)據(jù)跟原表數(shù)據(jù)相同,沒有什么影響,說明其他表的日志在空跑。
以上就是備份結(jié)合 oplog 的恢復(fù)操作。
備份很重要?。。?備份很重要?。。?備份很重要?。。≈匾氖虑橹v三遍~~~
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞標(biāo)題:MongoDB誤刪表恢復(fù)-創(chuàng)新互聯(lián)
文章位置:http://jinyejixie.com/article40/pshho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計公司、服務(wù)器托管、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容