要从快照中恢复单个数据库或集合(或特定文档),可以使用可查询备份导出单个数据库或集合以恢复到目标部署。
Considerations
在尝试恢复之前,请确保目标部署的主机有足够的存储空间用于恢复文件和恢复的数据库,以及用于数据集增长的额外空间。使用 db.stats() 查找当前数据库大小。
重要
要从您自己的工作站连接到可查询备份实例,请使用最新版本的MongoDB Database Tools,其中包括mongodump和mongorestore,以及最新版本的mongosh。
旧版本的mongodump存在一个问题,可能会导致集合选项中的键按错误顺序转储。要学习;了解更多信息,请参阅 TOOLS-3411。
如果您在 本地模式下运行 Ops Manager,请安装针对 Ops Manager 版本在 数据库工具兼容性表中列出的版本,而不是最新可用版本。
从可查询备份恢复数据库或集合
You can use a queryable backup snapshot to export data for a database or a collection and restore to the target deployment. The following procedure connects to the queryable backup instance via Ops Manager-provided tunnel [1].
注意
如果守护程序在无法访问互联网的情况下运行,请参阅将部署配置为具有有限的互联网访问权限,安装相应的二进制文件。
打开可查询快照的备份隧道。
选择要查询的快照,然后单击 Next。
Start 查询快照的进程。您将收到双因素验证的提示。
Select Backup Tunnel as the connection method to the queryable snapshot. [1]
选择您的平台并进行下载。
解压缩下载的文件。
注意
等待挂载可查询快照,然后再继续。 快照挂载时间取决于快照的大小。 导航到Continuous Backup并选择Restore History标签页,验证快照是否已挂载。 挂载快照后, Status列会报告挂载操作的日期和时间。
打开终端或命令提示符,转到解压缩的<隧道>目录。运行可执行文件,启动隧道。
隧道的默认端口是
27017。要更改端口,请使用--local标志,如下例所示:./<tunnel executable> --local localhost:27020注意
如果您更改端口,则必须在连接时包含端口信息。
使用mongodump 从可查询备份中导出单个数据库或集合。
- 要导出数据库的数据,
- mongodump --port <port for tunnel> --db <single-database> --out <data-dump-path>
- 要导出集合的数据,
include the following options to connect to the tunnel [1]:
--port设置为隧道的端口。--db,设置为要导出的数据库的名称。--collection用于单个集合。
mongodump --port <port for tunnel> \ --db <single-database> \ --collection <collection-name> \ --out <data-dump-path>例子
要连接到在端口
27020上运行的隧道,以将restaurants集合中的数据从test数据库转储到/mydata/restoredata/目录,请执行以下操作:mongodump --port 27020 \ --db test \ --collection restaurants \ --out /mydata/restoredata/mongodump将restaurants集合数据输出到/mydata/restoredata/test/restaurants.bson文件中。
使用mongorestore 恢复单个数据库或集合。
- 要恢复单个数据库,
包括以下
mongorestore选项:或者,如果数据库已经存在,您可以添加
--drop,以删除目标集群中的数据库。
mongorestore --port <port> --db <destination database> <data-dump-path/database> --drop例如,要从 /mydata/restoredata/test 目录恢复到新数据库 restoredTest:
mongorestore --port 27017 --db restoredTest /mydata/restoredata/test --drop该示例假设目标副本集的主节点或目标分片集群的 mongos 监听端口 27017。
- 若要恢复单个集合,
包括以下
mongorestore选项:或者,如果集合已经存在,您可以添加
--drop以删除目标集群中的集合。
mongorestore --port <port> --db <destination database> --collection <collection-name> <data-dump-path/dbname/collection.bson> --drop例如,要从 /mydata/restoredata/test/restaurants.bson 数据文件恢复到 test2 数据库中的新集合 rest2:
mongorestore --port 27017 --db test2 --collection rest2 /mydata/restoredata/test/restaurants.bson --drop该示例假设目标副本集的主节点或目标分片集群的 mongos 监听端口 27017。
| [1] | (1, 2, 3, 4) Alternatively, instead of connecting via the tunnel, you can connect directly to the queryable backup, using the X.509 PEM files provided. If connecting directly to the queryable backup, you must specify the hostname and port, the TLS/SSL option, and the X.509 certificates. |