简体   繁体   English

无法还原使用mongorestore删除的表

[英]can not restore table which was dropped using mongorestore

i do this to test mongorestore utility 我这样做是为了测试mongorestore实用程序

the following is my test procedure: 以下是我的测试过程:

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

do file backup using mongodump 使用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

start mongod 开始蒙哥

# /opt/mongodb/bin/mongod -f /etc/mongod.conf

drop table test1 删除表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

shutdown mongod 关闭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

try to restore test1 from backup 尝试从备份还原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

and find no test1 in mytest database 并在mytest数据库中找不到test1

# mongo 127.0.0.1/mytest -u alex -p alex\
> show collections;
system.indexes
system.users
testtable

As you use mongorestore with --directoryperdb option, every database's files will be saved in discrete directories on the disk. 当您将mongorestore与--directoryperdb选项一起使用时,每个数据库的文件将保存在磁盘上的离散目录中。

For your case, the namespace of collection test1 is mytest.test1 . 对于您的情况,集合test1的名称空间是mytest.test1 When you restored the dump with --directoryperdb option, the collection test1 was restored to directory /data/db/mytest , rather than /data/db . 使用--directoryperdb选项还原转储时,集合test1已还原到目录/data/db/mytest ,而不是目录/data/db

You couldn't find test1 collection if you start mongod with --dbpath /data/db because the collection was dropped from this dbpath. 如果使用--dbpath /data/db启动mongod,则找不到test1集合,因为该集合是从此dbpath中删除的。 To find the test1 collection that you restored, you should start mongod with --dbpath /data/db/mytest . 要找到您还原的test1集合,您应该使用--dbpath /data/db/mytest启动mongod。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM