[mongo@redhatB tf]$ mongodump -h 127.0.0.1 -d skytf -o skytf.dmp connected to: 127.0.0.1 Sat Nov 1717:37:33 DATABASE: skytf to skytf.dmp/skytf Sat Nov 1717:37:33 skytf.test_1 to skytf.dmp/skytf/test_1.bson Sat Nov 1717:37:33 doing snapshot query Sat Nov 1717:37:331 objects Sat Nov 1717:37:33 Metadata for skytf.test_1 to skytf.dmp/skytf/test_1.metadata.json Sat Nov 1717:37:33 skytf.things to skytf.dmp/skytf/things.bson Sat Nov 1717:37:33 doing snapshot query Sat Nov 1717:37:3330 objects Sat Nov 1717:37:33 Metadata for skytf.things to skytf.dmp/skytf/things.metadata.json Sat Nov 1717:37:33 skytf.things_1 to skytf.dmp/skytf/things_1.bson Sat Nov 1717:37:33 doing snapshot query Sat Nov 1717:37:3330 objects Sat Nov 1717:37:33 Metadata for skytf.things_1 to skytf.dmp/skytf/things_1.metadata.json Sat Nov 1717:37:33 skytf.test_2 to skytf.dmp/skytf/test_2.bson Sat Nov 1717:37:33 doing snapshot query Sat Nov 1717:37:334 objects Sat Nov 1717:37:33 Metadata for skytf.test_2 to skytf.dmp/skytf/test_2.metadata.json
[mongo@redhatB ~]$ mongo MongoDB shell version: 2.2.1 connecting to: test > show dbs; local (empty) skytf 0.0625GB test 0.0625GB
> use skytf; switched to db skytf
> show collections; system.indexes test_1 test_2 things things_1
> db.dropDatabase(); { "dropped" : "skytf", "ok" : 1 } > show collections; > show dbs; local (empty) skytf (empty) test 0.0625GB
备注:为了测试需要,先删除数据库 skytf。
3 还原数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf skytf.dmp/skytfconnected to:127.0.0.1 Sat Nov 1717:42:03 skytf.dmp/skytf/test_1.bson Sat Nov 1717:42:03 going into namespace [skytf.test_1] 1 objects found Sat Nov 1717:42:03 Creating index: { key: { _id:1 }, ns:"skytf.test_1", name:"_id_" } Sat Nov 1717:42:04 skytf.dmp/skytf/test_2.bson Sat Nov 1717:42:04 going into namespace [skytf.test_2] 4 objects found Sat Nov 1717:42:04 Creating index: { key: { _id:1 }, ns:"skytf.test_2", name:"_id_" } Sat Nov 1717:42:04 skytf.dmp/skytf/things_1.bson Sat Nov 1717:42:04 going into namespace [skytf.things_1] 30 objects found Sat Nov 1717:42:04 Creating index: { key: { _id:1 }, ns:"skytf.things_1", name:"_id_" } Sat Nov 1717:42:04 skytf.dmp/skytf/things.bson Sat Nov 1717:42:04 going into namespace [skytf.things] 30 objects found Sat Nov 1717:42:04 Creating index: { key: { _id:1 }, ns:"skytf.things", name:"_id_" }
备注:使用 mongorestore 恢复数据库,从上面看到数据库 skytf 已恢复的细节。
4 验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[mongo@redhatB ~]$ mongo MongoDB shell version: 2.2.1 connecting to: test
> show dbs; local (empty) skytf 0.0625GB test 0.0625GB
> use skytf; switched to db skytf
> show collections; system.indexes test_1 test_2 things things_1
[mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf skytf.dmp/ connected to: 127.0.0.1 Sat Nov 1717:41:15ERROR: ERROR: root directory must be a dump of a single database Sat Nov 1717:41:15ERROR: when specifying a db name with --db
[mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf --drop skytf.dmp/skytf connected to:127.0.0.1 Sat Nov 1717:45:37 skytf.dmp/skytf/test_1.bson Sat Nov 1717:45:37 going into namespace [skytf.test_1] Sat Nov 1717:45:37 dropping 1 objects found Sat Nov 1717:45:37 Creating index: { key: { _id:1 }, ns:"skytf.test_1", name:"_id_" } Sat Nov 1717:45:37 skytf.dmp/skytf/test_2.bson Sat Nov 1717:45:37 going into namespace [skytf.test_2] Sat Nov 1717:45:37 dropping 4 objects found Sat Nov 1717:45:37 Creating index: { key: { _id:1 }, ns:"skytf.test_2", name:"_id_" } Sat Nov 1717:45:37 skytf.dmp/skytf/things_1.bson Sat Nov 1717:45:37 going into namespace [skytf.things_1] Sat Nov 1717:45:37 dropping 30 objects found Sat Nov 1717:45:37 Creating index: { key: { _id:1 }, ns:"skytf.things_1", name:"_id_" } Sat Nov 1717:45:37 skytf.dmp/skytf/things.bson Sat Nov 1717:45:37 going into namespace [skytf.things] Sat Nov 1717:45:37 dropping 30 objects found Sat Nov 1717:45:37 Creating index: { key: { _id:1 }, ns:"skytf.things", name:"_id_" }