Recovering a WiredTiger collection from a corrupt MongoDB installation
Reference: http://www.alexbevi.com/blog/2016/02/10/recovering-a-wiredtiger-collection-from-a-corrupt-mongodb-installation/
Recently at work, we experienced a series of events that could have proven to be catastrophic for one of our datasets. We have a daily process that does daily cleanup, but relies on the presence of control data that is ETL’d in from another process.
The secondary process failed, and as a result, everything was “cleaned” … aka, we purged an entire dataset.
This data happens to be on a 5 node replicaset (primary-secondary-secondary-arbiter-hidden), and the hidden node died over the holidays and I waited too long to recover it, so it was unable to ever catch up to the primary (always stuck in a RECOVERING state).
My incredible foresight (… laziness … ) resulted in us having a backup of the data ready to be extracted from the out of sync hidden node. All we had to do was start up mongod … right?
1 |
|
Aw crap. I could not for the life of me get the node back up and running. Since this was a replica-set member, I thought maybe if I just copied the failing file from the (working) primary it would just work. Apparently that’s not the way MongoDB or WiredTiger works :P. Back to the drawing board.
I could see that my data directory contained a bunch of collection-*.wt and index-*.wt files, so I assumed these were the WiredTiger collection and index files. These are binary files so grep-ing didn’t help me identify the collection I needed.
I wanted to next see if I could just copy the collection’s backing file directly to a new (working) MongoDB installation, so I started up a new mongod, created a new collection with a document in it, then copied over any collection-*.wt file to see what would happen.
Guess what … didn’t work.
Identify the WiredTiger collection’s backing file
Since we had access to a working node, plus the collection hadn’t been dropped (just purged), I thought maybe the files on each node would be the same. I logged into the primary via the shell to get some info from my collection.
1 |
|
That "uri" : "statistics:table:collection-7895--1435676552983097781"entry looked promising.
I started hunting for a way to extract the data from this file without having to “mount” the file in another MongoDB installation, as I assumed this was not possible. I stumbled across a command line utility for WiredTiger that happened to have a ‘salvage’ command.
Salvaging the WiredTiger collection
In order to use the wt utility, you have to build it from source. Being comfortable in Linux, this was not daunting ;)
wget http://source.wiredtiger.com/releases/wiredtiger-2.7.0.tar.bz2
tar xvf wiredtiger-2.7.0.tar.bz2
cd wiredtiger-2.7.0
sudo apt-get install libsnappy-dev build-essential
./configure --enable-snappy
make
NOTE adding support for Google’s snappy compressor when building WiredTiger will save you some errors that I initially encountered when trying to salvage the data.
Now that I had a wt utility, I wanted to test it out on the collection file. It turns out that you need additional supporting files before you can do this. Once I’d copied over the necessary files, my working directory (called mongo-bak) looked like this:
-rw-r--r-- 1 root root 4738772992 Feb 9 14:06 collection-2657--1723320556100349955.wt
-rw-r--r-- 1 root root 1155072 Feb 9 14:05 _mdb_catalog.wt
-rw-r--r-- 1 root root 26935296 Feb 9 14:05 sizeStorer.wt
-rw-r--r-- 1 root root 95 Feb 9 14:05 storage.bson
-rw-r--r-- 1 root root 46 Feb 9 14:04 WiredTiger
-rw-r--r-- 1 root root 495 Feb 9 14:04 WiredTiger.basecfg
-rw-r--r-- 1 root root 21 Feb 9 14:04 WiredTiger.lock
-rw-r--r-- 1 root root 916 Feb 9 14:04 WiredTiger.turtle
-rw-r--r-- 1 root root 10436608 Feb 9 14:04 WiredTiger.wt
Now, from the directory where we compiled WiredTiger, we started salvaging the collection:
./wt -v -h ../mongo-bak -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R salvage collection-2657--1723320556100349955.wt
You know it’s working if you see output along the lines of:
WT_SESSION.salvage 639400
which I believe is just counting up the number of documents recovered. Once the operation has completed, it will have overwritten the source *.wt collection file with whatever it could salvage.
The only issue is that you still can’t load this into MongoDB yet.
Importing the WiredTiger collection via dump/load into MongoDB
In order to get the data into MongoDB, first we need to generate a dump file from the WiredTiger collection file. This is done using the wt utility:
./wt -v -h ../data -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R dump -f ../collection.dump collection-2657--1723320556100349955
This operation produces no output, so you’ll just have to sit tight and wait a while. You can always watch ls -l in another console if you want to make sure it’s working ;)
Once completed, you’ll have a collection.dump file, but this still can’t be loaded directly into MongoDB. You can however, using the wt utility one more time, load the dump back into a WiredTiger collection.
First, let’s startup a new mongod instance that we can try this out on.
mongod --dbpath tmp-mongo --storageEngine wiredTiger --nojournal
Next, let’s connect to this instance via the mongo shell and create a new collection:
use Recovery
db.borkedCollection.insert({test: 1})
db.borkedCollection.remove({})
db.borkedCollection.stats()
I’ve created a new db called Recovery, and inserted/removed a document so the collection’s backing file would be generated. You can use the stats() method to get the collection name, but since we’re only using one collection, it’s easy enough to find just using ls.
Now we’re going to take the backing file name of the collection we just created and use that to load our WiredTiger dump file:
./wt -v -h ../data -C "extensions=[./ext/compressors/snappy/.libs/libwiredtiger_snappy.so]" -R load -f ../collection.dump -r collection-2-880383588247732034
Note that we drop the .wt extension from the collection file above. Also, the -h flag needs to point to the directory where our mongod has it’s dbPath. Finally, mongod should not be running.
This operation also provides a progress indicator showing how much data has been loaded:
table:collection-4--4286091263744514813: 1386220
Once completed, we can start mongod back up, shell in and have a look:
1 |
|
WTF? The size looks right, but there are no documents???
1 |
|
Well that’s promising, but the collection still hasn’t been properly restored yet.
Restoring the MongoDB collection to a usable state
This final part is pretty straightforward, as we’re just going to do a mongodump, followed by a mongorestore.
NOTE The mongodump will fail if you’re using a version of MongoDB < 3.2, as 3.2 is built against WiredTiger 2.7. I initially tested this using MongoDB 3.0.9 and the dump operation just returned 0 results.
1 |
|
Now that we’ve dumped and reloaded the collection yet again, we can shell back in and validate that our recovery attempt has succeeded:
1 |
|
BOOYA! Everything is back and properly accessible.
The mongorestore could actually have been done to the primary node in order to recover the data for production purposes. Once that’s done, just recreate the necessary indexes and you’re back in business.
Recovering a WiredTiger collection from a corrupt MongoDB installation的更多相关文章
- Mongodb installation & userguide
1.Mongodb Installation in Ubuntu (1) Download from: https://www.mongodb.org/downloads File: mongodb- ...
- MongoDB 存储引擎:WiredTiger和In-Memory
存储引擎(Storage Engine)是MongoDB的核心组件,负责管理数据如何存储在硬盘(Disk)和内存(Memory)上.从MongoDB 3.2 版本开始,MongoDB 支持多数据存储引 ...
- MongoDB Sharding、库、collection设计学习汇总
sharding设计须考虑的几个因素 Sharding Key的选择 在片键的选择上,最好是能够在字段中选择混合型的片键,大范围的递增健.和随机分布的健组合,如按月份递增.按用户名 ...
- mongodb collection method
https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/ db.coll_test.getIndexes()# ...
- MongoDB存储引擎(中)——WiredTiger
上一篇博文介绍了MongoDB的MMAPv1存储引擎,本文接着介绍MongoDB另一个存储引擎--WiredTiger,WiredTiger是在MongoDB3.0版本引入的,并且在MongoDB3. ...
- 【翻译】MongoDB指南/CRUD操作(二)
[原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...
- MongoDB使用小结:一些常用操作分享
本文整理了一年多以来我常用的MongoDB操作,涉及mongo-shell.pymongo,既有运维层面也有应用层面,内容有浅有深,这也就是我从零到熟练的历程. MongoDB的使用之前也分享过一篇, ...
- CentOS7 安装MongoDB 3.0服务器
1,下载&安装 MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统.MongoDB宣称,3.0新版本不只 ...
- MongoDB 3.0(1):CentOS7 安装MongoDB 3.0服务
目录(?)[-] 1下载安装 2MongoDB CRUD 1创建数据 2更新数据 3删除 4查询 5更多方法 3MongoDB可视化工具 4总结 本文原文连接: http://blog.csdn. ...
随机推荐
- Oracle Sql优化之Rownum的使用
1.rownum:rownum是一个伪列,需要在数据取出来后,rownum才会有值,因此在分页查找时,需要进行嵌套查询. select sal,ename from (select rownum as ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- 如何让struts2和servlet的共存
如何让struts2和servlet的共存 (2013-08-29 14:07:49) 转载▼ 标签: servlet与struts2共存 同时配置struts2与ser servlet访问不到 分类 ...
- Nginx反向代理配置文件
server { listen ; server_name ; root E:/Upays/public/; index index.php index.html; log_not_found off ...
- 虚拟ip
网卡上增加一个IP: ifconfig eth0:1 192.168.0.1 netmask 255.255.255.0 删除网卡的第二个IP地址: ip addr del 192.168.0.1 d ...
- 转 如何高效使用和管理Bitmap--图片缓存管理模块的设计与实现
上周为360全景项目引入了图片缓存模块.因为是在Android4.0平台以上运作,出于惯性,都会在设计之前查阅相关资料,尽量避免拿一些以前2.3平台积累的经验来进行类比处理.开发文档中有一个 Bitm ...
- Linux字符编码转换 UTF8转GB3212
在LINUX上进行编码转换时,既可以利用iconv函数族编程实现,也可以利用iconv命令来实现,只不过后者是针对文件的,即将指定文件从一种编码转换为另一种编码. 一.利用iconv函数族进行编 ...
- java.lang.RuntimeException: java.lang.NoSuchMethodException:
[java] 15/12/19 14:09:46 INFO mapred.JobClient: Task Id : attempt_201512182036_0017_m_000000_0, Stat ...
- OCMOD代码调整系统(Modification System)
OCMOD 是一个允许用户上传压缩文件的系统,该压缩文件包含了XML, SQL和PHP文件,从而修改网站相关地方. OCMOD是opencart系统的代码调整系统,遵循GPL3协议免费使用. 如果OC ...
- 关于HTTP_CLIENT_IP,HTTP_X_FORWAR
HTTP_CLIENT_IP:可通过http头伪造HTTP_X_FORWARDED_FOR:可通过http头伪造REMOTE_ADDR:可能是用户真实IP也可能是代理IP 服务端获取IP地址 http ...