安装 wt
工具
wget http://source.wiredtiger.com/releases/wiredtiger-3.0.0.tar.bz2
tar xvf wiredtiger-3.0.0.tar.bz2
cd wiredtiger-3.0.0
sudo yum install snappy-devel -y
./configure --enable-snappy
make
修复 wt
文件
将要修复的mongo的 存储目录整个复制一份到 data ,这里比如我要恢复 collection-242-696896999396352821.wt 文件 ,命令如下
./wt -v -h ../data -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R dump -f ../collection.dump collection-242-696896999396352821.wt
output:
collection-242-696896999396352821.wt 3507261
修复好的数据在 …/collection.dump 中,需要导出,注意这里的文件没有 wt 后缀
./wt -v -h ../data -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R dump -f ../collection.dump collection-242-696896999396352821
数据恢复
- 新建一个库
use testdb
db.back.insert({test: 1})
db.back.remove({})
db.back.stats()
collection-15–558436840631144017
# 找到 back 集合 的文件是 collection-15–558436840631144017
- 将新mongo服务关闭,不然会占用 collection-15–558436840631144017.wt
将之前的 collection.dump load 到 testdb 的 collection-15–558436840631144017 ,注意这里的 …/…/data/mongodb/data 是新库的存储地址
./wt -v -h ../../data/mongodb/data -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R load -f ../collection.dump -r collection-15–558436840631144017
输出如下:
collection-15–558436840631144017 :3507261
- 再去新mongo 查看
db.back.count()
0
db.repairDatabase()
再去新mongo 查看 db.back.count()
3507261
完成恢复