本页显示 mongorestore的示例。
从系统命令行运行 mongorestore,而非 mongo shell。
使用访问控制进行恢复
在以下示例中, mongorestore从/opt/backup/mongodump-2011-10-24恢复到托管mongodb1.example.net上端口27017上运行的mongod实例。 --uri string省略了用户的密码,以让 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 来传入该集合的完整命名空间 (<database>.<collection>)。
下面的示例从位于 dump/ 目录中的相应文件中还原数据库 test 中名为 purchaseorders 的集合。
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 将数据恢复至端口 27017 上本地主机接口上运行的 mongod 实例中。
mongorestore --nsInclude='transactions.*' --nsExclude='transactions.*_dev' dump/
恢复期间更改集合命名空间
如要更改正在恢复的集合的命名空间,请使用 --nsFrom 和 --nsTo 选项。
--nsFrom 和 --nsTo 选项支持使用星号作为通配符,并且支持使用美元符号来分隔“通配符”变量以用于替换。
以您使用 mongodump 导出到 dump/ 目录的数据库 data 为例。data 数据库包含以下集合:
sales_customer1sales_customer2sales_customer3users_customer1users_customer2users_customer3
使用 --nsFrom 和 --nsTo,您可以将数据恢复到不同的命名空间中。以下操作
将
data数据库中的sales_<customerName>集合恢复为<customerName>数据库中的sales集合,并且将
users_<customerName>集合恢复为<customerName>数据库中的users集合。
mongorestore --nsInclude="data.*" --nsFrom="data.$prefix$_$customer$" --nsTo="$customer$.$prefix$"
复制/克隆数据库
从版本 4.2 开始,MongoDB 删除了已弃用的 copydb 命令和clone 命令。
此外,用户也可以使用 mongodump 和 mongorestore(使用 mongorestore 选项 --nsFrom 和 --nsTo)。
例如,要将 test 数据库从运行在默认端口 27017 上的本地实例复制到同一个实例上的 examples 数据库,可以:
使用
mongodump将test数据库转储到存档mongodump-test-db:mongodump --archive="mongodump-test-db" --db=test 使用
mongorestore与--nsFrom和--nsTo从存档中恢复(数据库名称发生变更):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.*"
要更改正在恢复的集合的命名空间,请使用 --nsFrom 和 --nsTo 选项以及 --gzip 选项。
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.bson 和 weather.metadata.json 添加到目录:
mongodump --db=test
使用 mongorestore 将数据恢复到 mongorestore.weather 命名空间:
mongorestore --host localhost --port 27017 --nsFrom="test.*" --nsTo="mongorestore.*" dump/
注意
您无法单独恢复 system.buckets.weather.bson 文件。尝试这样做会导致错误。
使用 AWS IAM 档案连接到 MongoDB Atlas 集群
100.1.0 版本新增。
要连接到已配置为支持通过Amazon Web Services IAM凭证 进行身份验证的MongoDB Atlas 群集,请向connection string 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 authentication mechanism 和 $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>
注意
或者,可以使用 --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>
当作为命令行参数提供时,这三个选项无需进行百分号编码。
您还可以使用标准Amazon Web Services IAM 环境变量在您的平台上设立这些凭证。当您使用 MONGODB-AWS authentication mechanism: 时,mongorestore 会检查以下环境变量:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_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 输出:
收集文档、元数据和选项。
索引定义。
如果指定了
--oplog,则在mongodump运行期间发生的写入。
将 mongodump 与 oplog 选项结合使用
mongodump --oplog 会在mongodump输出目录的顶层创建一个名为oplog.bson的文件。 该文件包含mongodump运行期间发生的写入操作。 mongodump完成后发生的写入不会记录在文件中。
要使用mongodump备份分分片的集群,请参阅使用数据库转储备份自管理分片集群。
使用 mongorestore 和 oplogReplay 选项
要从 oplog.bson 文件中恢复 oplog 条目,请使用 mongorestore --oplogReplay。将 mongodump --oplog 与 mongorestore --oplogReplay 结合使用,以确保数据库是最新的,并且具有在 mongodump 运行期间发生的所有写入操作。
了解详情
用于集群到集群迁移的 mongosync 实用程序
在 Atlas 中迁移或导入数据
在 Atlas 中备份、恢复和存档数据