

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 AWS DataSync 同步不同 AWS 區域中 Amazon EFS 檔案系統之間的資料 DataSync
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync"></a>

*Sarat Chandra Pothula 和 Aditya Ambati，Amazon Web Services*

## 總結
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-summary"></a>

此解決方案提供強大的架構，可在不同 AWS 區域中的 Amazon Elastic File System (Amazon EFS) 執行個體之間有效且安全地進行資料同步。此方法可擴展，並提供受控的跨區域資料複寫。此解決方案可以增強您的災難復原和資料備援策略。

透過使用 AWS Cloud Development Kit (AWS CDK)，此模式會使用 做為基礎設施的程式碼 (IaC) 方法來部署解決方案資源。AWS CDK 應用程式會部署必要的 AWS DataSync、Amazon EFS、Amazon Virtual Private Cloud (Amazon VPC) 和 Amazon Elastic Compute Cloud (Amazon EC2) 資源。此 IaC 提供可重複且版本控制的部署程序，完全符合 AWS 最佳實務。

## 先決條件和限制
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-prereqs"></a>

**先決條件**
+ 作用中的 AWS 帳戶
+ AWS Command Line Interface (AWS CLI) 2.9.11 版或更新版本，[已安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)並[設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
+ AWS CDK 2.114.1 版或更新版本，[已安裝](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_install)並[已引導](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html#getting_started_bootstrap)
+ NodeJS 20.8.0 版或更新版本，[已安裝](https://nodejs.org/en/download)

**限制**
+ 解決方案會繼承 DataSync 和 Amazon EFS 的限制，例如資料傳輸率、大小限制和區域可用性。如需詳細資訊，請參閱 [AWS DataSync 配額](https://docs.aws.amazon.com/datasync/latest/userguide/datasync-limits.html)和 [Amazon EFS 配額](https://docs.aws.amazon.com/efs/latest/ug/limits.html)。
+ 此解決方案僅支援 Amazon EFS。DataSync 支援[其他 AWS 服務](https://docs.aws.amazon.com/datasync/latest/userguide/working-with-locations.html)，例如 Amazon Simple Storage Service (Amazon S3) 和 Amazon FSx for Lustre。不過，此解決方案需要修改，才能與這些其他服務同步資料。

## Architecture
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-architecture"></a>

![\[將資料複寫到不同區域中 EFS 檔案系統的架構圖\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/e28ba6c2-ab8b-4812-932e-f038106d5496/images/18b35ae9-a22e-43e7-b7a3-30e40321c44e.png)


此解決方案會部署下列 AWS CDK 堆疊：
+ **Amazon VPC 堆疊** – 此堆疊會在主要和次要 AWS 區域中設定虛擬私有雲端 (VPC) 資源，包括子網路、網際網路閘道和 NAT 閘道。
+ **Amazon EFS 堆疊** – 此堆疊會將 Amazon EFS 檔案系統部署到主要和次要區域，並將其連接到各自的 VPCs。
+ **Amazon EC2 堆疊** – 此堆疊會在主要和次要區域中啟動 EC2 執行個體。這些執行個體設定為掛載 Amazon EFS 檔案系統，允許其存取共用儲存體。
+ **DataSync 位置堆疊** – 此堆疊使用名為 的自訂建構`DataSyncLocationConstruct`，在主要和次要區域中建立 DataSync 位置資源。這些資源定義用於資料同步的端點。
+ **DataSync 任務堆疊** – 此堆疊使用名為 的自訂建構`DataSyncTaskConstruct`，在主要區域中建立 DataSync 任務。此任務設定為使用 DataSync 來源和目的地位置，在主要和次要區域之間同步資料。

## 工具
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-tools"></a>

**AWS 服務**
+ [AWS 雲端開發套件 (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) 是一種軟體開發架構，可協助您在程式碼中定義和佈建 AWS 雲端基礎設施。
+ [AWS DataSync](https://docs.aws.amazon.com/datasync/latest/userguide/what-is-datasync.html) 是一種線上資料傳輸和探索服務，可協助您在 AWS 儲存服務之間來回移動檔案或物件資料。
+ [Amazon Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/ec2/) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，，並快速進行擴展或縮減。
+ [Amazon Elastic File System (Amazon EFS)](https://docs.aws.amazon.com/efs/latest/ug/whatisefs.html) 可協助您在 AWS 雲端中建立和設定共用檔案系統。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。這個虛擬網路類似於您在自己的資料中心內操作的傳統網路，具有使用可擴展的 AWS 基礎設施的優勢。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [Amazon EFS 跨區域 DataSync 專案](https://github.com/aws-samples/aws-efs-crossregion-datasync/tree/main)儲存庫中使用。

## 最佳實務
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-best-practices"></a>

遵循 [TypeScript 中使用 AWS CDK 建立 IaC 專案的最佳實務中所述的最佳實務](https://docs.aws.amazon.com/prescriptive-guidance/latest/best-practices-cdk-typescript-iac/introduction.html)。

## 史詩
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-epics"></a>

### 部署 AWS CDK 應用程式
<a name="deploy-the-aws-cdk-app"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 複製專案儲存庫。 | 輸入下列命令以複製 [Amazon EFS 跨區域 DataSync 專案](https://github.com/aws-samples/aws-efs-crossregion-datasync/tree/main)儲存庫。<pre>git clone https://github.com/aws-samples/aws-efs-crossregion-datasync.git</pre> | AWS DevOps | 
| 安裝 npm 相依性。 | 輸入以下命令。<pre>npm ci</pre> | AWS DevOps | 
| 選擇主要和次要區域。 | 在複製的儲存庫中，導覽至 `src/infa`目錄。在 `Launcher.ts` 檔案中，更新 `PRIMARY_AWS_REGION`和 `SECONDARY_AWS_REGION`值。使用對應的[區域代碼](https://docs.aws.amazon.com/general/latest/gr/datasync.html#datasync-region)。<pre>const primaryRegion = { account: account, region: '<PRIMARY_AWS_REGION>' };<br />const secondaryRegion = { account: account, region: '<SECONDARY_AWS_REGION>' };</pre> | AWS DevOps | 
| 引導環境。 | 輸入下列命令以引導您要使用的 AWS 帳戶和 AWS 區域。<pre>cdk bootstrap <aws_account>/<aws_region></pre>如需詳細資訊，請參閱 AWS CDK 文件中的[引導](https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html)。 | AWS DevOps | 
| 列出 AWS CDK 堆疊。 | 輸入下列命令以檢視應用程式中 AWS CDK 堆疊的清單。<pre>cdk ls</pre> | AWS DevOps | 
| 合成 AWS CDK 堆疊。 | 輸入下列命令，為 AWS CDK 應用程式中定義的每個堆疊產生 AWS CloudFormation 範本。<pre>cdk synth</pre> | AWS DevOps | 
| 部署 AWS CDK 應用程式。 | 輸入下列命令，將所有堆疊部署到您的 AWS 帳戶，而不需要任何變更的手動核准。<pre>cdk deploy --all --require-approval never</pre> | AWS DevOps | 

### 驗證部署
<a name="validate-the-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 登入主要區域中的 EC2 執行個體。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync.html) | AWS DevOps | 
| 建立暫存檔案。 | 輸入下列命令以在 Amazon EFS 掛載路徑中建立暫存檔案。<pre>sudo dd if=/dev/zero \<br />of=tmptst.dat \<br />bs=1G \<br />seek=5 \<br />count=0<br /><br />ls -lrt tmptst.dat</pre> | AWS DevOps | 
| 啟動 DataSync 任務。 | 輸入下列命令，將暫存檔案從主要區域複寫到次要區域，其中 `<ARN-task>`是 DataSync 任務的 Amazon Resource Name (ARN)。<pre>aws datasync start-task-execution \<br />    --task-arn <ARN-task></pre>命令會以下列格式傳回任務執行的 ARN。`arn:aws:datasync:<region>:<account-ID>:task/task-execution/<exec-ID>` | AWS DevOps | 
| 檢查資料傳輸的狀態。 | 輸入下列命令來描述 DataSync 執行任務，其中 `<ARN-task-execution>`是任務執行的 ARN。<pre>aws datasync describe-task-execution \<br />    --task-execution-arn <ARN-task-execution></pre>當 `PrepareStatus`、 和 `VerifyStatus`都有值 時`TransferStatus`，DataSync 任務即完成`SUCCESS`。 | AWS DevOps | 
| 登入次要區域中的 EC2 執行個體。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync.html) | AWS DevOps | 
| 驗證複寫。 | 輸入下列命令，以確認暫存檔案存在於 Amazon EFS 檔案系統中。<pre>ls -lrt<br />tmptst.dat</pre> | AWS DevOps | 

## 相關資源
<a name="synchronize-data-between-amazon-efs-file-systems-in-different-aws-regions-by-using-aws-datasync-resources"></a>

**AWS 文件**
+ [AWS CDK API 參考](https://docs.aws.amazon.com/cdk/api/v2/python/modules.html)
+ [使用 Amazon EFS 設定 AWS DataSync 傳輸](https://docs.aws.amazon.com/datasync/latest/userguide/create-efs-location.html)
+ [針對 AWS DataSync 傳輸的問題進行故障診斷](https://docs.aws.amazon.com/datasync/latest/userguide/troubleshooting-datasync-locations-tasks.html)

**其他 AWS 資源**
+ [AWS DataSync FAQs](https://aws.amazon.com/datasync/faqs/)