我这样做是为了测试 mongorestore 实用程序
以下是我的测试程序:
mongo 127.0.0.1/mytest -u alex -p alex
> db.createCollection("test1")
{ "ok" : 1 }
> db.test1.insert({id:1,name:"alex"})
> db.test1.find()
{ "_id" : ObjectId("519b3d7808b450e533c8ea5e"), "id" : 1, "name" : "alex" }
> show collections;
system.indexes
system.users
test1
testtable
> exit
ps -ef|grep mongod
root 3201 3180 0 16:46 pts/1 00:00:00 vi /etc/mongod.conf
root 3583 1 0 17:27 ? 00:00:00 /opt/mongodb/bin/mongod -f /etc/mongod.conf
root 3600 3267 0 17:28 pts/3 00:00:00 grep mongod
kill -9 3583
使用 mongodump 进行文件备份
/opt/mongodb/bin/mongodump --dbpath /data/db/ --journal --out /data/dump
Tue May 21 17:26:38.107 [tools] journal dir=/data/db/journal
Tue May 21 17:26:38.107 [tools] recover begin
Tue May 21 17:26:38.107 [tools] recover lsn: 33813
Tue May 21 17:26:38.107 [tools] recover /data/db/journal/j._0
Tue May 21 17:26:38.126 [tools] recover skipping application of section seq:0 < lsn:33813
Tue May 21 17:26:38.129 [tools] recover cleaning up
Tue May 21 17:26:38.129 [tools] removeJournalFiles
Tue May 21 17:26:38.130 [tools] recover done
Tue May 21 17:26:38.204 [tools] all dbs
Tue May 21 17:26:38.205 [tools] DATABASE: mytest to /data/dump/mytest
Tue May 21 17:26:38.206 [tools] mytest.system.indexes to /data/dump/mytest/system.indexes.bson
Tue May 21 17:26:38.207 [tools] 4 objects
Tue May 21 17:26:38.207 [tools] mytest.system.users to /data/dump/mytest/system.users.bson
Tue May 21 17:26:38.207 [tools] 1 objects
Tue May 21 17:26:38.207 [tools] Metadata for mytest.system.users to /data/dump/mytest/system.users.metadata.json
Tue May 21 17:26:38.207 [tools] mytest.testtable to /data/dump/mytest/testtable.bson
Tue May 21 17:26:38.208 [tools] 3 objects
Tue May 21 17:26:38.208 [tools] Metadata for mytest.testtable to /data/dump/mytest/testtable.metadata.json
Tue May 21 17:26:38.208 [tools] mytest.test1 to /data/dump/mytest/test1.bson
Tue May 21 17:26:38.208 [tools] 1 objects
Tue May 21 17:26:38.208 [tools] Metadata for mytest.test1 to /data/dump/mytest/test1.metadata.json
Tue May 21 17:26:38.208 dbexit:
Tue May 21 17:26:38.209 [tools] shutdown: going to close listening sockets...
Tue May 21 17:26:38.209 [tools] shutdown: going to flush diaglog...
Tue May 21 17:26:38.209 [tools] shutdown: going to close sockets...
Tue May 21 17:26:38.209 [tools] shutdown: waiting for fs preallocator...
Tue May 21 17:26:38.209 [tools] shutdown: lock for final commit...
Tue May 21 17:26:38.209 [tools] shutdown: final commit...
Tue May 21 17:26:38.209 [tools] shutdown: closing all files...
Tue May 21 17:26:38.210 [tools] closeAllFiles() finished
Tue May 21 17:26:38.210 [tools] journalCleanup...
Tue May 21 17:26:38.210 [tools] removeJournalFiles
Tue May 21 17:26:38.211 [tools] shutdown: removing fs lock...
Tue May 21 17:26:38.211 dbexit: really exiting now
启动 mongod
# /opt/mongodb/bin/mongod -f /etc/mongod.conf
掉落表测试1
# mongo 127.0.0.1/mytest -u alex -p alex
> show collections;
system.indexes
system.users
test1
testtable
> db.test1.drop()
true
> show collections;
system.indexes
system.users
testtable
> exit
bye
关闭mongod
# ps -ef|grep mongod
root 3201 3180 0 16:46 pts/1 00:00:00 vi /etc/mongod.conf
root 3544 1 0 17:24 ? 00:00:00 /opt/mongodb/bin/mongod -f /etc/mongod.conf
root 3556 3267 0 17:24 pts/3 00:00:00 grep mongod
# kill -9 3544
尝试从备份中恢复 test1
# mongorestore --dbpath /data/db --directoryperdb --journal /data/dump/
Tue May 21 17:29:57.518 [tools] journal dir=/data/db/journal
Tue May 21 17:29:57.518 [tools] recover begin
Tue May 21 17:29:57.519 [tools] info no lsn file in journal/ directory
Tue May 21 17:29:57.519 [tools] recover lsn: 0
Tue May 21 17:29:57.519 [tools] recover /data/db/journal/j._0
Tue May 21 17:29:57.554 [tools] recover cleaning up
Tue May 21 17:29:57.554 [tools] removeJournalFiles
Tue May 21 17:29:57.555 [tools] recover done
Tue May 21 17:29:57.629 [tools] /data/dump/mytest/test1.bson
Tue May 21 17:29:57.630 [tools] going into namespace [mytest.test1]
Tue May 21 17:29:57.631 [tools] warning: Restoring to mytest.test1 without dropping. Restored data will be inserted without raising errors; check your server log
1 objects found
Tue May 21 17:29:57.632 [tools] Creating index: { key: { _id: 1 }, ns: "mytest.test1", name: "_id_" }
Tue May 21 17:29:57.633 [tools] /data/dump/mytest/system.users.bson
Tue May 21 17:29:57.633 [tools] going into namespace [mytest.system.users]
Tue May 21 17:29:57.633 [tools] warning: Restoring to mytest.system.users without dropping. Restored data will be inserted without raising errors; check your server log
1 objects found
Tue May 21 17:29:57.634 [tools] Creating index: { key: { _id: 1 }, ns: "mytest.system.users", name: "_id_" }
Tue May 21 17:29:57.634 [tools] Creating index: { key: { user: 1, userSource: 1 }, unique: true, ns: "mytest.system.users", name: "user_1_userSource_1" }
Tue May 21 17:29:57.634 [tools] /data/dump/mytest/testtable.bson
Tue May 21 17:29:57.634 [tools] going into namespace [mytest.testtable]
Tue May 21 17:29:57.634 [tools] warning: Restoring to mytest.testtable without dropping. Restored data will be inserted without raising errors; check your server log
3 objects found
Tue May 21 17:29:57.635 [tools] Creating index: { key: { _id: 1 }, ns: "mytest.testtable", name: "_id_" }
Tue May 21 17:29:57.635 dbexit:
Tue May 21 17:29:57.636 [tools] shutdown: going to close listening sockets...
Tue May 21 17:29:57.636 [tools] shutdown: going to flush diaglog...
Tue May 21 17:29:57.636 [tools] shutdown: going to close sockets...
Tue May 21 17:29:57.636 [tools] shutdown: waiting for fs preallocator...
Tue May 21 17:29:57.636 [tools] shutdown: lock for final commit...
Tue May 21 17:29:57.636 [tools] shutdown: final commit...
Tue May 21 17:29:57.649 [tools] shutdown: closing all files...
Tue May 21 17:29:57.649 [tools] closeAllFiles() finished
Tue May 21 17:29:57.649 [tools] journalCleanup...
Tue May 21 17:29:57.649 [tools] removeJournalFiles
Tue May 21 17:29:57.650 [tools] shutdown: removing fs lock...
Tue May 21 17:29:57.650 dbexit: really exiting now
在 mytest 数据库中找不到 test1
# mongo 127.0.0.1/mytest -u alex -p alex\
> show collections;
system.indexes
system.users
testtable