AWS Aurora MySQL 使用 CLI 克隆数据库

AWS Aurora MySQL Database cloning using CLI

我想每周为我的生产极光 mysql 数据库创建一个副本。副本将用于开发。

我喜欢 Aurora 的克隆功能 MySQL 但不幸的是,从 AWS CLI 创建这些克隆的说明并不明确。

docs, I am able to create another Aurora cluster, but it doesn't create the DBs. It just creates an empty cluster. I am not able to figure out the commands to create a new Db inside this cluster from a snapshot of the Db in the source cluster as the restore-db-instance-from-db-snapshot is not supported 极光之后 MySQL。

请告诉我克隆 Aurora 集群及其内部数据库的命令。

根据 AWS documentation,这是一个两阶段过程。

当您创建一个新集群时:

aws rds restore-db-cluster-to-point-in-time \ 
  --source-db-cluster-identifier arn:aws:rds:eu-central-1:AAAAAAAAAAA:cluster:BBBBBBBBBB-cluster \ 
  --db-cluster-identifier YYYYYYYYYY-cluster \ 
  --restore-type copy-on-write \ 
  --use-latest-restorable-time

完成后,数据存储已创建并可以使用,但没有 aurora 实例 运行。

第二步是创建一个(或多个)实例:

aws rds create-db-instance \
  --db-cluster-identifier YYYYYYYYYY-cluster \ 
  --db-instance-class <value> \
  --engine <value>
  (other optional values)

答案是正确的,一个重要的细节,没有提到并且让我认为它不起作用,是安全策略不一定是相同的,所以为了使数据库可用,你需要设置相同或适当加使 DB public。我正在为 Java API 提供一些片段:

private final AmazonRDS rds;
rds.restoreDBClusterToPointInTime(
        new RestoreDBClusterToPointInTimeRequest()
            .withSourceDBClusterIdentifier("sourceClusterIdentifier")
            .withDBClusterIdentifier("targetName")
            .withRestoreType("copy-on-write")
            .withVpcSecurityGroupIds("vpc_group_id_to_be_found")  //important
            .withUseLatestRestorableTime(true));

DBInstance instanceOfDb = rds.createDBInstance(new CreateDBInstanceRequest()
    .withDBClusterIdentifier("targetName")
    .withDBInstanceIdentifier("targetName-cluster")
    .withEngine("aurora-postgresql")
    .withDBInstanceClass("db.r4.large")
    .withPubliclyAccessible(true) //important
    .withMultiAZ(false)
);

rds.waiters().dBInstanceAvailable()
    .run(new WaiterParameters<>(new DescribeDBInstancesRequest()
        .withDBInstanceIdentifier(instanceOfDb.getDBInstanceIdentifier()))
        .withPollingStrategy(new PollingBuilder().delay(30).maxWait(30, TimeUnit.MINUTES).build()));

Aurora DB 克隆的概念花了我一段时间才弄清楚。使用 Aurora,数据实际上是集群的一部分。数据库实例从集群中获取数据。要将一个 Aurora 集群克隆到另一个,您需要克隆该集群,然后在新集群中创建一个数据库实例。您在新集群中创建的数据库实例将从创建它的集群中获取数据。呸!那是一个很长的解释。无论如何,下面的 shell 脚本是我从 cron 运行 得到的,它对我有用(到目前为止)。对于此示例,下面的安全组 ID 显然是假的。

#!/bin/bash

start=$(date +%s)
NOW_DATE=$(date '+%Y-%m-%d-%H-%M')
SOURCE_CLUSTER_INSTANCE_ID=source-aurora-cluster
TARGET_CLUSTER_INSTANCE_ID=target-aurora-cluster
TARGET_CLUSTER_INSTANCE_CLASS=db.r3.large
TARGET_ENGINE="aurora-mysql"
NEW_MASTER_PASS=setyourpasshere
SECURITY_GROUP_ID=sg-0cbc97f44ed74d652
SECURITY_GROUP_ID_DEV=sg-0b36b590347ba8796
SECURITY_GROUP_ID_ADMIN=sg-04032188f428031fd
BACKUP_RETENTION=7


