Mongodb的导出mongodump和mongorestore导入工具结合使用。可以用于数据库的备份和还原。

mongodump是用于创建数据库内容的二进制导出的实用程序。
mongodump可以从mongod或mongos实例中导出数据;即可以从独立,副本集和分片群集部署中导出数据。
为了避免影响线上的业务,我们才使用mongodup工具时,尽可能在副本集的从节点或者延迟节点执行操作。

mongodump和mongorestore不能成为正在进行分片事务的4.2分片群集的备份策略的一部分,因为用mongodump创建的备份不能保持跨分片事务的原子性保证。

官网也强调:
对于具有正在进行的分片事务的4.2分片群集,请使用以下协调的备份和还原过程之一,该过程确实维护了跨分片事务的原子性保证:
MongoDB Atlas,
MongoDB Cloud Manager, or
MongoDB Ops Manager.

1、可以使用mongodump --help 查看帮助文档使用方法。

只要我们安装了mongodb服务,就能够使用mongodump命令,

1.1、备份整个实例:

mongodump -h IP:Port -o 导出到目录

[root@VM_6_17_centos ~]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020 -o dump/
2020-05-27T10:49:21.610+0800    writing admin.system.version to
2020-05-27T10:49:21.610+0800    done dumping admin.system.version (1 document)
2020-05-27T10:49:21.611+0800    writing test_haijiao.user_file to
2020-05-27T10:49:21.611+0800    writing test_jia.user_info to
2020-05-27T10:49:21.611+0800    done dumping test_jia.user_info (2 documents)
2020-05-27T10:49:21.611+0800    done dumping test_haijiao.user_file (1 document)

1.2、备份单个数据库:

mongodump -h IP:Port -d 数据库名 -o 导出到目录

[root@VM_6_17_centos conf]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -d test_jia -o dump/
2020-05-27T10:53:11.404+0800    writing test_jia.user_info to
2020-05-27T10:53:11.405+0800    done dumping test_jia.user_info (2 documents)

1.3、备份数据库的一张表:

mongodump -h IP:Port -d 数据库名 -c 集合名 -o 导出到目录

[root@VM_6_17_centos conf]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -d test_jia -c user_info  -o dump/
2020-05-27T10:53:49.162+0800    writing test_jia.user_info to
2020-05-27T10:53:49.162+0800    done dumping test_jia.user_info (2 documents)

1.4、备份数据库的一张表并且过滤:

查看表里的数据我们过滤年龄大于30岁的数据

db.user_info.find()
{ "_id" : ObjectId("5ecdcd0bc6a43453c65e484f"), "name" : "jiahaijiao", "age" : 32 }
{ "_id" : ObjectId("5ecdcde7c6a43453c65e4850"), "name" : "jiahaijiao", "age" : 32 }
{ "_id" : ObjectId("5ecdd6f5bf235c4a69f3b672"), "name" : "zhang", "age" : 30 }
{ "_id" : ObjectId("5ecdd6febf235c4a69f3b673"), "name" : "wang", "age" : 30 }
{ "_id" : ObjectId("5ecdd780bf235c4a69f3b674"), "name" : "li", "age" : 28 }

[root@VM_6_17_centos conf]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -d test_jia -c user_info -q '{"age":{"$gt": 30}}'  -o dump/
2020-05-27T11:18:19.103+0800    writing test_jia.user_info to
2020-05-27T11:18:19.103+0800    done dumping test_jia.user_info (2 documents)

1.5、导出单个库的压缩文件:

[root@VM_6_17_centos conf]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -d test_jia -o dump/ --gzip
2020-05-27T11:19:33.748+0800    writing test_jia.user_info to
2020-05-27T11:19:33.749+0800    done dumping test_jia.user_info (5 documents)
[root@VM_6_17_centos conf]# ll dump/
总用量 4
drwxr-xr-x 2 root root 4096 5月  27 11:19 test_jia
[root@VM_6_17_centos conf]# ll dump/test_jia/
总用量 16
-rw-r--r-- 1 root root 112 5月  27 11:18 user_info.bson
-rw-r--r-- 1 root root 152 5月  27 11:19 user_info.bson.gz
-rw-r--r-- 1 root root 166 5月  27 11:18 user_info.metadata.json
-rw-r--r-- 1 root root 151 5月  27 11:19 user_info.metadata.json.gz

