要从快照中恢复单个数据库或集合(或特定文档),可以使用可查询备份导出单个数据库或集合以恢复到目标部署。
Considerations
在尝试恢复之前,请确保目标部署的主机有足够的存储空间用于恢复文件和恢复的数据库,以及用于数据集增长的额外空间。使用 db.stats() 查找当前数据库大小。
重要
使用最新版本的MongoDB Database Tools ,其中包括mongodump和mongorestore 。 使用最新版本的mongosh 。
旧版本的mongodump存在一个问题,可能会导致集合选项中的键按错误顺序转储。要学习;了解更多信息,请参阅 TOOLS-3411。
从可查询备份恢复数据库或集合
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 Cloud Manager-provided tunnel [1].
打开可查询快照的备份隧道。
选择要查询的快照,然后单击 Next。
Start 查询快照的进程。您将收到双因素验证的提示。
Select Backup Tunnel as the connection method to the queryable snapshot. [1]
选择您的平台并进行下载。
解压缩下载的文件。
注意
等待挂载可查询快照,然后再继续。 快照挂载时间取决于快照的大小。 要验证快照是否已挂载,请执行以下操作:
在MongoDB Cloud Manager中, Go项目的 Continuous Backup 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 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. |