

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

# 在 point-in-time復原
<a name="PointInTimeRecovery_Tutorial"></a>

使用 Amazon Keyspaces （適用於 Apache Cassandra)，您可以使用時間點還原 (PITR) Point-in-Time 將資料表還原至特定時間點。PITR 可讓您將資料表還原至過去 35 天內的狀態，提供資料保護和復原功能。此功能在意外刪除資料、應用程式錯誤或用於測試等情況下非常有用。您可以快速有效地復原資料，將停機時間和資料遺失降到最低。以下各節會引導您完成在 Amazon Keyspaces 中使用 PITR 還原資料表的程序，以確保資料完整性和業務連續性。

**Topics**
+ [設定 Amazon Keyspaces PITR 的還原資料表 IAM 許可](howitworks_restore_permissions.md)
+ [在 Amazon Keyspaces 中設定資料表的 PITR](configure_PITR.md)
+ [關閉 Amazon Keyspaces 資料表的 PITR](disable_PITR.md)
+ [將資料表從備份還原到 Amazon Keyspaces 中指定的時間點](restoretabletopointintime.md)
+ [使用 Amazon Keyspaces PITR 還原已刪除的資料表](restoredeleted.md)

# 設定 Amazon Keyspaces PITR 的還原資料表 IAM 許可
<a name="howitworks_restore_permissions"></a>

本節摘要說明如何設定 AWS Identity and Access Management (IAM) 主體的許可來還原 Amazon Keyspaces 資料表。在 IAM 中， AWS 受管政策`AmazonKeyspacesFullAccess`包含還原 Amazon Keyspaces 資料表的許可。若要實作具有最低必要許可的自訂政策，請考慮下一節中概述的需求。

若要成功還原資料表，IAM 主體需要下列最低許可：
+ `cassandra:Restore` – 目標資料表需要還原動作才能還原。
+ `cassandra:Select` – 從來源資料表讀取時需要選取動作。
+ `cassandra:TagResource` – 標籤動作是選用的，只有在還原操作新增標籤時才需要。

