文档菜单
文档首页
/
MongoDB数据库工具
/

mongorestore示例

本页内容

  • 使用访问控制恢复
  • 恢复集合
  • 使用通配符恢复集合
  • 在恢复期间更改集合命名空间
  • 复制/克隆数据库
  • 从存档文件恢复
  • 从压缩数据恢复
  • 恢复时间序列集合
  • 使用AWS IAM凭据连接到MongoDB Atlas集群
  • 创建并恢复一致的备份文件
  • 了解更多

此页面展示了以下示例mongorestore.

从系统命令行运行 mongorestore,而不是从mongo 壳中。

以下示例中,mongorestore/opt/backup/mongodump-2011-10-24 恢复到运行在主机 mongodb1.example.net 的端口 27017 上的 mongod 实例。--uri 字符串省略了用户密码,以便 mongorestore 提示输入密码。

mongorestore --uri="mongodb://user@mongodb1.example.net:27017/?authSource=admin" /opt/backup/mongodump-2011-10-24

或者,您可以使用 --host--port--username--authenticationDatabase 来指定主机、端口、用户名和身份验证数据库。省略 --password 以使 mongorestore 提示输入密码。

mongorestore --host=mongodb1.example.net --port=27017 --username=user --authenticationDatabase=admin /opt/backup/mongodump-2011-10-24

要恢复特定集合,请使用 --nsInclude,传入集合的完整命名空间(<数据库>.<集合>)。

以下示例将数据库 test 中名为 purchaseorders 的集合从位于 dump/ 目录中的对应文件恢复。

mongorestore --nsInclude=test.purchaseorders dump/

mongorestore 输出结果,包括恢复的文档数量。

2019-06-28T19:23:42.858-0400 preparing collections to restore from
2019-06-28T19:23:42.858-0400 reading metadata for test.purchaseorders from dump/test/purchaseorders.metadata.json
2019-06-28T19:23:42.893-0400 restoring test.purchaseorders from dump/test/purchaseorders.bson
2019-06-28T19:23:42.896-0400 restoring indexes for collection test.purchaseorders from metadata
2019-06-28T19:23:42.991-0400 finished restoring test.purchaseorders (6 documents, 0 failures)
2019-06-28T19:23:42.991-0400 6 document(s) restored successfully. 0 document(s) failed to restore.

如果 dump/ 目录不包含指定命名空间的对应数据文件,则不会恢复任何数据。

2019-07-08T14:39:57.121-0400. preparing collections to restore from
2019-07-08T14:39:57.121-0400 0 document(s) restored successfully. 0 document(s) failed to restore.

或者,您可以使用 --db--collection 和一个 .bson 文件来恢复特定的集合。

mongorestore --db=test --collection=purchaseorders dump/test/purchaseorders.bson
2019-06-30T12:21:44.777-0400 checking for collection data in dump/test/purchaseorders.bson
2019-06-30T12:21:44.779-0400 reading metadata for test.purchaseorders from dump/test/purchaseorders.metadata.json
2019-06-30T12:21:44.813-0400 restoring test.purchaseorders from dump/test/purchaseorders.bson
2019-06-30T12:21:44.881-0400 restoring indexes for collection test.purchaseorders from metadata
2019-06-30T12:21:44.987-0400 finished restoring test.purchaseorders (6 documents, 0 failures)
2019-06-30T12:21:44.987-0400 6 document(s) restored successfully. 0 document(s) failed to restore.

--nsInclude--nsExclude 支持使用星号作为 通配符 来指定您希望在恢复操作中包含或排除的命名空间。

以下示例恢复当前目录中 dump/ 子目录下匹配指定命名空间模式的文档。--nsInclude 语句指定只恢复 transactions 数据库中的文档,而 --nsExclude 指示 mongorestore 排除以 _dev 结尾的集合。 mongorestore 将数据恢复到运行在本机接口上端口号为 27017mongod 实例。

mongorestore --nsInclude='transactions.*' --nsExclude='transactions.*_dev' dump/

要更改您正在恢复的集合的命名空间,请使用 --nsFrom--nsTo 选项。

--nsFrom--nsTo 选项支持使用星号作为通配符 以及 使用美元符号来界定“通配符”变量,用于替换。

考虑一个数据库 data,您已使用 mongodump 将其导出到 dump/ 目录。该 data 数据库包含以下集合

  • sales_customer1

  • sales_customer2

  • sales_customer3

  • users_customer1

  • users_customer2

  • users_customer3

使用 --nsFrom--nsTo,您可以将数据恢复到不同的命名空间。以下操作

  • data 数据库中的 sales_<customerName> 集合恢复到 <customerName> 数据库中的 sales 集合中,并且

  • data 数据库中的 users_<customerName> 集合恢复到 <customerName> 数据库中的 users 集合中。

mongorestore --nsInclude="data.*" --nsFrom="data.$prefix$_$customer$" --nsTo="$customer$.$prefix$"

从版本 4.2 开始,MongoDB 移除了已弃用的 copydb 命令和 clone 命令。

作为替代方案,用户可以使用 mongodumpmongorestore(使用 mongorestore 选项 --nsFrom--nsTo)。

例如,要将本地实例(默认端口 27017)上的 test 数据库复制到同一实例上的 examples 数据库中,您可以

  1. 使用 mongodumptest 数据库导出到存档 mongodump-test-db

    mongodump --archive="mongodump-test-db" --db=test
  2. 使用带有 --nsFrom--nsTomongorestore 从存档恢复(更改数据库名称)

    mongorestore --archive="mongodump-test-db" --nsFrom="test.*" --nsTo="examples.*"

