簡體   English   中英

無法還原使用mongorestore刪除的表

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

我這樣做是為了測試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

開始蒙哥

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

刪除表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

當您將mongorestore與--directoryperdb選項一起使用時,每個數據庫的文件將保存在磁盤上的離散目錄中。

對於您的情況,集合test1的名稱空間是mytest.test1 使用--directoryperdb選項還原轉儲時,集合test1已還原到目錄/data/db/mytest ,而不是目錄/data/db

如果使用--dbpath /data/db啟動mongod,則找不到test1集合,因為該集合是從此dbpath中刪除的。 要找到您還原的test1集合,您應該使用--dbpath /data/db/mytest啟動mongod。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM