MongoDB:备份数据( Mongodump )

和之前介绍的 mongoexport 的数据导出工具不同, mongodump 是将数据以二进制形式导出,而 mongoexport 导出的数据格式为 csv 或 json 格式; mongodump 可以导出一个数据库,或者整个 MongoDB 服务上的所有数据库,因此 mongodump 是更大范围的备份工具。

一 备份一个数据库

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

备注:从日志看出,mongodump 的结果会生成一个目录,第一层目录为数据库名,再下一层为每个集合对应的备份文件。

二 备份一个集合

1
2
3
4
5
6
7
[mongo@redhatB tf]$ mongodump -h 127.0.0.1 -d skytf -c things -o things.dmp  
connected to: 127.0.0.1
Sat Nov 17 17:12:20 DATABASE: skytf to things.dmp/skytf
Sat Nov 17 17:12:20 skytf.things to things.dmp/skytf/things.bson
Sat Nov 17 17:12:20 doing snapshot query
Sat Nov 17 17:12:20 30 objects
Sat Nov 17 17:12:20 Metadata for skytf.things to things.dmp/skytf/things.metadata.json

备注: mongodump 加上 -c 参数则可备份指定集合。

三 备份整个实例

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
26
27
28
29
[mongo@redhatB tf]$ mongodump -h 127.0.0.1 -o all.dmp  
connected to: 127.0.0.1
Sat Nov 17 17:17:04 all dbs
Sat Nov 17 17:17:04 DATABASE: skytf to all.dmp/skytf
Sat Nov 17 17:17:04 skytf.test_1 to all.dmp/skytf/test_1.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 1 objects
Sat Nov 17 17:17:04 Metadata for skytf.test_1 to all.dmp/skytf/test_1.metadata.json
Sat Nov 17 17:17:04 skytf.things to all.dmp/skytf/things.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 30 objects
Sat Nov 17 17:17:04 Metadata for skytf.things to all.dmp/skytf/things.metadata.json
Sat Nov 17 17:17:04 skytf.things_1 to all.dmp/skytf/things_1.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 30 objects
Sat Nov 17 17:17:04 Metadata for skytf.things_1 to all.dmp/skytf/things_1.metadata.json
Sat Nov 17 17:17:04 skytf.test_2 to all.dmp/skytf/test_2.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 4 objects
Sat Nov 17 17:17:04 Metadata for skytf.test_2 to all.dmp/skytf/test_2.metadata.json
Sat Nov 17 17:17:04 DATABASE: test to all.dmp/test
Sat Nov 17 17:17:04 test.test_2 to all.dmp/test/test_2.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 4 objects
Sat Nov 17 17:17:04 Metadata for test.test_2 to all.dmp/test/test_2.metadata.json
Sat Nov 17 17:17:04 test.things to all.dmp/test/things.bson
Sat Nov 17 17:17:04 doing snapshot query
Sat Nov 17 17:17:04 30 objects
Sat Nov 17 17:17:04 Metadata for test.things to all.dmp/test/things.metadata.json

备注:mongodump 如果不指定 -d 参数,刚会备份整个 MongoDB 实例。

3.1 查看目录

[mongo@redhatB tf]$ ll all.dmp/  
total 8.0K  
drwxrwxr-x. 2 mongo mongo 4.0K Nov 17 17:17 skytf  
drwxrwxr-x. 2 mongo mongo 4.0K Nov 17 17:17 test

备注:在目录 all.dmp 下产生了两个目录,目录名分别对应 MongoDB 上实例上的数据库名。

四 参考

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

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

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

相关推荐

发表回复

登录后才能评论