AWS Aurora:如何通过 aws cli 恢复数据库集群快照?

AWS Aurora: how to restore a db cluster snapshot via aws cli?

通过控制台非常简单,但我需要从 CLI 执行相同的操作。

首先我创建了一个数据库快照:

aws rds create-db-cluster-snapshot \
    --db-cluster-snapshot-identifier $SNAPSHOT_ID \
    --db-cluster-identifier $CLUSTER \

CLUSTER 仅包含一个编写器实例

I did not use create-db-snapshot method because it throwned an error

A client error (InvalidParameterValue) occurred when calling the CreateDBSnapshot operation: The specified instance is a member of a cluster and a snapshot cannot be created directly. Please use the CreateDBClusterSnapshot API instead.

有效:

aws rds create-db-cluster-snapshot \
  --db-cluster-snapshot-identifier $SNAPSHOT_ID \
  --db-cluster-identifier $CLUSTER \
{
    "DBClusterSnapshot": {
        "Engine": "aurora", 
        "SnapshotCreateTime": "2016-12-08T11:48:07.534Z", 
    ....
}

所以,我想从快照中恢复一个新的 Aurora 集群,然后我尝试了:

aws rds restore-db-instance-from-db-snapshot \
    --db-instance-identifier from-snap2 \
    --db-snapshot-identifier snap2 \

 A client error (DBSnapshotNotFound) occurred when calling the RestoreDBInstanceFromDBSnapshot operation: DBSnapshot not found: snap2

所以我尝试用以下方式恢复:

aws rds restore-db-cluster-from-snapshot \
    --db-cluster-identifier from-snap2 \
    --snapshot-identifier snap2 \
    --engine aurora \
    --vpc-security-group-ids $PREPROD_SG \
    --db-subnet-group-name my-db-subnet-group \

有效...

{
    "DBCluster": {
        ...
        "EngineVersion": "5.6.10a", 
        "DBClusterIdentifier": "from-snap2", 
...
        "DBClusterMembers": [], 
...
}

但为什么集群不包含任何 Aurora 实例?

哪里错了?

如果您使用 aws rds create-db-cluster-snapshot 创建,则无法使用 aws rds restore-db-instance-from-db-snapshot 恢复。第一个创建数据库快照,第二个恢复集群快照,不同类型。

从你的问题来看,你的恢复似乎是正确的,也许你需要指定 --database-name。您也可以尝试仅使用所需参数进行还原,即没有 vpc sg 或 DB 子网。

这很违反直觉。如果你从快照恢复一个集群,但是集群中没有任何成员实例,究竟是什么操作成功了?似乎这一切所做的只是创建某种逻辑实体,也许是后备存储,但没有实例。

奇怪。但是,API documentation 确实在示例响应中将集群成员显示为空集。

<DBClusterMembers/>

所以看起来你创建了一个集群,正如你所做的那样,然后你显然在集群中创建了实例,如 AWS 论坛中所述 post:

aws rds create-db-instance --db-instance-identifier my-instance --db-instance-class db.r3.large --engine aurora --db-subnet-group-name default-vpc-xxxxxx --db-cluster-identifier my-instance-cluster

https://forums.aws.amazon.com/thread.jspa?messageID=688727

显然,控制台在同一操作后封装了多个 API 请求。

AWS Support 的回复:

This is a known issue when using the API calls and our engineers are working on it. Even if the cluster is visible on AWS Console after the creation via CLI it will not create any instance automatically in your Aurora Cluster. In this case, you will need to create a db-instance and associate it to your newly restored cluster. When performing this Action on the AWS Console a new instance is automatically created for the cluster, but the action from the CLI uses separated API calls.

The following documentation provides detailed information on how to create a DB instance: http://docs.aws.amazon.com/cli/latest/reference/rds/create-db-instance.html

You can describe your clusters using the AWS Console or using the CLI: http://docs.aws.amazon.com/cli/latest/reference/rds/describe-db-clusters.html

Here is a command line example that creates the instance and associate it to a fictional cluster: aws rds create-db-instance --engine aurora --db-cluster-identifier yourauroraclusteridentifier --db-instance-class db.t2.medium --db-instance-identifier yourinstanceidentifier

在我的例子中,--db-cluster-identifier 是从集群快照创建的集群。