1. 6、设置导出集合并发数,加快导出速度

-j, --numParallelCollections= number of collections to dump in parallel (4 by default) (default: 4) //默导出文档并发4个

[root@VM_6_17_centos conf]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -d test_jia -o dump/ --gzip -j 6
2020-05-27T11:23:34.568+0800    writing test_jia.user_info to
2020-05-27T11:23:34.569+0800    done dumping test_jia.user_info (5 documents)
[root@VM_6_17_centos conf]#

1.7、数据认证管理员备份

数据库开启认证授权以后需要使用用户名和密码,认证数据库参数进行备份。

[root@VM_6_17_centos ~]# /root/mongodb-4.2.1/bin/mongodump -h 192.168.6.17:27020  -u dba -p12387654  -d test_jia -o dump/   --authenticationDatabase=admin
2020-05-27T15:06:51.049+0800    writing test_jia.user_info to
2020-05-27T15:06:51.050+0800    done dumping test_jia.user_info (5 documents)
[root@VM_6_17_centos ~]#

2、mongorestore

将mongodump生成的备份还原到正在运行的实例

2.1、还原整个实例

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030  dump/
2020-05-27T15:25:52.968+0800    preparing collections to restore from
2020-05-27T15:25:52.970+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json
2020-05-27T15:25:52.973+0800    reading metadata for test_haijiao.user_file from dump/test_haijiao/user_file.metadata.json
2020-05-27T15:25:52.996+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson
2020-05-27T15:25:52.998+0800    no indexes to restore
2020-05-27T15:25:52.998+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T15:25:53.007+0800    restoring test_haijiao.user_file from dump/test_haijiao/user_file.bson
2020-05-27T15:25:53.011+0800    no indexes to restore
2020-05-27T15:25:53.011+0800    finished restoring test_haijiao.user_file (1 document, 0 failures)
2020-05-27T15:25:53.011+0800    restoring users from dump/admin/system.users.bson
2020-05-27T15:25:53.078+0800    6 document(s) restored successfully. 0 document(s) failed to restore.

[root@VM_6_17_centos ~]#

2.1、还原单个库

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030  --db test_jia  dump/test_jia/
2020-05-27T15:35:06.633+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2020-05-27T15:35:06.634+0800    building a list of collections to restore from dump/test_jia dir
2020-05-27T15:35:06.634+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json
2020-05-27T15:35:06.657+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson
2020-05-27T15:35:06.658+0800    no indexes to restore
2020-05-27T15:35:06.658+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T15:35:06.658+0800    5 document(s) restored successfully. 0 document(s) failed to restore.

2.2、还原库的单个表

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030  --db test_jia  -c user_info  dump/test_jia/user_hobby.bson
2020-05-27T15:47:52.552+0800    checking for collection data in dump/test_jia/user_hobby.bson
2020-05-27T15:47:52.552+0800    restoring to existing collection test_jia.user_info without dropping
2020-05-27T15:47:52.552+0800    reading metadata for test_jia.user_info from dump/test_jia/user_hobby.metadata.json
2020-05-27T15:47:52.552+0800    restoring test_jia.user_info from dump/test_jia/user_hobby.bson
2020-05-27T15:47:52.613+0800    no indexes to restore
2020-05-27T15:47:52.613+0800    finished restoring test_jia.user_info (1 document, 0 failures)
2020-05-27T15:47:52.613+0800    1 document(s) restored successfully. 0 document(s) failed to restore.

