对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

恢复单个数据库或集合

要从快照中恢复单个数据库或集合(或特定文档),可以使用可查询备份导出单个数据库或集合以恢复到目标部署。

在尝试恢复之前,请确保目标部署的主机有足够的存储空间用于恢复文件和恢复的数据库,以及用于数据集增长的额外空间。使用 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].

注意

如果守护程序在无法访问互联网的情况下运行,请参阅将部署配置为具有有限的互联网访问权限,安装相应的二进制文件。

1

如果您想查询某个部署的备份,在 Options 列下单击 ,然后选择 Query。

您还可以单击部署以查看其快照,然后单击 Actions(操作)列下的Query(查询)按钮。

2
  1. 选择要查询的快照,然后单击 Next。

  2. Start 查询快照的进程。您将收到双因素验证的提示。

  3. Select Backup Tunnel as the connection method to the queryable snapshot. [1]

  4. 选择您的平台并进行下载。

  5. 解压缩下载的文件。

    注意

    等待挂载可查询快照,然后再继续。 快照挂载时间取决于快照的大小。 导航到Continuous Backup并选择Restore History标签页,验证快照是否已挂载。 挂载快照后, Status列会报告挂载操作的日期和时间。

  6. 打开终端或命令提示符,转到解压缩的<隧道>目录。运行可执行文件,启动隧道。

    隧道的默认端口是 27017。要更改端口,请使用 --local 标志,如下例所示:

    ./<tunnel executable> --local localhost:27020

    注意

    如果您更改端口,则必须在连接时包含端口信息。

3

要导出数据库的数据,

包括以下 mongodump 选项以连接到隧道 [1]:

  • --port 设置为隧道的端口

  • --db ,设置为要导出的数据库的名称

  • --out 设置为一个空目录,以输出数据转储。

    重要

    确保运行 mongodump 的用户可以写入指定目录。

mongodump --port <port for tunnel> --db <single-database> --out <data-dump-path>

例子

要连接到在端口 27020 上运行的隧道以将数据从test数据库转储到/mydata/restoredata/目录:

mongodump --port 27020 --db test --out /mydata/restoredata/

mongodump 会将 test 数据库文件输出到 /mydata/restoredata/test/ 目录中。

如果 mongodump 不在您的 PATH 中,请指定工具的路径。

要导出集合的数据,

include the following options to connect to the tunnel [1]:

  • --port 设置为隧道的端口。

  • --db ,设置为要导出的数据库的名称。

  • --collection 用于单个集合。

  • --out 设置为一个空目录,以输出数据转储。

    重要

    确保运行 mongodump 的用户可以写入指定目录。

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 文件中。

4

要恢复单个数据库,

包括以下 mongorestore 选项:

  • --port ,设置为目标集群的端口。

  • --db 设置为目标数据库的名称。

或者,如果数据库已经存在,您可以添加 --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 选项:

  • --port ,设置为目标集群的端口。

  • --db 设置为目标数据库的名称。

或者,如果集合已经存在,您可以添加 --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。

5

完成后,可以终止可查询实例:

  1. 转到 Restore History 并将鼠标悬停在部署项的 Status(状态)列上。

  2. 单击 Cancel(连接)。

[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.

重要

恢复使用 AES256-GCM 加密的快照后轮换主密钥

如果恢复 MongoDB Ops Manager 使用 AES256 -GCM 加密的 加密快照 ,请在完成恢复后 轮换主密钥 。