通过AWS Command Line从AWS RDS导出MySQL转储

时间:2016-09-07 05:52:45

标签: mysql amazon-web-services amazon-s3 amazon-ec2 amazon-rds

我正在尝试从AWS RDS自动获取MySQL备份。我认为使用AWS Command Line会很有用,我可以在EC2 Red Hat上使用crontab来自动触发事件。

现在的问题是:如何连接到RDS,备份MySQL,将其放在EC2上或复制到S3并让它每晚运行。

我是AWS Command Line的新手。请随意删除建议和代码段。

谢谢!

2 个答案:

答案 0 :(得分:4)

  • 在EC2实例上创建一个cron.sh文件并放在内容之下。

    mysqldump -h RDS_ENPOINT -u MASTER_USER_DATABASE -p DATABASE_NAME > /backup/bkp.$(date +%Y%m%d).sql

  • 创建其他文件move_to_s3.sh并放在内容

    之下
        #!/bin/bash
        echo "starting upload to s3 ..."
        TODAY=$(date +%Y%m%d);
        month=$(date +"%m");
        year=$(date +"%Y");
        bucket="mybkp"
        file="$year/$month/bkp.$TODAY.tar"
        filepath="/backup/bkp.$TODAY.tar"
        resource="/${bucket}/${file}"
        contentType="application/x-compressed-tar"
        dateValue=`date -R`
        stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
        s3Key=AKIAI7BE3RKNSsdfsdfASF
        s3Secret=sdfksdfkJsdfgd76sdfkljhdfsdfsdfsdf
        signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64`;
    
        RESPONSE=$(curl -w "%{http_code}" -s -X PUT -T "${filepath}" \
                -H "Host: ${bucket}.s3.amazonaws.com" \
                -H "Date: ${dateValue}" \
                -H "Content-Type: ${contentType}" \
                -H "Authorization: AWS ${s3Key}:${signature}" \
                https://${bucket}.s3.amazonaws.com/${file} -o /dev/null $1);
    
        echo $RESPONSE;
        if [ $RESPONSE -ne 200 ] ; then
            echo "There was an issue in transfering DB dbbackup file to S3. Noticed Error Code: $RESPONSE" | mail -s "Issue on transfer to S3" test@gmail.com;
        else
            rm $filepath;
        fi
        echo "finished upload."`
    

在半小时距离内设置cron两个!

希望有所帮助:)

答案 1 :(得分:2)

您可以使用mysqldump直接从EC2进行备份。

  1. 编辑RDS实例的安全组入站规则

    键入:Mysql / Aurora

    协议:TCP

    港口范围:3306

    来源:自定义EC2_SECURITY_GROUP_ID

    示例(来源:自定义sg_451caa43)

  2. 使用SSH连接到EC2实例:

    [MacBook-Pro: user]$ ssh -i keypair.pem ec2-user@PUBLIC_IP

  3. 在EC2实例中安装mysql-client:

    [ec2-user@ip-170-10-20-30]$ sudo yum install mysql

  4. 尝试 mysqldump 命令

  5. [ec2-user@ip-170-10-20-30]$ mysqldump -h RDS_ENPOINT -u MASTER_USER_DATABASE -p DATABASE_NAME > backup.sql

    [ec2-user@ip-170-10-20-30]$ mysqldump -h db_test.cdsludsd.us-west-2.rds.amazonaws.com -u admin -p my_database > backup_my_database.sql
    
    1. 创建cron作业。