提示

根据需要包括其他选项,例如指定 uri 或主机、用户名、密码和认证数据库。

要从归档文件恢复,请运行带有新的 --archive 选项和归档文件名的 mongorestore

mongorestore --archive=test.20150715.archive

要从归档文件恢复,请运行带有新的 --archive 选项和归档文件名的 mongorestore。例如,以下操作从文件 test.20150715.archive 恢复 test 数据库。

mongorestore --archive=test.20150715.archive --nsInclude="test.*"

mongorestore 可以从由 mongodump 创建的压缩文件或压缩归档文件中恢复。

要从包含压缩文件的转储目录恢复,请使用带有--gzip选项的mongorestore命令。例如,以下操作从默认的dump目录中压缩文件恢复test数据库。

mongorestore --gzip --nsInclude="test.*" dump/

要从压缩的存档文件恢复,请使用带有--gzip选项和--archive选项的mongorestore命令。例如,以下操作从存档文件test.20150715.gz恢复test数据库。

mongorestore --gzip --archive=test.20150715.gz --nsInclude="test.*"

要更改您正在恢复的集合的命名空间,请使用带有--gzip选项的--nsFrom--nsTo选项。

mongorestore --gzip --nsFrom="data.$prefix$_$customer$" --nsTo="$customer$.$prefix$"

使用mongosh创建时间序列集合。以下示例使用默认的test数据库

db.createCollection(
"weather",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "hours"
}
}
)

将时间序列文档插入到集合中

db.weather.insertMany( [
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T00:00:00.000Z"),
"temp": 12
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T04:00:00.000Z"),
"temp": 11
},
{
"metadata": { "sensorId": 5578, "type": "temperature" },
"timestamp": ISODate("2021-05-18T08:00:00.000Z"),
"temp": 11
}
] )

从您的终端,使用mongodump将时间序列集合导出到dump/test目录。此命令将system.buckets.weather.bsonweather.metadata.json添加到目录

mongodump --db=test

使用mongorestore将数据恢复到mongorestore.weather命名空间

mongorestore --host localhost --port 27017 --nsFrom="test.*" --nsTo="mongorestore.*" dump/

注意

您不能单独恢复system.buckets.weather.bson文件。尝试这样做会导致错误。

版本100.1.0.

要连接到一个已配置为通过 MongoDB Atlas 集群支持身份验证的 AWS IAM 凭据,请提供类似以下格式的 连接字符串mongorestore

mongorestore 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' <other options>

使用这种方式通过 AWS IAM 凭据连接到 Atlas,将使用 MONGODB-AWS 认证机制$external authSource,如下例所示。

如果使用 AWS 会话令牌,也请提供相应的 AWS_SESSION_TOKEN authMechanismProperties 值,如下所示

mongorestore 'mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>' <other options>

注意

如果 AWS 访问密钥 ID、秘密访问密钥或会话令牌包含以下字符

: / ? # [ ] @

这些字符必须使用百分编码进行转换。

或者,可以使用--username--password--awsSessionToken选项在连接字符串外部提供AWS访问密钥ID、秘密访问密钥和可选的会话令牌,例如

mongorestore 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' --username <aws access key id> --password <aws secret access key> --awsSessionToken <aws session token> <other options>

当作为命令行参数提供时,这三个选项不需要百分编码。

您还可以使用标准的AWS IAM环境变量在这些平台上设置凭证。mongorestore在您使用MONGODB-AWS 身份验证机制时检查以下环境变量:

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_SESSION_TOKEN

如果已设置,则不需要在连接字符串或通过其显式选项中指定这些凭证。

注意

如果您选择使用AWS环境变量来指定这些值,则不能与这些凭证的相应显式或连接字符串选项混合使用。要么使用环境变量来指定访问密钥ID和秘密访问密钥(如果使用会话令牌),要么使用显式或连接字符串选项分别指定这些值。

以下示例在bash shell中设置这些环境变量

export AWS_ACCESS_KEY_ID='<aws access key id>'
export AWS_SECRET_ACCESS_KEY='<aws secret access key>'
export AWS_SESSION_TOKEN='<aws session token>'

在其他shell中设置环境变量的语法将不同。请参阅您平台的相关文档以获取更多信息。

您可以使用以下命令验证这些环境变量是否已设置

env | grep AWS

设置后,以下示例将使用这些环境变量连接到MongoDB Atlas集群

mongorestore 'mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS' <other options>

要使用oplog条目创建一个一致的mongodump备份文件,请使用mongodump --oplog选项。要从备份文件中恢复数据,请使用mongorestore --oplogReplay选项。

oplog包含数据库写入操作的历史记录。

mongodump输出

  • 集合文档、元数据和选项。

  • 索引定义。

  • 如果在mongodump运行期间指定了--oplog,则输出运行期间发生的写入。

mongodump --oplogmongodump输出目录的最高层创建一个名为oplog.bson的文件。该文件包含在mongodump运行期间发生的写入操作。在mongodump完成后发生的写入不会记录在该文件中。

有关使用mongodump备份分片集群的信息,请参阅使用数据库转储备份自管理的分片集群。

要从 oplog.bson 文件恢复 oplog 条目,请使用 mongorestore --oplogReplay。使用 mongodump --oplogmongorestore --oplogReplay 一起使用,以确保数据库是最新的并且包含在 mongodump 运行期间发生的所有写入。

后退

行为