Docs 菜单
Docs 主页
/
MongoDB Atlas
/

从 S3 导入存档

在此页面上

  • 先决条件
  • 步骤

注意

此功能不适用于 M0 免费集群、M2M5 集群。要详细了解哪些功能不可用,请参阅 Atlas M0(免费集群)、M2 和 M5 限制

您可以使用 3mongoimport mongorestore将存档数据恢复到 S 存储桶。本页提供了使用 AWS CLI 和 MongoDB 数据库工具导入存档数据和重建索引的示例过程。

在开始之前,您必须:

1
aws s3 cp s3://<bucketName>/<prefix> <downloadFolder> --recursive
gunzip -r <downloadFolder>

其中:

<bucketName>
AWS S3 存储桶的名称。
<prefix>

存储桶中已归档数据的路径。路径的格式如下:

/exported_snapshots/<orgId>/<projectId>/<clusterName>/<initiationDateOfSnapshot>/<timestamp>/
<downloadFolder>
要从中复制归档数据的本地文件夹的路径。

例如,运行与以下类似的命令:

例子

aws s3 cp
s3://export-test-bucket/exported_snapshots/1ab2cdef3a5e5a6c3bd12de4/12ab3456c7d89d786feba4e7/myCluster/2021-04-24T0013/1619224539
mybucket --recursive
gunzip -r mybucket
2
#!/bin/bash
regex='/(.+)/(.+)/.+'
dir=${1%/}
connstr=$2
# iterate through the subdirectories of the downloaded and
# extracted snapshot export and restore the docs with mongoimport
find $dir -type f -not -path '*/\.*' -not -path '*metadata\.json' | while read line ; do
[[ $line =~ $regex ]]
db_name=${BASH_REMATCH[1]}
col_name=${BASH_REMATCH[2]}
mongoimport --uri "$connstr" --mode=upsert -d $db_name -c $col_name --file $line --type json
done
# create the required directory structure and copy/rename files
# as needed for mongorestore to rebuild indexes on the collections
# from exported snapshot metadata files and feed them to mongorestore
find $dir -type f -name '*metadata\.json' | while read line ; do
[[ $line =~ $regex ]]
db_name=${BASH_REMATCH[1]}
col_name=${BASH_REMATCH[2]}
mkdir -p ${dir}/metadata/${db_name}/
cp $line ${dir}/metadata/${db_name}/${col_name}.metadata.json
done

此处:

  • --mode=upsert 使mongoimport能够处理存档中的重复文档。

  • --uri 指定了 Atlas 集群的连接字符串。

3
sh massimport.sh <downloadFolder> "mongodb+srv://<connectionString>"

其中:

<downloadFolder>
将存档数据复制到的本地文件夹的路径。
<connectionString>
Atlas 集群的连接字符串。

例如,运行与以下类似的命令:

例子

sh massimport.sh mybucket "mongodb+srv://<myConnString>"
← 将 Cloud Manager 快照恢复到 Atlas

在此页面上