2.3、压缩备份文件,还原

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030 --gzip --db test_jia   dump/test_jia/
2020-05-27T15:54:05.013+0800    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2020-05-27T15:54:05.013+0800    building a list of collections to restore from dump/test_jia dir
2020-05-27T15:54:05.014+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json.gz
2020-05-27T15:54:05.014+0800    reading metadata for test_jia.user_hobby from dump/test_jia/user_hobby.metadata.json.gz
2020-05-27T15:54:05.038+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson.gz
2020-05-27T15:54:05.041+0800    no indexes to restore
2020-05-27T15:54:05.041+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T15:54:05.063+0800    restoring test_jia.user_hobby from dump/test_jia/user_hobby.bson.gz
2020-05-27T15:54:05.065+0800    no indexes to restore
2020-05-27T15:54:05.065+0800    finished restoring test_jia.user_hobby (1 document, 0 failures)
2020-05-27T15:54:05.065+0800    6 document(s) restored successfully. 0 document(s) failed to restore.

2.4、还原时删除已存在的集合

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030 --gzip  --drop --db test_jia  -c user_info  dump/test_jia/user_info.bson.gz
2020-05-27T15:57:18.065+0800    checking for collection data in dump/test_jia/user_info.bson.gz
2020-05-27T15:57:18.070+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json.gz
2020-05-27T15:57:18.085+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson.gz
2020-05-27T15:57:18.146+0800    no indexes to restore
2020-05-27T15:57:18.146+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T15:57:18.146+0800    5 document(s) restored successfully. 0 document(s) failed to restore.

2.5、还原设置并行集合数

默认并行集合数为4,我们可以设置该参数

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030 --gzip  --drop  dump/  -j 6
2020-05-27T16:15:17.370+0800    preparing collections to restore from
2020-05-27T16:15:17.375+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json.gz
2020-05-27T16:15:17.379+0800    reading metadata for test_jia.user_hobby from dump/test_jia/user_hobby.metadata.json.gz
2020-05-27T16:15:17.404+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson.gz
2020-05-27T16:15:17.407+0800    no indexes to restore
2020-05-27T16:15:17.407+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T16:15:17.412+0800    restoring test_jia.user_hobby from dump/test_jia/user_hobby.bson.gz
2020-05-27T16:15:17.413+0800    no indexes to restore
2020-05-27T16:15:17.413+0800    finished restoring test_jia.user_hobby (1 document, 0 failures)
2020-05-27T16:15:17.413+0800    6 document(s) restored successfully. 0 document(s) failed to restore.
[root@VM_6_17_centos ~]#

2.5、开启认证管理员恢复

[root@VM_6_17_centos ~]#  /root/mongodb-4.2.1/bin/mongorestore -h 192.168.6.17:27030 --gzip  --drop  --dir  dump/  -j 6    -u dba -p12387654  --authenticationDatabase=admin
2020-05-27T16:22:23.883+0800    preparing collections to restore from
2020-05-27T16:22:23.899+0800    reading metadata for test_jia.user_hobby from dump/test_jia/user_hobby.metadata.json.gz
2020-05-27T16:22:23.940+0800    restoring test_jia.user_hobby from dump/test_jia/user_hobby.bson.gz
2020-05-27T16:22:23.941+0800    reading metadata for test_jia.user_info from dump/test_jia/user_info.metadata.json.gz
2020-05-27T16:22:23.942+0800    no indexes to restore
2020-05-27T16:22:23.942+0800    finished restoring test_jia.user_hobby (1 document, 0 failures)
2020-05-27T16:22:23.964+0800    restoring test_jia.user_info from dump/test_jia/user_info.bson.gz
2020-05-27T16:22:23.965+0800    no indexes to restore
2020-05-27T16:22:23.966+0800    finished restoring test_jia.user_info (5 documents, 0 failures)
2020-05-27T16:22:23.966+0800    6 document(s) restored successfully. 0 document(s) failed to restore.

在数据量较小的情况下可是使用mongodump进行备份,数据大的情况下可以使用复制集的延迟备份,云服务器的磁盘快照进行备份。

关于增量oplog备份请点击以下链接。

Mongodb恢复到任意时间点