代码之家  ›  专栏  ›  技术社区  ›  Smruti Mandal

使用CLI克隆AWS Aurora MySQL数据库

  •  0
  • Smruti Mandal  · 技术社区  · 7 年前

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

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

    docs ,我可以创建另一个极光集群,但它不能创建DBs。它只是创建了一个空集群。我无法理解从源集群中的数据库快照在该集群中创建新数据库的命令,因为从数据库快照恢复数据库实例是 not supported 对于Aurora MySQL。

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

    1 回复  |  直到 7 年前
        1
  •  6
  •   chris    7 年前

    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)
    
        2
  •  0
  •   Lukasz    7 年前

    答案是正确的,一个重要的细节,没有提到,使我认为它没有工作,是不一定安全策略将是相同的,因此,为了使数据库可用,您需要设置相同或适当的,并使数据库公开。我提供了一些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()));
    
        3
  •  0
  •   Peter Drinnan    6 年前

    极光DB克隆的概念花了我一段时间才弄明白。使用Aurora,数据实际上是集群的一部分。数据库实例从集群获取其数据。要将一个Aurora群集克隆到另一个群集,您需要克隆该群集,然后在新群集中创建一个DB实例。在新集群中创建的db实例将从创建它的集群中获取数据。呸!这是一个很长的解释。无论如何,下面的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