這是將最低必要許可授予使用者以還原金鑰空間 中的資料表的政策範例`mykeyspace`。

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "cassandra:Restore",
            "cassandra:Select"
         ],
         "Resource":[
            "arn:aws:cassandra:us-east-1:111122223333:/keyspace/mykeyspace/*",
            "arn:aws:cassandra:us-east-1:111122223333:/keyspace/system*"
         ]
      }
   ]
}
```

根據其他選取的功能，可能需要其他許可才能還原資料表。例如，如果來源資料表使用客戶受管金鑰進行靜態加密，Amazon Keyspaces 必須具有存取來源資料表之客戶受管金鑰的許可，才能成功還原資料表。如需詳細資訊，請參閱[加密資料表的 PITR 還原](PointInTimeRecovery_HowItWorks.md#howitworks_backup_encryption)。

如果您使用 IAM 政策搭配[條件金鑰](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)來限制特定來源的傳入流量，您必須確保 Amazon Keyspaces 具有代表您委託人執行還原操作的許可。如果您的政策將傳入流量限制為下列任何項目，您必須將 `aws:ViaAWSService`條件金鑰新增至 IAM 政策：
+ 使用 的 VPC 端點 `aws:SourceVpce`
+ 使用 的 IP 範圍 `aws:SourceIp`
+ 使用 VPCs `aws:SourceVpc`

當任何 AWS 服務使用委託人的登入資料提出請求時， `aws:ViaAWSService`條件金鑰允許存取。如需詳細資訊，請參閱《[IAM 使用者指南》中的 IAM JSON 政策元素：條件索引鍵](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)。 **

以下是將來源流量限制為特定 IP 地址，並允許 Amazon Keyspaces 代表委託人還原資料表的政策範例。

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[
      {
         "Sid":"CassandraAccessForCustomIp",
         "Effect":"Allow",
         "Action":"cassandra:*",
         "Resource":"*",
         "Condition":{
            "Bool":{
               "aws:ViaAWSService":"false"
            },
            "ForAnyValue:IpAddress":{
               "aws:SourceIp":[
                  "123.45.167.89"
               ]
            }
         }
      },
      {
         "Sid":"CassandraAccessForAwsService",
         "Effect":"Allow",
         "Action":"cassandra:*",
         "Resource":"*",
         "Condition":{
            "Bool":{
               "aws:ViaAWSService":"true"
            }
         }
      }
   ]
}
```

 如需使用`aws:ViaAWSService`全域條件金鑰的範例政策，請參閱 [VPC 端點政策和 Amazon Keyspaces point-in-time(PITR)](vpc-endpoints.md#VPC_PITR_restore)。

# 在 Amazon Keyspaces 中設定資料表的 PITR
<a name="configure_PITR"></a>

您可以在 Amazon Keyspaces 中設定資料表，以搭配主控台、CQL 和 使用 PITR 進行備份和還原操作 AWS CLI。

使用 CQL 或 建立新資料表時 AWS CLI，您必須在建立資料表陳述式中明確啟用 PITR。當您使用主控台建立新資料表時，預設會啟用 PITR。

若要了解如何還原資料表，請參閱 [將資料表從備份還原到 Amazon Keyspaces 中指定的時間點](restoretabletopointintime.md)。

------
#### [ Console ]

**使用主控台設定資料表的 PITR**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在導覽窗格中，選擇**資料表**，然後選取您要編輯的資料表。

1. 在**備份**索引標籤上，選擇**編輯**。

1. 在**編輯point-in-time復原設定**區段中，選取**啟用Point-in-time復原**。

1. 選擇**儲存變更**。

------
#### [ Cassandra Query Language (CQL) ]

**使用 CQL 設定資料表的 PITR**

1. 您可以使用 `point_in_time_recovery`自訂屬性來管理資料表的 PITR 設定。

   若要在建立新資料表時啟用 PITR，您必須將狀態設定為 `point_in_time_recovery` `enabled`。您可以使用下列 CQL 命令做為範例。

   ```
   CREATE TABLE "my_keyspace1"."my_table1"(
   	"id" int,
   	"name" ascii,
   	"date" timestamp,
   	PRIMARY KEY("id"))
   WITH CUSTOM_PROPERTIES = {
   	'capacity_mode':{'throughput_mode':'PAY_PER_REQUEST'}, 
   	'point_in_time_recovery':{'status':'enabled'}
   }
   ```
**注意**  
如果未指定point-in-time復原自訂屬性，則時間點point-in-time復原預設為停用。

1. 若要使用 CQL 為現有資料表啟用 PITR，請執行下列 CQL 命令。

   ```
   ALTER TABLE mykeyspace.mytable
   WITH custom_properties = {'point_in_time_recovery': {'status': 'enabled'}}
   ```

------
#### [ CLI ]

**使用 設定資料表的 PITR AWS CLI**

1. 您可以使用 `UpdateTable` API 管理資料表的 PITR 設定。

   若要在建立新資料表時啟用 PITR，您必須在建立資料表命令`point-in-time-recovery 'status=ENABLED'`中包含 。您可以使用下列 AWS CLI 命令做為範例。命令已分成不同的行來改善可讀性。

   ```
   aws keyspaces create-table --keyspace-name 'myKeyspace' --table-name 'myTable' 
               --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' 
               --point-in-time-recovery 'status=ENABLED'
   ```
**注意**  
如果未指定point-in-time復原值，則會預設停用point-in-time復原。

1. 若要確認資料表的point-in-time復原設定，您可以使用下列 AWS CLI 命令。

   ```
   aws keyspaces get-table --keyspace-name 'myKeyspace' --table-name 'myTable'
   ```

1. 若要使用 為現有資料表啟用 PITR AWS CLI，請執行下列命令。

   ```
   aws keyspaces update-table --keyspace-name 'myKeyspace' --table-name 'myTable' --point-in-time-recovery 'status=ENABLED'
   ```

------

# 關閉 Amazon Keyspaces 資料表的 PITR
<a name="disable_PITR"></a>

您可以隨時使用主控台、CQL 或 來關閉 Amazon Keyspaces 資料表的 PITR AWS CLI。

**重要**  
停用 PITR 會立即刪除備份歷史記錄，即使您在 35 天內在資料表上重新啟用 PITR。

若要了解如何還原資料表，請參閱 [將資料表從備份還原到 Amazon Keyspaces 中指定的時間點](restoretabletopointintime.md)。

------
#### [ Console ]

**使用主控台停用資料表的 PITR**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在導覽窗格中，選擇**資料表**，然後選取您要編輯的資料表。

1. 在**備份**索引標籤上，選擇**編輯**。

1. 在**編輯point-in-time復原設定**區段中，清除**啟用Point-in-time復原**核取方塊。

1. 選擇**儲存變更**。

------
#### [ Cassandra Query Language (CQL) ]

**使用 CQL 停用資料表的 PITR**
+ 若要停用現有資料表的 PITR，請執行下列 CQL 命令。

  ```
  ALTER TABLE mykeyspace.mytable
  WITH custom_properties = {'point_in_time_recovery': {'status': 'disabled'}}
  ```

------
#### [ CLI ]

**使用 停用資料表的 PITR AWS CLI**
+ 若要停用現有資料表的 PITR，請執行下列 AWS CLI 命令。

  ```
  aws keyspaces update-table --keyspace-name 'myKeyspace' --table-name 'myTable' --point-in-time-recovery 'status=DISABLED'
  ```

------

# 將資料表從備份還原到 Amazon Keyspaces 中指定的時間點
<a name="restoretabletopointintime"></a>

下一節示範如何將現有的 Amazon Keyspaces 資料表還原至指定的時間點。

**注意**  
此程序假設您使用的資料表已設定point-in-time復原。若要啟用資料表的 PITR，請參閱 [在 Amazon Keyspaces 中設定資料表的 PITR](configure_PITR.md)。

**重要**  
 還原進行中時，請勿修改或刪除授予 IAM 主體 （例如使用者、群組或角色） 執行還原許可的 AWS Identity and Access Management (IAM) 政策。否則，可能會造成意外行為。例如，如果您在資料表還原時移除資料表的寫入許可，基礎`RestoreTableToPointInTime`操作就無法將任何還原的資料寫入資料表。  
您只能在還原操作完成後修改或刪除許可。

------
#### [ Console ]

**使用主控台將資料表還原至指定的時間點**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/keyspaces/home](https://console.aws.amazon.com/keyspaces/home) 開啟 Amazon Keyspaces 主控台。

1. 在主控台左側的導覽窗格中，選擇 **Tables** (資料表)。

1. 在資料表清單中，選擇您要還原的資料表。

1. 在資料表的**備份**索引標籤的**Point-in-time復原**區段中，選擇**還原**。

1. 針對新的資料表名稱，輸入還原資料表的新名稱，例如 **mytable\$1restored**。

1. 若要定義還原操作的時間點，您可以選擇兩個選項：
   + 選取預先設定的**最早**時間。
   + 選取**指定日期和時間**，然後輸入您要還原新資料表的日期和時間。
**注意**  
您可以在**最早**時間和目前時間還原至任何時間點。Amazon Keyspaces 會根據選取的日期和時間 （日：小時：分鐘：秒） 將資料表資料還原至 狀態。

1. 選擇**還原**以開始還原程序。

   正在還原的資料表會顯示為 **Restoring (正在還原)** 狀態。還原程序完成後，還原資料表的狀態會變更為**作用中**。

------
#### [ Cassandra Query Language (CQL) ]

**使用 CQL 將資料表還原至某個時間點**

1. 您可以將作用中資料表還原至介於 point-in-time。 `earliest_restorable_timestamp`目前時間是預設值。

   若要確認資料表已啟用point-in-time復原，請查詢 `system_schema_mcs.tables`，如本範例所示。

   ```
   SELECT custom_properties
   FROM system_schema_mcs.tables
   WHERE keyspace_name = 'mykeyspace' AND table_name = 'mytable';
   ```

   Point-in-time復原已啟用，如下列範例輸出所示。

   ```
   custom_properties
   -----------------
   {
     ...,
       "point_in_time_recovery": {
       "earliest_restorable_timestamp":"2020-06-30T19:19:21.175Z"
       "status":"enabled"
     }
   }
   ```

1. 
   + 將資料表還原至目前時間。當您省略 `WITH restore_timestamp = ...`子句時，會使用目前的時間戳記。

     ```
     RESTORE TABLE mykeyspace.mytable_restored
     FROM TABLE mykeyspace.mytable;
     ```
   + 您也可以還原至特定時間點，由 ISO 8601 格式`restore_timestamp`的 定義。您可以指定過去 35 天內的任何時間點。例如，下列命令可復原資料表至 `EarliestRestorableDateTime`。

     ```
     RESTORE TABLE mykeyspace.mytable_restored
     FROM TABLE mykeyspace.mytable
     WITH restore_timestamp = '2020-06-30T19:19:21.175Z';
     ```

     如需完整的語法描述，請參閱語言參考[RESTORE 資料表](cql.ddl.table.md#cql.ddl.table.restore)中的 。

1. 若要驗證資料表的還原是否成功，請查詢 `system_schema_mcs.tables` 以確認資料表的狀態。

   ```
   SELECT status
   FROM system_schema_mcs.tables
   WHERE keyspace_name = 'mykeyspace' AND table_name = 'mytable_restored'
   ```

   查詢會顯示下列輸出。

   ```
   status
   ------
   RESTORING
   ```

   正在還原的資料表會顯示為 **Restoring (正在還原)** 狀態。還原程序完成後，資料表的狀態會變更為**作用中**。

------
#### [ CLI ]

**使用 將資料表還原至某個時間點 AWS CLI**

1. 建立名為 `myTable`且已啟用 PITR 的簡單資料表。命令已分成不同的行以方便閱讀。

   ```
   aws keyspaces create-table --keyspace-name 'myKeyspace' --table-name 'myTable' 
               --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' 
               --point-in-time-recovery 'status=ENABLED'
   ```

1. 確認新資料表的屬性，並檢閱 PITR `earliestRestorableTimestamp` 的 。

   ```
   aws keyspaces get-table --keyspace-name 'myKeyspace' --table-name 'myTable'
   ```

   此命令的輸出會傳回下列項目。

   ```
   {
       "keyspaceName": "myKeyspace",
       "tableName": "myTable",
       "resourceArn": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/myKeyspace/table/myTable",
       "creationTimestamp": "2022-06-20T14:34:57.049000-07:00",
       "status": "ACTIVE",
       "schemaDefinition": {
           "allColumns": [
               {
                   "name": "id",
                   "type": "int"
               },
               {
                   "name": "date",
                   "type": "timestamp"
               },
               {
                   "name": "name",
                   "type": "text"
               }
           ],
           "partitionKeys": [
               {
                   "name": "id"
               }
           ],
           "clusteringKeys": [],
           "staticColumns": []
       },
       "capacitySpecification": {
           "throughputMode": "PAY_PER_REQUEST",
           "lastUpdateToPayPerRequestTimestamp": "2022-06-20T14:34:57.049000-07:00"
       },
       "encryptionSpecification": {
           "type": "AWS_OWNED_KMS_KEY"
       },
       "pointInTimeRecovery": {
           "status": "ENABLED",
           "earliestRestorableTimestamp": "2022-06-20T14:35:13.693000-07:00"
       },
       "defaultTimeToLive": 0,
       "comment": {
           "message": ""
       }
   }
   ```

1. 
   + 若要將資料表還原至某個時間點，`restore_timestamp`請以 ISO 8601 格式指定 。您可以選擇過去 35 天內的任何時間點，每隔一秒鐘。例如，下列命令可復原資料表至 `EarliestRestorableDateTime`。

     ```
     aws keyspaces restore-table --source-keyspace-name 'myKeyspace' --source-table-name 'myTable' --target-keyspace-name 'myKeyspace' --target-table-name 'myTable_restored' --restore-timestamp "2022-06-20 21:35:14.693"
     ```

     此命令的輸出會傳回還原資料表的 ARN。

     ```
     {
         "restoredTableARN": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/myKeyspace/table/myTable_restored"
     }
     ```
   + 若要將資料表還原至目前時間，您可以省略 `restore-timestamp` 參數。

     ```
     aws keyspaces restore-table --source-keyspace-name 'myKeyspace' --source-table-name 'myTable' --target-keyspace-name 'myKeyspace' --target-table-name 'myTable_restored1'"
     ```

------

# 使用 Amazon Keyspaces PITR 還原已刪除的資料表
<a name="restoredeleted"></a>

下列程序說明如何將已刪除的資料表從備份還原至刪除時間。您可以使用 CQL 或 來執行此操作 AWS CLI。

**注意**  
此程序假設刪除的資料表上已啟用 PITR。



------
#### [ Cassandra Query Language (CQL) ]

**使用 CQL 還原已刪除的資料表**

1. 若要確認已刪除資料表已啟用point-in-time復原，請查詢系統資料表。只會顯示已啟用point-in-time復原的資料表。

   ```
   SELECT custom_properties
   FROM system_schema_mcs.tables_history 
   WHERE keyspace_name = 'mykeyspace' AND table_name = 'my_table';
   ```

   查詢會顯示下列輸出。

   ```
   custom_properties
   ------------------
   {
       ...,
      "point_in_time_recovery":{
         "restorable_until_time":"2020-08-04T00:48:58.381Z",
         "status":"enabled"
      }
   }
   ```

1. 使用下列範例陳述式將資料表還原至刪除時間。

   ```
   RESTORE TABLE mykeyspace.mytable_restored
   FROM TABLE mykeyspace.mytable;
   ```

------
#### [ CLI ]

**使用 還原已刪除的資料表 AWS CLI**

1. 刪除您先前建立且已啟用 PITR 的資料表。下列是範例命令。

   ```
   aws keyspaces delete-table --keyspace-name 'myKeyspace' --table-name 'myTable'
   ```

1. 使用下列命令將已刪除的資料表還原至刪除時間。

   ```
   aws keyspaces restore-table --source-keyspace-name 'myKeyspace' --source-table-name 'myTable' --target-keyspace-name 'myKeyspace' --target-table-name 'myTable_restored2'
   ```

   此命令的輸出會傳回還原資料表的 ARN。

   ```
   {
       "restoredTableARN": "arn:aws:cassandra:us-east-1:111122223333:/keyspace/myKeyspace/table/myTable_restored2"
   }
   ```

------