MongoDB 备份方式
- mongodump
- 系统快照(具体内容见官网:Back Up with Filesystem Snapshots)
- cp 或者 rsync
0.区别
区别 | mongodump/mongorestore | mongoexport/mongoimport |
---|---|---|
主要用途 | 数据备份小规模或部分或测试期间的数据/恢复 | 备份/恢复小型的MongoDB数据库 |
导出格式 | JSON/CSV | BSON |
指定导出 | 不支持导出单个db的所有collection | 支持导出单个db的所有collection |
注意事项 | 不要用于生产环境的备份 | 不适合备份/恢复大型MongoDB数据库 |
1.mongodump
下载mongdodump
https://www.mongodb.com/try/download/database-tools
mongodump参数
官方文档:https://docs.mongodb.com/v4.2/reference/program/mongodump/#bin.mongodump
https://www.mongodb.com/docs/database-tools/mongodump/#mongodb-binary-bin.mongodump
mongodump --help
mongodump --help
参数 | 介绍 |
---|---|
-h,--host | 127.0.0.1,当然也可以指定端口号:127.0.0.1:27017 |
-d,--db | 需要备份的数据库实例 |
-u,--username | 指定用户名 |
-o,--out | 备份的数据存放位置 |
-c,--collection | 指定表名字 |
-p,--password | 指定密码 |
--port | 指定端口 |
--archive | 备份到一个文件 |
--oplog | 使用oplog进行时间点快照 |
--authenticationDatabase | --authenticationDatabase |
[!WARNING]
WARNING
Failed: bad option: --out not allowed when --archive is specified -o, --out 不能和 --archive 同时指定
仅备份数据库中的文档,不备份索引,所以我们还原后,需要重新生成索引。
mongodump 备份过程中会对 mongod 服务的性能产生影响,我们建议在业务低峰期进行操作。如果我们备份的数据,大于系统内存,我们备份的时候容易出现错误
无用户密码dump
备份单个库
[root@slave01 ~]# mongodump -h 127.0.0.1 --port 27017 -d test -o /root/test
2021-12-14T02:18:29.459-0500 writing test.userdetails to /root/test/test/userdetails.bson
2021-12-14T02:18:29.460-0500 done dumping test.userdetails (1 document)
注意:
/root/test下test 目录必须为空,否则失败,没有自动创建test目录
[root@slave01 ~]# mongodump -h 127.0.0.1 --port 27017 -d test -o /root/test
2021-12-14T02:18:29.459-0500 writing test.userdetails to /root/test/test/userdetails.bson
2021-12-14T02:18:29.460-0500 done dumping test.userdetails (1 document)
注意:
/root/test下test 目录必须为空,否则失败,没有自动创建test目录
备份所有库
#不指定-d 参数,备份所有库,--oplog参数(的开启副本才行),这样的备份是基于某一时间点的快照,只能用于备份全部库时才可用,单库和单表不适用
[root@slave01 ~]# mongodump -h 127.0.0.1 --port 27017 -o /root/test
2021-12-14T02:49:52.383-0500 writing admin.system.users to /root/test/admin/system.users.bson
2021-12-14T02:49:52.384-0500 done dumping admin.system.users (3 documents)
2021-12-14T02:49:52.384-0500 writing admin.system.version to /root/test/admin/system.version.bson
2021-12-14T02:49:52.385-0500 done dumping admin.system.version (2 documents)
2021-12-14T02:49:52.385-0500 writing test.userdetails to /root/test/test/userdetails.bson
2021-12-14T02:49:52.386-0500 done dumping test.userdetails (1 document)
#不指定-d 参数,备份所有库,--oplog参数(的开启副本才行),这样的备份是基于某一时间点的快照,只能用于备份全部库时才可用,单库和单表不适用
[root@slave01 ~]# mongodump -h 127.0.0.1 --port 27017 -o /root/test
2021-12-14T02:49:52.383-0500 writing admin.system.users to /root/test/admin/system.users.bson
2021-12-14T02:49:52.384-0500 done dumping admin.system.users (3 documents)
2021-12-14T02:49:52.384-0500 writing admin.system.version to /root/test/admin/system.version.bson
2021-12-14T02:49:52.385-0500 done dumping admin.system.version (2 documents)
2021-12-14T02:49:52.385-0500 writing test.userdetails to /root/test/test/userdetails.bson
2021-12-14T02:49:52.386-0500 done dumping test.userdetails (1 document)
备份单个表
[root@slave01 test]# mongodump -h 127.0.0.1 --port 27017 -d test -c userdetails -o /root/test/test_userdetails
2021-12-14T02:32:36.415-0500 writing test.userdetails to /root/test/test_userdetails/test/userdetails.bson
2021-12-14T02:32:36.417-0500 done dumping test.userdetails (1 document)
[root@slave01 test]# mongodump -h 127.0.0.1 --port 27017 -d test -c userdetails -o /root/test/test_userdetails
2021-12-14T02:32:36.415-0500 writing test.userdetails to /root/test/test_userdetails/test/userdetails.bson
2021-12-14T02:32:36.417-0500 done dumping test.userdetails (1 document)
- 带密码备份
mongodump -u admin -p password -h 127.0.0.1 --port 27017 -d db_name -o /path/db_name
mongodump -u admin -p password -h 127.0.0.1 --port 27017 -d db_name -o /path/db_name
压缩备份
[root@slave01 ~]# mongodump -h 127.0.0.1 --port=27017 -d test --archive=/path/test.gz --gzip
2021-12-14T21:19:12.340-0500 writing test.user to archive 'test.gz'
2021-12-14T21:19:12.345-0500 writing test.userdetails to archive 'test.gz'
2021-12-14T21:19:12.347-0500 done dumping test.userdetails (1 document)
2021-12-14T21:19:12.347-0500 done dumping test.user (1 document)
[root@slave01 ~]# mongodump -h 127.0.0.1 --port=27017 -d test --archive=/path/test.gz --gzip
2021-12-14T21:19:12.340-0500 writing test.user to archive 'test.gz'
2021-12-14T21:19:12.345-0500 writing test.userdetails to archive 'test.gz'
2021-12-14T21:19:12.347-0500 done dumping test.userdetails (1 document)
2021-12-14T21:19:12.347-0500 done dumping test.user (1 document)
用户鉴定库备份
#参数 --authenticationDatabase admin ,admin 不可修改
[root@slave01 ~]# mongodump -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive=/root/test.gz --gzip
2021-12-15T00:49:40.824-0500 writing test.user to archive '/root/test.gz'
2021-12-15T00:49:40.827-0500 writing test.userdetails to archive '/root/test.gz'
2021-12-15T00:49:40.831-0500 done dumping test.user (1 document)
2021-12-15T00:49:40.836-0500 done dumping test.userdetails (1 document)
#参数 --authenticationDatabase admin ,admin 不可修改
[root@slave01 ~]# mongodump -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive=/root/test.gz --gzip
2021-12-15T00:49:40.824-0500 writing test.user to archive '/root/test.gz'
2021-12-15T00:49:40.827-0500 writing test.userdetails to archive '/root/test.gz'
2021-12-15T00:49:40.831-0500 done dumping test.user (1 document)
2021-12-15T00:49:40.836-0500 done dumping test.userdetails (1 document)
带有加密算法备份
#备份所有库
/data/apps/mongo_tools/bin/mongodump -u leihuo -p fa2e5474db854dd -h 172.16.195.193 --port 47017 --authenticationDatabase admin --authenticationMechanism=SCRAM-SHA-1 -o /root/yidu_mongo
#备份所有库
/data/apps/mongo_tools/bin/mongodump -u leihuo -p fa2e5474db854dd -h 172.16.195.193 --port 47017 --authenticationDatabase admin --authenticationMechanism=SCRAM-SHA-1 -o /root/yidu_mongo
生产环境备份
https://docs.mongodb.com/manual/core/backups/#back-up-with-mongodump
2.mongorestore
参数
参数 | 介绍 |
---|---|
--drop | 删除现有数据 |
-d,--dir | 指定要恢复的数据存放的文件夹 |
--archive | 指定恢复的文件名称 |
--excludeCollection | 不恢复指定集合 |
--excludeCollectionsWithPrefix | 不恢复匹配前缀的集合 |
[!WARNING] WARNING
Failed: stream or file does not appear to be a mongodump archive 可能是压缩过的文件,需指定 --gzip 参数
恢复压缩包库
#删除原有的库
[root@slave01 ~]# mongorestore -d test --drop --archive=test.gz --gzip
2021-12-14T21:24:55.685-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T21:24:55.695-0500 preparing collections to restore from
2021-12-14T21:24:55.715-0500 reading metadata for test.user from archive 'test.gz'
2021-12-14T21:24:55.715-0500 reading metadata for test.userdetails from archive 'test.gz'
2021-12-14T21:24:55.720-0500 dropping collection test.userdetails before restoring
2021-12-14T21:24:55.732-0500 restoring test.userdetails from archive 'test.gz'
2021-12-14T21:24:55.743-0500 finished restoring test.userdetails (1 document, 0 failures)
2021-12-14T21:24:55.743-0500 dropping collection test.user before restoring
2021-12-14T21:24:55.759-0500 restoring test.user from archive 'test.gz'
2021-12-14T21:24:55.771-0500 finished restoring test.user (1 document, 0 failures)
2021-12-14T21:24:55.771-0500 no indexes to restore for collection test.user
2021-12-14T21:24:55.771-0500 no indexes to restore for collection test.userdetails
2021-12-14T21:24:55.771-0500 2 document(s) restored successfully. 0 document(s) failed to restore.
#删除原有的库
[root@slave01 ~]# mongorestore -d test --drop --archive=test.gz --gzip
2021-12-14T21:24:55.685-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T21:24:55.695-0500 preparing collections to restore from
2021-12-14T21:24:55.715-0500 reading metadata for test.user from archive 'test.gz'
2021-12-14T21:24:55.715-0500 reading metadata for test.userdetails from archive 'test.gz'
2021-12-14T21:24:55.720-0500 dropping collection test.userdetails before restoring
2021-12-14T21:24:55.732-0500 restoring test.userdetails from archive 'test.gz'
2021-12-14T21:24:55.743-0500 finished restoring test.userdetails (1 document, 0 failures)
2021-12-14T21:24:55.743-0500 dropping collection test.user before restoring
2021-12-14T21:24:55.759-0500 restoring test.user from archive 'test.gz'
2021-12-14T21:24:55.771-0500 finished restoring test.user (1 document, 0 failures)
2021-12-14T21:24:55.771-0500 no indexes to restore for collection test.user
2021-12-14T21:24:55.771-0500 no indexes to restore for collection test.userdetails
2021-12-14T21:24:55.771-0500 2 document(s) restored successfully. 0 document(s) failed to restore.
恢复单个库
[root@slave01 test]# mongorestore -h 127.0.0.1 --port=27017 -d test /root/test/test
2021-12-14T04:22:35.064-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T04:22:35.064-0500 building a list of collections to restore from /root/test/test dir
2021-12-14T04:22:35.064-0500 reading metadata for test.userdetails from /root/test/test/userdetails.metadata.json
2021-12-14T04:22:35.065-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:22:35.065-0500 restoring test.userdetails from /root/test/test/userdetails.bson
2021-12-14T04:22:35.067-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:22:35.076-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:22:35.076-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:22:35.076-0500 0 document(s) restored successfully. 1 document(s) failed to restore.
[root@slave01 test]# mongorestore -h 127.0.0.1 --port=27017 -d test /root/test/test
2021-12-14T04:22:35.064-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T04:22:35.064-0500 building a list of collections to restore from /root/test/test dir
2021-12-14T04:22:35.064-0500 reading metadata for test.userdetails from /root/test/test/userdetails.metadata.json
2021-12-14T04:22:35.065-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:22:35.065-0500 restoring test.userdetails from /root/test/test/userdetails.bson
2021-12-14T04:22:35.067-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:22:35.076-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:22:35.076-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:22:35.076-0500 0 document(s) restored successfully. 1 document(s) failed to restore.
恢复单个表
#语法
mongorestore -h 127.0.0.1 --port 27017 -d db_name -c table_name /path/table_name.bson
[root@slave01 test]# mongorestore -h 127.0.0.1 --port 27017 -d test -c userdetails /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.339-0500 checking for collection data in /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.339-0500 reading metadata for test.userdetails from /root/test/test_userdetails/test/userdetails.metadata.json
2021-12-14T04:27:10.340-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:27:10.340-0500 restoring test.userdetails from /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.343-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:27:10.383-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:27:10.384-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:27:10.384-0500 0 document(s) restored successfully. 1 document(s) failed to restore.
#语法
mongorestore -h 127.0.0.1 --port 27017 -d db_name -c table_name /path/table_name.bson
[root@slave01 test]# mongorestore -h 127.0.0.1 --port 27017 -d test -c userdetails /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.339-0500 checking for collection data in /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.339-0500 reading metadata for test.userdetails from /root/test/test_userdetails/test/userdetails.metadata.json
2021-12-14T04:27:10.340-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:27:10.340-0500 restoring test.userdetails from /root/test/test_userdetails/test/userdetails.bson
2021-12-14T04:27:10.343-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:27:10.383-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:27:10.384-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:27:10.384-0500 0 document(s) restored successfully. 1 document(s) failed to restore.
只恢复部分表
#语法
--nsInclude="支持正则"
mongorestore -h 127.0.0.1 --port 27017 -d db_name --nsInclude="test.use*" /path/
[root@slave01 test]# mongorestore -h 127.0.0.1 --port 27017 -d test --nsInclude="test.user" /root/test/test/
2021-12-14T04:38:28.334-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T04:38:28.334-0500 building a list of collections to restore from /root/test/test dir
2021-12-14T04:38:28.334-0500 reading metadata for test.user from /root/test/test/user.metadata.json
2021-12-14T04:38:28.334-0500 reading metadata for test.userdetails from /root/test/test/userdetails.metadata.json
2021-12-14T04:38:28.335-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:38:28.335-0500 restoring test.userdetails from /root/test/test/userdetails.bson
2021-12-14T04:38:28.339-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:38:28.347-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:38:28.348-0500 restoring test.user from /root/test/test/user.bson
2021-12-14T04:38:28.360-0500 finished restoring test.user (1 document, 0 failures)
2021-12-14T04:38:28.360-0500 no indexes to restore for collection test.user
2021-12-14T04:38:28.360-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:38:28.360-0500 1 document(s) restored successfully. 1 document(s) failed to restore.
#语法
--nsInclude="支持正则"
mongorestore -h 127.0.0.1 --port 27017 -d db_name --nsInclude="test.use*" /path/
[root@slave01 test]# mongorestore -h 127.0.0.1 --port 27017 -d test --nsInclude="test.user" /root/test/test/
2021-12-14T04:38:28.334-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-14T04:38:28.334-0500 building a list of collections to restore from /root/test/test dir
2021-12-14T04:38:28.334-0500 reading metadata for test.user from /root/test/test/user.metadata.json
2021-12-14T04:38:28.334-0500 reading metadata for test.userdetails from /root/test/test/userdetails.metadata.json
2021-12-14T04:38:28.335-0500 restoring to existing collection test.userdetails without dropping
2021-12-14T04:38:28.335-0500 restoring test.userdetails from /root/test/test/userdetails.bson
2021-12-14T04:38:28.339-0500 continuing through error: E11000 duplicate key error collection: test.userdetails index: _id_ dup key: { _id: ObjectId('61b711f67978d27bed034272') }
2021-12-14T04:38:28.347-0500 finished restoring test.userdetails (0 documents, 1 failure)
2021-12-14T04:38:28.348-0500 restoring test.user from /root/test/test/user.bson
2021-12-14T04:38:28.360-0500 finished restoring test.user (1 document, 0 failures)
2021-12-14T04:38:28.360-0500 no indexes to restore for collection test.user
2021-12-14T04:38:28.360-0500 no indexes to restore for collection test.userdetails
2021-12-14T04:38:28.360-0500 1 document(s) restored successfully. 1 document(s) failed to restore.
恢复到新库
[root@slave01 ~]# mongodump -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive="mongodump-test-db"
[root@slave01 ~]# mongorestore -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'
2021-12-15T00:58:48.555-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-15T00:58:48.575-0500 preparing collections to restore from
2021-12-15T00:58:48.586-0500 reading metadata for examples.user from archive 'mongodump-test-db'
2021-12-15T00:58:48.587-0500 reading metadata for examples.userdetails from archive 'mongodump-test-db'
2021-12-15T00:58:48.603-0500 restoring examples.userdetails from archive 'mongodump-test-db'
2021-12-15T00:58:48.614-0500 finished restoring examples.userdetails (1 document, 0 failures)
2021-12-15T00:58:48.627-0500 restoring examples.user from archive 'mongodump-test-db'
2021-12-15T00:58:48.638-0500 finished restoring examples.user (1 document, 0 failures)
2021-12-15T00:58:48.638-0500 no indexes to restore for collection examples.user
2021-12-15T00:58:48.638-0500 no indexes to restore for collection examples.userdetails
2021-12-15T00:58:48.638-0500 2 document(s) restored successfully. 0 document(s) failed to restore.
#查看数据库
> use admin
switched to db admin
> db.auth("admin","123456")
1
> show dbs;
admin 0.000GB
config 0.000GB
examples 0.000GB ----->新的数据库
local 0.000GB
test 0.000GB
[root@slave01 ~]# mongodump -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive="mongodump-test-db"
[root@slave01 ~]# mongorestore -u admin -p 123456 -h 127.0.0.1 --port=27017 --authenticationDatabase admin -d test --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'
2021-12-15T00:58:48.555-0500 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-12-15T00:58:48.575-0500 preparing collections to restore from
2021-12-15T00:58:48.586-0500 reading metadata for examples.user from archive 'mongodump-test-db'
2021-12-15T00:58:48.587-0500 reading metadata for examples.userdetails from archive 'mongodump-test-db'
2021-12-15T00:58:48.603-0500 restoring examples.userdetails from archive 'mongodump-test-db'
2021-12-15T00:58:48.614-0500 finished restoring examples.userdetails (1 document, 0 failures)
2021-12-15T00:58:48.627-0500 restoring examples.user from archive 'mongodump-test-db'
2021-12-15T00:58:48.638-0500 finished restoring examples.user (1 document, 0 failures)
2021-12-15T00:58:48.638-0500 no indexes to restore for collection examples.user
2021-12-15T00:58:48.638-0500 no indexes to restore for collection examples.userdetails
2021-12-15T00:58:48.638-0500 2 document(s) restored successfully. 0 document(s) failed to restore.
#查看数据库
> use admin
switched to db admin
> db.auth("admin","123456")
1
> show dbs;
admin 0.000GB
config 0.000GB
examples 0.000GB ----->新的数据库
local 0.000GB
test 0.000GB
3.cp 或者 rsync
可以直接复制数据文件,但是我们必须在复制文件前停止对 MongoDB 的操作,否则我们复制的文件是无效的
4.查看备份数据
部分的数据都是二进制的,我们直接查看是查看不到的,那么我们可以通过工具 bsondump
[root@slave01 test]# bsondump user.bson
{"_id":{"$oid":"61b864e1e463d036cacdca76"},"id":{"$numberDouble":"1.0"},"name":"jack","age":{"$numberDouble":"73.0"}}
2021-12-14T21:50:37.923-0500 1 objects found
[root@slave01 test]# bsondump user.bson
{"_id":{"$oid":"61b864e1e463d036cacdca76"},"id":{"$numberDouble":"1.0"},"name":"jack","age":{"$numberDouble":"73.0"}}
2021-12-14T21:50:37.923-0500 1 objects found
mongodb 使用mongodump备份 指定用户名密码 出现错误
[root@MongoDB ~]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /root/
[root@MongoDB ~]# mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /root/
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
解决方式:
mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /tmp/mongobak --authenticationDatabase admin
Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed
解决方式:
mongodump --host 127.0.0.1 --port 27017 -u admin -p 123456 -d db1 -o /tmp/mongobak --authenticationDatabase admin
mongorestore -h 8.210.164.8:27017 -u whp -p whp123 -d db_name /path/db_name
mongorestore -h 8.210.164.8:27017 -u whp -p whp123 -d db_name /path/db_name
5.mongoexport
mongoexport --help
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase admin
没有用户名密码的导出:
> mongoexport --db=sales --collection=contacts --out=contacts.json
带有用户名密码的导出:
> mongoexport --port 27017 -u root -p 123456 --authenticationDatabase testdb --collection=info --db=testdb --out=/opt/backup-data/info.json
带有过滤条件的导出
> mongoexport --db=sales --collection=contacts -q '{"field":"fieldValue"}' --out=contacts.json
说明:-q 的参数key也一定要有双引号引起来,要不然会出现以下错误
Failed: error parsing query as Extended JSON: invalid JSON input
mongoexport --help
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase admin
没有用户名密码的导出:
> mongoexport --db=sales --collection=contacts --out=contacts.json
带有用户名密码的导出:
> mongoexport --port 27017 -u root -p 123456 --authenticationDatabase testdb --collection=info --db=testdb --out=/opt/backup-data/info.json
带有过滤条件的导出
> mongoexport --db=sales --collection=contacts -q '{"field":"fieldValue"}' --out=contacts.json
说明:-q 的参数key也一定要有双引号引起来,要不然会出现以下错误
Failed: error parsing query as Extended JSON: invalid JSON input
6.mongoimport
mongoimport --help
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
-j:--numInsertionWorkers=<number> number of insert operations to run concurrently
没有用户名密码的导入:
> mongoimport --db=users --collection=contacts --file=contacts.json
带有用户名密码的导入:
> mongoimport --port 27017 -u root -p 123456 --authenticationDatabase testdb -d testdb -c info --file=/opt/backup-data/info.json
说明:-f /opt/backup-data/info.json 这样传文件参数是不行的,指定文件一定需要使用--file参数
mongoimport --help
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
-j:--numInsertionWorkers=<number> number of insert operations to run concurrently
没有用户名密码的导入:
> mongoimport --db=users --collection=contacts --file=contacts.json
带有用户名密码的导入:
> mongoimport --port 27017 -u root -p 123456 --authenticationDatabase testdb -d testdb -c info --file=/opt/backup-data/info.json
说明:-f /opt/backup-data/info.json 这样传文件参数是不行的,指定文件一定需要使用--file参数
7.时间点恢复
备份--oplog
[root@master bin]# ./mongodump -h 127.0.0.1 --port 28017 --oplog --gzip -o /root/mongo_back/test
2021-12-26T22:34:36.522-0500 writing admin.system.version to
2021-12-26T22:34:36.522-0500 done dumping admin.system.version (1 document)
2021-12-26T22:34:36.523-0500 writing test.new10 to
2021-12-26T22:34:36.524-0500 done dumping test.new10 (1 document)
2021-12-26T22:34:36.524-0500 writing captured oplog to
2021-12-26T22:34:36.525-0500 dumped 1 oplog entry
#验证方式
./mongodump -h 127.0.0.1 --port 28017 --oplog --authenticationDatabase admin -u 用户名 -p密码 --gzip -o /data/mongodb_back/mongotestdump
[root@master bin]# ./mongodump -h 127.0.0.1 --port 28017 --oplog --gzip -o /root/mongo_back/test
2021-12-26T22:34:36.522-0500 writing admin.system.version to
2021-12-26T22:34:36.522-0500 done dumping admin.system.version (1 document)
2021-12-26T22:34:36.523-0500 writing test.new10 to
2021-12-26T22:34:36.524-0500 done dumping test.new10 (1 document)
2021-12-26T22:34:36.524-0500 writing captured oplog to
2021-12-26T22:34:36.525-0500 dumped 1 oplog entry
#验证方式
./mongodump -h 127.0.0.1 --port 28017 --oplog --authenticationDatabase admin -u 用户名 -p密码 --gzip -o /data/mongodb_back/mongotestdump
还原--oplogReplay
[root@master bin]# ./mongorestore -h 127.0.0.1 --port 28017 --oplogReplay --gzip /root/mongo_back/test
2021-12-26T22:41:59.062-0500 preparing collections to restore from
2021-12-26T22:41:59.064-0500 reading metadata for test.new10 from /root/mongo_back/test/test/new10.metadata.json.gz
2021-12-26T22:41:59.088-0500 restoring test.new10 from /root/mongo_back/test/test/new10.bson.gz
2021-12-26T22:41:59.094-0500 no indexes to restore
2021-12-26T22:41:59.094-0500 finished restoring test.new10 (1 document)
2021-12-26T22:41:59.094-0500 replaying oplog
2021-12-26T22:41:59.094-0500 done
#验证方式
./mongorestore -h 127.0.0.1 --port 端口 --oplogReplay --authenticationDatabase admin -u 用户名 -p 密码 --gzip /root/mongo_back/test
[root@master bin]# ./mongorestore -h 127.0.0.1 --port 28017 --oplogReplay --gzip /root/mongo_back/test
2021-12-26T22:41:59.062-0500 preparing collections to restore from
2021-12-26T22:41:59.064-0500 reading metadata for test.new10 from /root/mongo_back/test/test/new10.metadata.json.gz
2021-12-26T22:41:59.088-0500 restoring test.new10 from /root/mongo_back/test/test/new10.bson.gz
2021-12-26T22:41:59.094-0500 no indexes to restore
2021-12-26T22:41:59.094-0500 finished restoring test.new10 (1 document)
2021-12-26T22:41:59.094-0500 replaying oplog
2021-12-26T22:41:59.094-0500 done
#验证方式
./mongorestore -h 127.0.0.1 --port 端口 --oplogReplay --authenticationDatabase admin -u 用户名 -p 密码 --gzip /root/mongo_back/test
指定时间点还原
#查看此时间点对应的时间和操作序列,在源库local中的oplog.rs 集合查询
>use local
>db.oplog.rs.find({"o._id": ObjectId("61c9275b5745d52b72d861c5")}).sort({ts:-1}).limit(1);
./mongorestore -h 127.0.0.1 --port 28017 --oplogReplay --oplogLimit "1527736438:858" --authenticationDatabase admin -u 用户名 -p 密码 /root/mongo_back/test
#查看此时间点对应的时间和操作序列,在源库local中的oplog.rs 集合查询
>use local
>db.oplog.rs.find({"o._id": ObjectId("61c9275b5745d52b72d861c5")}).sort({ts:-1}).limit(1);
./mongorestore -h 127.0.0.1 --port 28017 --oplogReplay --oplogLimit "1527736438:858" --authenticationDatabase admin -u 用户名 -p 密码 /root/mongo_back/test
8.集群备份
mongoexport备份
备份所有
#退出mongos连接,到命令行界面执行备份
mongoexport -h 10.0.0.40 --port 28017 -d olda -c student -o /tmp/mongodb_backup.json
#退出mongos连接,到命令行界面执行备份
mongoexport -h 10.0.0.40 --port 28017 -d olda -c student -o /tmp/mongodb_backup.json
指定列备份
#退出mongos连接,到命令行界面执行备份(要指定数据列的键)
#注意:想要备份哪一列,那就指定哪一列
mongoexport -h 10.0.0.40 --port 28017 -d oldb -c student --type=csv -f id,name,age,date -o /tmp/mongodb_backup.csv
#退出mongos连接,到命令行界面执行备份(要指定数据列的键)
#注意:想要备份哪一列,那就指定哪一列
mongoexport -h 10.0.0.40 --port 28017 -d oldb -c student --type=csv -f id,name,age,date -o /tmp/mongodb_backup.csv
mongoimport恢复
恢复所有
mongodb恢复不会覆盖原有的数据,当前测试环境会产生数据冲突。
所以恢复数据到其他的表中(也可以直接恢复到其他 "库" )
mongoimport -h 10.0.0.40 --port 28017 -d olda -c student_source /tmp/mongodb_backup.json
mongodb恢复不会覆盖原有的数据,当前测试环境会产生数据冲突。
所以恢复数据到其他的表中(也可以直接恢复到其他 "库" )
mongoimport -h 10.0.0.40 --port 28017 -d olda -c student_source /tmp/mongodb_backup.json
恢复指定列
要指定第一列的id,name,age,date不为数据,mongodb没有数据格式束缚
本次恢复数据到wsp与wll库
#csv格式的文件第一行,没有数据对应列的名字(导入wsp库)
mongoimport -h 10.0.0.40 --port 28017 -d wsp -c student --type=csv -f id,name,age,date --file /tmp/mongodb_backup.csv
要指定第一列的id,name,age,date不为数据,mongodb没有数据格式束缚
本次恢复数据到wsp与wll库
#csv格式的文件第一行,没有数据对应列的名字(导入wsp库)
mongoimport -h 10.0.0.40 --port 28017 -d wsp -c student --type=csv -f id,name,age,date --file /tmp/mongodb_backup.csv
#csv格式的文件第一行,有数据对应列的名字(导入wll库)
#--headerline:指明第一行是列名,不需要导入。
mongoimport -h 10.0.0.40 --port 28017 -d wll -c student --type=csv --headerline --file /tmp/mongodb_backup.csv
#csv格式的文件第一行,有数据对应列的名字(导入wll库)
#--headerline:指明第一行是列名,不需要导入。
mongoimport -h 10.0.0.40 --port 28017 -d wll -c student --type=csv --headerline --file /tmp/mongodb_backup.csv
dump
- mongos节点
备份所有
mkdir /tmp/backup
mongodump -h 10.0.0.40 --port 28017 -o /tmp/backup/
mkdir /tmp/backup
mongodump -h 10.0.0.40 --port 28017 -o /tmp/backup/
备份指定库
mongodump -h 10.0.0.40 --port 28017 -d world -o /tmp/backup/
mongodump -h 10.0.0.40 --port 28017 -d world -o /tmp/backup/
备份指定表
mongodump -h 10.0.0.40 --port 28017 -d world -c city -o /tmp/backup/
mongodump -h 10.0.0.40 --port 28017 -d world -c city -o /tmp/backup/
恢复
#加了--drop参数会自动覆盖原有数据,不需要删除原有数据
mongorestore -h 10.0.0.40 --port 28017 -d world -c city --drop /tmp/backup/world/city.bson
#加了--drop参数会自动覆盖原有数据,不需要删除原有数据
mongorestore -h 10.0.0.40 --port 28017 -d world -c city --drop /tmp/backup/world/city.bson