使用CLI的AWS Aurora MySQL数据库克隆

时间:2018-12-12 15:02:37

标签: amazon-web-services amazon-rds amazon-rds-aurora

我想每周创建我的生产aurora mysql数据库的副本。这些副本将用于开发。

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

docs之后,我可以创建另一个Aurora群集,但是它不能创建数据库。它只是创建一个空集群。我无法从源集群中Db的快照中找出在该集群中创建新Db的命令,因为Aurora MySQL的restore-db-instance-from-db-snapshot为not supported

请让我知道用于克隆Aurora群集及其内部DB的命令。

3 个答案:

答案 0 :(得分:3)

根据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)

答案 1 :(得分:0)

答案是正确的,没有提到一个重要的细节,并且导致我认为它没有用,那就是安全策略不一定相同,因此,要使数据库可用,您需要设置相同的值或适当加上数据库公开。我提供了一些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()));

答案 2 :(得分:0)

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