MongoDB:恢复数据( Mongorestore )

上篇学习了使用 mongodump 以二进制备份数据库,接着学习使用对应的 mongorestore 恢复数据库,以下是实验过程:

1 备份数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[mongo@redhatB tf]$ mongodump -h 127.0.0.1 -d skytf -o skytf.dmp  
connected to: 127.0.0.1
Sat Nov 17 17:37:33 DATABASE: skytf to skytf.dmp/skytf
Sat Nov 17 17:37:33 skytf.test_1 to skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33 1 objects
Sat Nov 17 17:37:33 Metadata for skytf.test_1 to skytf.dmp/skytf/test_1.metadata.json
Sat Nov 17 17:37:33 skytf.things to skytf.dmp/skytf/things.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33 30 objects
Sat Nov 17 17:37:33 Metadata for skytf.things to skytf.dmp/skytf/things.metadata.json
Sat Nov 17 17:37:33 skytf.things_1 to skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33 30 objects
Sat Nov 17 17:37:33 Metadata for skytf.things_1 to skytf.dmp/skytf/things_1.metadata.json
Sat Nov 17 17:37:33 skytf.test_2 to skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:37:33 doing snapshot query
Sat Nov 17 17:37:33 4 objects
Sat Nov 17 17:37:33 Metadata for skytf.test_2 to skytf.dmp/skytf/test_2.metadata.json

备注:上述命令备份数据库 skytf,产生的备份文件为目录 skytf.dmp/skytf。

2 删除数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[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 17 17:42:03 skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:42:03 going into namespace [skytf.test_1]
1 objects found
Sat Nov 17 17:42:03 Creating index: { key: { _id: 1 }, ns: "skytf.test_1", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:42:04 going into namespace [skytf.test_2]
4 objects found
Sat Nov 17 17:42:04 Creating index: { key: { _id: 1 }, ns: "skytf.test_2", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:42:04 going into namespace [skytf.things_1]
30 objects found
Sat Nov 17 17:42:04 Creating index: { key: { _id: 1 }, ns: "skytf.things_1", name: "_id_" }
Sat Nov 17 17:42:04 skytf.dmp/skytf/things.bson
Sat Nov 17 17:42:04 going into namespace [skytf.things]
30 objects found
Sat Nov 17 17: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

备注:从上看出,数据库 skytf 已恢复;还原指定数据库,数据文件需要指定到备份文件下面的子目录,如果不指定子目录,会报以下错误。

5 ERROR

1
2
3
4
[mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf skytf.dmp/  
connected to: 127.0.0.1
Sat Nov 17 17:41:15 ERROR: ERROR: root directory must be a dump of a single database
Sat Nov 17 17:41:15 ERROR: when specifying a db name with --db

6 在恢复前先删除数据库中的对像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[mongo@redhatB tf]$ mongorestore -h 127.0.0.1 -d skytf --drop skytf.dmp/skytf  
connected to: 127.0.0.1
Sat Nov 17 17:45:37 skytf.dmp/skytf/test_1.bson
Sat Nov 17 17:45:37 going into namespace [skytf.test_1]
Sat Nov 17 17:45:37 dropping
1 objects found
Sat Nov 17 17:45:37 Creating index: { key: { _id: 1 }, ns: "skytf.test_1", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/test_2.bson
Sat Nov 17 17:45:37 going into namespace [skytf.test_2]
Sat Nov 17 17:45:37 dropping
4 objects found
Sat Nov 17 17:45:37 Creating index: { key: { _id: 1 }, ns: "skytf.test_2", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/things_1.bson
Sat Nov 17 17:45:37 going into namespace [skytf.things_1]
Sat Nov 17 17:45:37 dropping
30 objects found
Sat Nov 17 17:45:37 Creating index: { key: { _id: 1 }, ns: "skytf.things_1", name: "_id_" }
Sat Nov 17 17:45:37 skytf.dmp/skytf/things.bson
Sat Nov 17 17:45:37 going into namespace [skytf.things]
Sat Nov 17 17:45:37 dropping
30 objects found
Sat Nov 17 17:45:37 Creating index: { key: { _id: 1 }, ns: "skytf.things", name: "_id_" }

备注:使用 –drop 参数可以在恢复前先删除已存在的对像。

7 参考

http://docs.mongodb.org/manual/reference/mongorestore/

原创文章,作者:carmelaweatherly,如若转载,请注明出处:https://blog.ytso.com/237916.html

(0)
上一篇 2022年1月29日
下一篇 2022年1月29日

相关推荐

发表回复

登录后才能评论