

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 Amazon Keyspaces 中使用 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)。

如果您使用带有[条件键](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)的 IAM 策略来限制特定源的传入流量，则必须确保 Amazon Keyspaces 有权代表您的主体执行还原操作。如果您的策略将传入流量限制为以下任一项，则您必须将 `aws:ViaAWSService` 条件键添加到 IAM 策略：
+ 具有 `aws:SourceVpce` 的 VPC 端点
+ 使用 `aws:SourceIp` 的 IP 范围
+ VPCs 与 `aws:SourceVpc`

`aws:ViaAWSService` 条件键允许在任何 AWS 服务使用主体的凭证发出请求时进行访问。有关更多信息，请参阅《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>

您可以通过控制台、CQL 和 AWS CLI，使用 PITR 在 Amazon Keyspaces 中为备份和还原操作配置表。

使用 CQL 或创建新表时 AWS CLI，必须在创建表语句中明确启用 PITR。使用控制台创建新表时，PITR 将默认处于启用状态。

要了解如何还原表，请参阅[在 Amazon Keyspaces 中将表从备份还原到指定的时间点](restoretabletopointintime.md)。

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

**使用控制台为表配置 PITR**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

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 或 AWS CLI关闭 Amazon Keyspaces 表的 PITR。

**重要**  
禁用 PITR 会立即删除您的备份历史记录，即使您在 35 天内为表重新启用 PITR 也是如此。

要了解如何还原表，请参阅[在 Amazon Keyspaces 中将表从备份还原到指定的时间点](restoretabletopointintime.md)。

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

**使用控制台禁用表的 PITR**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

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 委托人（例如用户、群组或角色）执行恢复权限的 (IAM) 策略。 AWS Identity and Access Management 否则，可能会出现意外行为。例如，如果在还原表时删除表的写入权限，则底层 `RestoreTableToPointInTime` 操作将无法向表中写入任何还原的数据。  
您只能在还原操作完成之后修改或删除权限。

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

**使用控制台将表还原到指定的时间点。**

1. [登录并在家中打开 Amazon Keyspaces 控制台。 AWS 管理控制台 https://console.aws.amazon.com/keyspaces/](https://console.aws.amazon.com/keyspaces/home)

1. 在控制台左侧的导航窗格中，选择**表**。

1. 在表列表中，选择要还原的表。

1. 在该表的 “**备份**” 选项卡上，在 “**Point-in-time 恢复**” 部分中，选择 “**恢复**”。

1. 对于新表名，为已还原的表输入一个新名称，例如 **mytable\$1restored**。

1. 要定义还原操作的时间点，可以在两个选项之间进行选择：
   + 选择预配置的**最早**时间。
   + 选择**指定日期和时间**并输入要将新表还原到的日期和时间。
**注意**  
您可以还原到**最早**时间和当前时间之间的任何时间点。Amazon Keyspaces 会根据所选日期和时间 (day:hour:minute:second) 将表数据还原到相应状态。

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 TABLE](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. 
   + 要将表还原到某个时间点，请指定 ISO 8601 格式的 `restore_timestamp`。您可以选择最近 35 天中的任何时间点，时间间隔为 1 秒。例如，以下命令使表还原到 `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"
   }
   ```

------