2016-09-07 54 views
0

我試圖從AWS RDS自動獲取MySQL備份。我認爲使用AWS命令行會很有幫助,我可以在EC2 Red Hat上使用crontab來自動觸發事件。通過AWS命令行從AWS RDS導出MySQL轉儲

現在的問題:我該如何連接到RDS,備份MySQL,將它放在EC2上,或者複製到S3並讓它每天晚上運行。

我是AWS命令行的新手。請隨意刪除建議和代碼片段。

謝謝!

+0

可能重複的[MySQL定期備份MySQL數據](http:// stackoverflow。COM /問題/ 38916163/PHP的常規備份的MySQL的數據) – e4c5

回答

2

您可以使用mysqldump直接從EC2進行備份。

  1. 編輯安全組的RDS實例的入站規則

    類型:MySQL /極光

    協議:TCP

    端口範圍:3306

    來源:自定義EC2_SECURITY_GROUP_ID

    實施例(來源:定製sg_451caa43)

  2. 連接到EC2實例與SSH:

    [MacBook-Pro: user]$ ssh -i keypair.pem [email protected]_IP

  3. 在EC2實例中安裝的MySQL的客戶端:

    [[email protected]]$ sudo yum install mysql

  4. 嘗試mysqldump的命令

[[email protected]]$ mysqldump -h RDS_ENPOINT -u MASTER_USER_DATABASE -p DATABASE_NAME > backup.sql

[[email protected]]$ mysqldump -h db_test.cdsludsd.us-west-2.rds.amazonaws.com -u admin -p my_database > backup_my_database.sql 
  • 創建計劃作業。
  • 3
    • 在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" [email protected]; 
          else 
           rm $filepath; 
          fi 
          echo "finished upload."` 
      

    都設置在cron在半小時的距離!

    希望它有幫助:)