mongorestore示例
本页内容
此页面展示了以下示例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
将数据恢复到运行在本机接口上端口号为 27017
的 mongod
实例。
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
命令。
作为替代方案,用户可以使用 mongodump
和 mongorestore
(使用 mongorestore
选项 --nsFrom
和 --nsTo
)。
例如,要将本地实例(默认端口 27017)上的 test
数据库复制到同一实例上的 examples
数据库中,您可以
使用
mongodump
将test
数据库导出到存档mongodump-test-db
mongodump --archive="mongodump-test-db" --db=test 使用带有
--nsFrom
和--nsTo
的mongorestore
从存档恢复(更改数据库名称)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.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.
要连接到一个已配置为通过 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>
注意
或者,可以使用--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
,则输出运行期间发生的写入。
使用带有oplog选项的mongodump
mongodump --oplog
在mongodump
输出目录的最高层创建一个名为oplog.bson
的文件。该文件包含在mongodump
运行期间发生的写入操作。在mongodump
完成后发生的写入不会记录在该文件中。
有关使用mongodump
备份分片集群的信息,请参阅使用数据库转储备份自管理的分片集群。
使用带有 oplogReplay 选项的 mongorestore
要从 oplog.bson
文件恢复 oplog 条目,请使用 mongorestore --oplogReplay
。使用 mongodump --oplog
与 mongorestore --oplogReplay
一起使用,以确保数据库是最新的并且包含在 mongodump
运行期间发生的所有写入。