How-to restore MongoDB from raw data files

MongoDB is different from the traditional RDBMS database. It provided tools to read from raw data files directly. This is extremely helpful in some cases.

Here is an example:

Scenario: A DB were wrongly dropped by the user. Now the user want to recover that DB from the backup data files. 
1. Dump data from backup data files for the db "testdb"
[mongo@alexzeng bak]$ mongodump -d testdb --dbpath /mongo/data/bak/ -o dump
Mon Jun 11 20:14:25 [tools] query testdb.* ntoreturn:1 reslen:20 4219ms
DATABASE: testdb	 to 	dump/testdb
	testdb.system.indexes to dump/testdb/system.indexes.bson
		 2 objects
	testdb.autofill_hadoop to dump/testdb/autofill_hadoop.bson
		 0 objects
Mon Jun 11 20:14:25 dbexit: 
Mon Jun 11 20:14:25 [tools] shutdown: going to close listening sockets...
Mon Jun 11 20:14:25 [tools] shutdown: going to flush diaglog...
Mon Jun 11 20:14:25 [tools] shutdown: going to close sockets...
Mon Jun 11 20:14:25 [tools] shutdown: waiting for fs preallocator...
Mon Jun 11 20:14:25 [tools] shutdown: closing all files...
Mon Jun 11 20:14:25 [tools] closeAllFiles() finished
Mon Jun 11 20:14:25 [tools] shutdown: removing fs lock...
Mon Jun 11 20:14:25 dbexit: really exiting now

2. Restore the DB "testdb" to the target MongoDB replicat-set.
[mongo@alexzeng bak]$ mongorestore -d testdb -h alexzeng.wordpress.com:27017 --objcheck /mongo/data/bak/dump/testdb 
connected to: alexzeng.wordpress.com:27017
Mon Jun 11 20:27:25 /mongo/data/bak/dump/testdb/autofill_hadoop.bson
Mon Jun 11 20:27:25 	 going into namespace [testdb.autofill_hadoop]
Mon Jun 11 20:27:25 file /mongo/data/bak/dump/testdb/autofill_hadoop.bson empty, skipping
Mon Jun 11 20:27:25 /mongo/data/bak/dump/testdb/system.indexes.bson
Mon Jun 11 20:27:25 	 going into namespace [testdb.system.indexes]
Mon Jun 11 20:27:25 { key: { _id: 1 }, ns: "testdb.autofill_hadoop", name: "_id_" }
Mon Jun 11 20:27:25 { key: { prefix: 1.0 }, ns: "testdb.autofill_hadoop", name: "prefix_1" }
2 objects found

Advertisements

About Alex Zeng
I would be very happy if this blog can help you. I appreciate every honest comments. Please forgive me if I'm too busy to reply your comments in time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: