MongoDB 異常掉電後的數據打撈

當異常掉電後,實例啓動會報相似以下錯誤,屬於文件級別的損壞,這時候常規的修復方案是沒用的,能夠使用 wt 進行數據打撈。c++

2019-07-10T21:06:05.725-0500 E STORAGE  [initandlisten] WiredTiger (0) [1454119565:724960][1745:0x7f2ac9534bc0], file:WiredTiger.wt, cursor.next: read checksum error for 4096B block at offset 6
799360: block header checksum of 1769173605 doesnt match expected checksum of 4176084783
2019-07-10T21:06:05.725-0500 E STORAGE  [initandlisten] WiredTiger (0) [1454119565:725067][1745:0x7f2ac9534bc0], file:WiredTiger.wt, cursor.next: WiredTiger.wt: encountered an illegal file format or internal value
2019-07-10T21:06:05.725-0500 E STORAGE  [initandlisten] WiredTiger (-31804) [1454119565:725088][1745:0x7f2ac9534bc0], file:WiredTiger.wt, cursor.next: the process must exit and restart: WT_PANIC: WiredTiger library panic
2019-07-10T21:06:05.725-0500 I -        [initandlisten] Fatal Assertion 28558


安裝

必要組件安裝

[centos]
yum install  snappy-devel  make gcc gcc-c++ kernel-devel

[ubuntu]
apt-get install libsnappy-dev build-essential 

 wt 安裝 

官網地址 http://source.wiredtiger.comapache

wget http://source.wiredtiger.com/releases/wiredtiger-3.2.0.tar.bz2
tar xvf wiredtiger-3.2.0.tar.bz2 
cd wiredtiger-3.2.0
./configure --enable-snappy
make

數據打撈過程

創建恢復目錄

切記不要在損壞的原目錄進行操做,要在工做路徑創建一個恢復路徑,將損壞的 dbpath 複製一份到此處。
好比拷貝到 /opt/5113_wechatworkpre_bak/ ,理論上只有必要的 .wt 和損壞的 collection 文件自己就夠了,以防萬一能夠拷貝整個 dbpath .ubuntu

查找損壞的 collection 的文件名

假設日誌中損壞的 collection 叫 dmeo.
執行 db.demo.stats() 定位到 uri ,拿到具體的文件名 wechatworkpre_leju_com/collection-26--7067895897049507085 swift

打撈

執行以下命令進行打撈做業,打撈出來的文件將覆蓋原文件centos

./wt -v -h /opt/5113_wechatworkpre_bak/ -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R salvage wechatworkpre_leju_com/collection-26--7067895897049507085.wt (帶.wt後綴)

dump

到此爲止,不能直接將新生成的文件拷回 datapath ,要執行一次 dump ,生成咱們想要的 collection 的原始數據,以下是  demo.dump bash

./wt -v -h /opt/5113_wechatworkpre_bak/ -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R dump -f demo.dump wechatworkpre_leju_com/collection-26--7067895897049507085 (不帶.wt後綴)


建立新備份實例

這個備份實例的做用是用於導入用 wt dump 出來的數據,爲後來的工做作準備app

mkdir /opt/mongo-recovery/ -p 
mongod --dbpath mongo-recovery --storageEngine wiredTiger --nojournal
mongo --port 27017 
use recovery
db.demo.insert({test: 1})
db.demo.remove({})
db.demo.stats()

load

拿到 collection 對應的數據文件 "uri" : "statistics:table:collection-7--5182884231633924913",將上面 wt 生成的 dump 文件 load 到新備份實例中(須要關閉備份實例)ui

./wt -v -h  /opt/mongo-recovery/  -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R load -f demo.dump -r collection-7--5182884231633924913

從新啓動備份實例

> use recovery
switched to db recovery
> db.demo.find()
{ "_id" : ObjectId("5d270709f43c5aedc1aae7c8"), "age" : 1 }

到此爲止,打撈出來的數據成功導入備份實例
若是還有問題只須要執行次 mongodump 和 mongorestore --drop  便可。spa

wt 幫助

global options:
        -C      wiredtiger_open configuration
        -h      database directory
        -L      turn logging off for debug-mode
        -R      run recovery if configured
        -V      display library version and exit
        -v      verbose
commands:
        alter     alter an object
        backup    database backup
        compact   compact an object
        copyright copyright information
        create    create an object
        downgrade downgrade a database
        drop      drop an object
        dump      dump an object
        list      list database objects
        load      load an object
        loadtext  load an object from a text file
        printlog  display the database log
        read      read values from an object
        rebalance rebalance an object
        rename    rename an object
        salvage   salvage a file
        stat      display statistics for an object
        truncate  truncate an object, removing all content
        upgrade   upgrade an object
        verify    verify an object
        write     write values to an object
相關文章
相關標籤/搜索