无法恢复使用mongorestore删除的表

时间:2013-05-21 01:41:28

标签: linux mongodb

我这样做是为了测试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

drop table test1

# 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

1 个答案:

答案 0 :(得分:3)

当您使用带有--directoryperdb选项的mongorestore时,每个数据库的文件都将保存在磁盘上的离散目录中。

对于您的情况,集合test1的名称空间为mytest.test1。使用--directoryperdb选项还原转储时,集合test1已还原到目录/data/db/mytest,而不是/data/db

如果您使用test1启动mongod,则无法找到--dbpath /data/db集合,因为该集合已从此dbpath中删除。要查找已恢复的test1集合,您应该使用--dbpath /data/db/mytest启动mongod。