重要
在公共预览版期间,Atlas Infinite 集群目前不支持本页面描述的功能。有关公共预览版中支持的功能,请参阅 公共预览版可用性。
注意
此功能适用于 M10 集群或更高版本。要了解有关 Free 集群可用功能的更多信息,请参阅 Atlas Free Cluster Limits;有关 Flex 集群的更多信息,请参阅 Atlas Flex Limitations。
您可以使用 和 恢复存档到 S3mongoimport 、mongorestore Azure 或 Google Cloud 存储的数据。本页提供了使用 Amazon Web Services、azopy 或 gcloud CLI(根据数据源)和 MongoDB Database Tools 导入存档数据和重建索引的示例过程。
要了解如何在导入之前将云备份快照导出到云对象存储存储桶,请参阅 将云备份快照导出到对象存储。
先决条件
在开始之前,您必须:
步骤
使用 AWS CLI 将 S 存储桶中的数据复制到文件夹并解压缩数据。3
aws s3 cp s3://<bucketName>/<prefix> <downloadFolder> --recursivegunzip -r <downloadFolder>其中:
| AWS S3 存储桶的名称。 |
| 存储桶中已归档数据的路径。路径的格式如下: |
| 要从中复制归档数据的本地文件夹的路径。 |
例如,运行与以下类似的命令:
例子
aws s3 cps3://export-test-bucket/exported_snapshots/1ab2cdef3a5e5a6c3bd12de4/12ab3456c7d89d786feba4e7/myCluster/2021-04-24T0013/1619224539mybucket --recursivegunzip -r mybucket将以下脚本复制并存储在名为 massimport.sh 的文件中。
regex='/(.+)/(.+)/.+'dir=${1%/}connstr=$2
# iterate through the subdirectories of the downloaded and# extracted snapshot export and restore the docs with mongoimportfind $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 jsondone
# 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 mongorestorefind $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.jsondonemongorestore "$connstr" ${dir}/metadata/
# remove the metadata directory because we do not need it anymore and this returns# the snapshot directory in an identical state as it was prior to the importrm -rf ${dir}/metadata/此处:
--mode=upsert使mongoimport能够处理存档中的重复文档。--uri指定了 Atlas 集群的连接字符串。
使用 azcopy CLI复制Azure Blob 存储容器中的数据并提取数据。
azcopy copy "https://<storageAccountName>.blob.core.windows.net/<containerName>/<prefix>/*" "<downloadFolder>" --recursive其中:
| blob存储容器所属的Azure帐户的名称。 |
| Azure blob存储容器的名称。 |
| 存储桶中已归档数据的路径。 |
| 要从中复制归档数据的本地文件夹的路径。 |
例子
azcopy copy "https://mystorageaccount.blob.core.windows.net/mycontainer/myTextFile.txt" "~/downloads" --recursive将以下脚本复制并存储在名为 massimport.sh 的文件中。
regex='/(.+)/(.+)/.+'dir=${1%/}connstr=$2
# iterate through the subdirectories of the downloaded and# extracted snapshot export and restore the docs with mongoimportfind $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 jsondone
# 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 mongorestorefind $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.jsondonemongorestore "$connstr" ${dir}/metadata/
# remove the metadata directory because we do not need it anymore and this returns# the snapshot directory in an identical state as it was prior to the importrm -rf ${dir}/metadata/此处:
--mode=upsert使mongoimport能够处理存档中的重复文档。--uri指定了 Atlas 集群的连接字符串。
使用 gcloud CLI复制 Google Cloud Storage 存储桶中的数据并提取数据。
gsutil -m cp -r "gs://<bucketName>/<prefix> <downloadFolder>" --recursivegunzip -r <downloadFolder>其中:
| Google Cloud Platform存储桶的名称。 |
| 存储桶中已归档数据的路径。路径的格式如下: |
| 要从中复制归档数据的本地文件夹的路径。 |
例子
gsutil -m cp -rgs://export-test-bucket/exported_snapshots/1ab2cdef3a5e5a6c3bd12de4/12ab3456c7d89d786feba4e7/myCluster/2021-04-24T0013/1619224539mybucket --recursivegunzip -r mybucket将以下脚本复制并存储在名为 massimport.sh 的文件中。
regex='/(.+)/(.+)/.+'dir=${1%/}connstr=$2
# iterate through the subdirectories of the downloaded and# extracted snapshot export and restore the docs with mongoimportfind $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 jsondone
# 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 mongorestorefind $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.jsondonemongorestore "$connstr" ${dir}/metadata/
# remove the metadata directory because we do not need it anymore and this returns# the snapshot directory in an identical state as it was prior to the importrm -rf ${dir}/metadata/此处:
--mode=upsert使mongoimport能够处理存档中的重复文档。--uri指定了 Atlas 集群的连接字符串。
运行 massimport.sh 实用程序将存档数据导入 Atlas 集群。
sh massimport.sh <downloadFolder> "mongodb+srv://<connectionString>"其中:
| 将存档数据复制到的本地文件夹的路径。 |
| Atlas 集群的连接字符串。 |
例子
sh massimport.sh mybucket "mongodb+srv://<myConnString>"