

# 将 DynamoDB 表还原到某个时间点


Amazon DynamoDB 时间点恢复 (PITR) 提供 DynamoDB 表数据的持续备份。您可以使用 DynamoDB 控制台或 Amazon Command Line Interface (Amazon CLI) 将表还原到某个时间点。时间点恢复过程始终还原到新表。

如果要使用 Amazon CLI，必须先对其进行配置。有关更多信息，请参阅 [访问 DynamoDB](AccessingDynamoDB.md)。

**Topics**
+ [

## 将 DynamoDB 表还原到某个时间点（控制台）
](#restoretabletopointintime_console)
+ [

## 将表还原到某个时间点（Amazon CLI）
](#restorepointintime_cli)

## 将 DynamoDB 表还原到某个时间点（控制台）


以下示例演示如何使用 DynamoDB 控制台将名为 `Music` 的现有表还原到某个时间点。

**注意**  
此过程假定您已启用时间点故障恢复。要为 `Music` 表启用该功能，在 **Backups (备份)** 选项卡上的 **Point-in-time recovery (PITR) (时间点恢复 (PITR))** 部分，选择 **Edit (编辑)**，然后选中 **Enable point-in-time-recovery (启用时间点恢复)** 旁边的复选框。

**将表还原到某个时间点**

1. 登录 Amazon Web Services 管理控制台，打开 DynamoDB 控制台：[https://console.aws.amazon.com/dynamodb/](https://console.amazonaws.cn/dynamodb/)。

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

1. 在表的列表中，选择 `Music` 表。

1. 在 `Music` 表的 **Backups (备份)** 选项卡的 **Point-in-time recovery (PITR) (时间点恢复 (PITR))** 部分，选择 **Restore (还原)**。

1. 对于新表名称，输入 **MusicMinutesAgo**。
**注意**  
您可以将表还原到同一 Amazon 区域或源表所在的其他区域。您还可以阻止在还原的表上创建二级索引。此外，您可以指定其他加密模式。

1. 要确认可还原时间，请将还原日期和时间设置为 **Earliest (最早)**。然后选择 **Restore (还原)** 来启动还原过程。

   正在还原的表显示状态为 **Restoring (正在还原)**。还原过程完成后，`MusicMinutesAgo` 表的状态更改为 **Active (活动)**。

## 将表还原到某个时间点（Amazon CLI）


以下过程演示如何使用 Amazon CLI 将名为 `Music` 的现有表还原到某个时间点。

**注意**  
此过程假定您已启用时间点故障恢复。要为 `Music` 表启用，请运行下面的命令。  

```
aws dynamodb update-continuous-backups \
    --table-name Music \
    --point-in-time-recovery-specification PointInTimeRecoveryEnabled=True
```



**将表还原到某个时间点**

1. 通过使用 `Music` 命令来确认已为 `describe-continuous-backups` 表启用时间点恢复。

   ```
   aws dynamodb describe-continuous-backups \
       --table-name Music
   ```

   已启用持续备份 (在创建表时自动启用) 和时间点恢复。

   ```
   {
       "ContinuousBackupsDescription": {
           "PointInTimeRecoveryDescription": {
               "PointInTimeRecoveryStatus": "ENABLED", 
               "EarliestRestorableDateTime": 1519257118.0, 
               "LatestRestorableDateTime": 1520018653.01
           }, 
           "ContinuousBackupsStatus": "ENABLED"
       }
   }
   ```

1. 使表还原到某个时间点。在此示例中，对于同一 Amazon 区域，`Music` 表将还原到 `LatestRestorableDateTime`（约 5 分钟前）。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicMinutesAgo \
       --use-latest-restorable-time
   ```
**注意**  
 您还可以还原到特定时间点。为此，请运行使用 `--restore-date-time` 参数的命令，并指定时间戳。您可以指定配置的恢复期内的任意时间点，恢复期可以设置为 1 到 35 天之间的任意值。例如，以下命令使表还原到 `EarliestRestorableDateTime`。  

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicEarliestRestorableDateTime \
       --no-use-latest-restorable-time \
       --restore-date-time 1519257118.0
   ```
 在还原到特定时间点时，指定 `--no-use-latest-restorable-time` 参数是可选的。

1. 使用自定义表设置将表还原到某个时间点。在此示例中，`Music` 表还原到 `LatestRestorableDateTime` (\$15 分钟前)。

   您可以为还原的表指定其他加密模式，如下所示。
**注意**  
`sse-specification-override` 参数采用与 `sse-specification-override` 命令中使用的 `CreateTable` 参数相同的值。要了解更多信息，请参阅 [管理 DynamoDB 中的加密表](encryption.tutorial.md)。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicMinutesAgo \
       --use-latest-restorable-time \
       --sse-specification-override Enabled=true,SSEType=KMS,KMSMasterKeyId=abcd1234-abcd-1234-a123-ab1234a1b234
   ```

   可以将表还原到源表所在的其他 Amazon 区域。
**注意**  
`sse-specification-override` 参数对于跨区域还原是必需的，但对于到与源表相同的区域的还原是可选的。
必须为跨区域还原提供 `source-table-arn` 参数。
在从命令行执行跨区域还原时，您必须将默认 Amazon 区域设置为所需的目标区域。要了解更多信息，请参阅《Amazon Command Line Interface 用户指南》中的[命令行选项](https://docs.amazonaws.cn/cli/latest/userguide/cli-configure-options.html)。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-arn arn:aws:dynamodb:us-east-1:123456789012:table/Music \
       --target-table-name MusicMinutesAgo \
       --use-latest-restorable-time \
       --sse-specification-override Enabled=true,SSEType=KMS,KMSMasterKeyId=abcd1234-abcd-1234-a123-ab1234a1b234
   ```

   您可以覆盖已还原表的计费模式和预配置的吞吐量。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicMinutesAgo \
       --use-latest-restorable-time \
       --billing-mode-override PAY_PER_REQUEST
   ```

   您可以阻止在还原的表上创建部分或所有二级索引。
**注意**  
如果您阻止在新的还原表上创建部分或所有二级索引，则还原操作会更快且更具成本效益。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicMinutesAgo \
       --use-latest-restorable-time \
       --global-secondary-index-override '[]'
   ```

   您可以组合使用不同的替代方式。例如，您可以使用单个全局二级索引并同时更改预配置的吞吐量，如下所示。

   ```
   aws dynamodb restore-table-to-point-in-time \
       --source-table-name Music \
       --target-table-name MusicMinutesAgo \
       --billing-mode-override PROVISIONED \
       --provisioned-throughput-override ReadCapacityUnits=100,WriteCapacityUnits=100 \
       --global-secondary-index-override IndexName=singers-index,KeySchema=["{AttributeName=SingerName,KeyType=HASH}"],Projection="{ProjectionType=KEYS_ONLY}",ProvisionedThroughput="{ReadCapacityUnits=50,WriteCapacityUnits=50}" \
       --sse-specification-override Enabled=true,SSEType=KMS \
       --use-latest-restorable-time
   ```

要验证还原，请使用 `describe-table` 命令来描述 `MusicEarliestRestorableDateTime` 表。

```
aws dynamodb describe-table --table-name MusicEarliestRestorableDateTime
```

正在还原的表显示状态为 **Creating (正在创建)**，并且正在还原为 **true**。还原过程完成后，`MusicEarliestRestorableDateTime` 表的状态更改为 **Active (活动)**。

**重要**  
正在还原时，请勿修改或删除授予 IAM 实体（例如，用户、组或角色）执行还原的权限的 Amazon Identity and Access Management (IAM) 策略。否则，可能会出现意外行为。例如，假设您在还原表时删除了对该表的写入权限。在这种情况下，底层 `RestoreTableToPointInTime` 操作将无法向表中写入任何还原的数据。请注意，涉及有关访问目标还原表的源 IP 限制的 IAM policy 也可能同样导致问题。  
您只能在还原操作完成之后修改或删除权限。