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
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 | |
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
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
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
2
3
4
5
6 7 8 9 10 11 12 13 | |
WTF? The size looks right, but there are no documents???
1
2
3
4
5
6 7 8 9 10 11 12 | |
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
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
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
2
3
4
5
6 7 8 9 10 11 12 13 | |
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.