echo -e "\e[93mDeleting existing RDS instance ${TARGET_CLUSTER_INSTANCE_ID} ..."
aws rds delete-db-instance --db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID --skip-final-snapshot
echo -e "\e[93mWaiting for database deletion to complete..."
sleep 10
aws rds wait db-instance-deleted --db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID
echo -e "\e[92mFinished deleting old ${TARGET_CLUSTER_INSTANCE_ID} RDS instance."

EXISTING_CLUSTER_INSTANCE=$(aws rds describe-db-instances --db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID --query 'DBInstances[0].[DBInstanceIdentifier]' --output text)

echo -e "\e[93mDeleting existing cluster instance ${TARGET_CLUSTER_INSTANCE_ID} ..."
aws rds delete-db-cluster --db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID --skip-final-snapshot
echo -e "\e[93mWaiting for cluster deletion to complete..."

status="available"
while [ "$status" == "available" ] || [ "$status" == "deleting" ]; do
    sleep 10
    status=$(aws rds describe-db-clusters --db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID --query "*[].{DBClusters:Status}" --output text)

    echo " status = $status "
done

echo -e "\e[92mFinished deleting old ${TARGET_CLUSTER_INSTANCE_ID} cluster."

echo -e "\e[93mRestoring cluster ${SOURCE_CLUSTER_INSTANCE_ID} to new cluster ${TARGET_CLUSTER_INSTANCE_ID} ..."

CRUSTERRESTORECOMMAND="

aws rds restore-db-cluster-to-point-in-time \
--source-db-cluster-identifier $SOURCE_CLUSTER_INSTANCE_ID \
--db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID \
--restore-type copy-on-write \
--use-latest-restorable-time "

eval $CRUSTERRESTORECOMMAND

status=unknown
while [ "$status" != "available" ]; do
    sleep 10
    status=$(aws rds describe-db-clusters --db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID --query "*[].{DBClusters:Status}" --output text)
done

echo -e "\e[93mModifying cluster ${TARGET_CLUSTER_INSTANCE_ID} settings..."

CREATECLUSTERCOMMAND="
aws rds modify-db-cluster \
    --db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID \
    --master-user-password $NEW_MASTER_PASS \
    --vpc-security-group-ids $SECURITY_GROUP_ID $SECURITY_GROUP_ID_DEV $SECURITY_GROUP_ID_ADMIN \
    --backup-retention-period $BACKUP_RETENTION \
    --apply-immediately "

eval $CREATECLUSTERCOMMAND

status_modify=unknown
while [ "$status_modify" != "available" ]; do
    sleep 10
    status_modify=$(aws rds describe-db-clusters --db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID --query "*[].{DBClusters:Status}" --output text)

    echo -e "\e[92mModifications to ${TARGET_CLUSTER_INSTANCE_ID} complete."

done

echo " create RDS instance within new cluser ${TARGET_CLUSTER_INSTANCE_ID}."

CREATEDBCOMMAND="
aws rds create-db-instance \
--db-cluster-identifier $TARGET_CLUSTER_INSTANCE_ID \
--db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID \
--db-instance-class $TARGET_CLUSTER_INSTANCE_CLASS \
--publicly-accessible
--engine $TARGET_ENGINE "

eval $CREATEDBCOMMAND

# neeed to wait until the new db is in an available state

while [ "${exit_status3}" != "0" ]; do
    echo -e "\e[93mWaiting for ${TARGET_CLUSTER_INSTANCE_ID} to enter 'available' state..."
    aws rds wait db-instance-available --db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID
    exit_status3="$?"

    INSTANCE_STATUS=$(aws rds describe-db-instances --db-instance-identifier $TARGET_CLUSTER_INSTANCE_ID --query 'DBInstances[0].[DBInstanceStatus]' --output text)
    echo -e "\e[92m${TARGET_CLUSTER_INSTANCE_ID} is now ${INSTANCE_STATUS}."
    echo -e "\e[92mCreation of ${TARGET_CLUSTER_INSTANCE_ID} complete."
done

echo -e "\e[92mFinished clone of ${SOURCE_DB_INSTANCE_ID} to ${TARGET_CLUSTER_INSTANCE_ID}!"


end=$(date +%s)
runtime=$((end - start))
displaytime=$(displaytime runtime)

echo -e "\e[92mFinished clone of '${SOURCE_DB_INSTANCE_ID}' to '${TARGET_CLUSTER_INSTANCE_ID}"
echo -e "\e[92mThe script took ${displaytime} to run."
exit 0