腳本自動下載阿里雲每日備份數據庫鏡像

腳本自動下載阿里雲每日備份數據庫鏡像

背景

前端時間街道一個臨時需求,要求根據每日的數據快照,統計計算出須要數據結果,並寫入數據庫,提供查詢接口。
遇到兩個本身沒有嘗試過的點:html

  1. 阿里雲導出的數據庫是.frm和.ibd結構的數據,須要恢復的本身的服務器
  2. 須要自動化的定時拉取阿里雲每日生成的鏡像備份

今天的主要說的是,怎麼使用自動腳本拉取阿里雲備份的數據庫快照前端

解決

  1. 阿里雲提供API和SDK
  2. 選擇Python開發
  3. Python安裝aliyunsdkrds和aliyunsdkcore

參考連接
python SDK
APIpython

代碼

獲取每日備份URL的具體實現代碼,能夠根據需求本身修改mysql

#!/usr/bin/env python
# coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkrds.request.v20140815.DescribeBackupsRequest import DescribeBackupsRequest
import json
import sys
import getopt


def parser_args(argv):
    access_key = ''
    access_secret = ''
    region = ''
    db_instance_id = ''
    try:
        opts, args = getopt.getopt(argv, "hk:s:r:i:", ["access_key=", "access_secret=", "region=", "db_instance_id="])
    except getopt.GetoptError:
        print(
            'Get opt Error, '
            'usage: mysql_db_back_url.py -k <access_key> -s <access_secret> -r <region> -i <db_instance_id> '
            'ex: mysql_db_back_url.py -k 12312 -s 12312 -r cn-shanghai -i rm-uf6d4vg47vtr432z9')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(
                'Get opt Error, '
                'usage: mysql_db_back_url.py -k <access_key> -s <access_secret> -r <region> -i <db_instance_id>'
                'ex: mysql_db_back_url.py -k 12312 -s 12312 -r cn-shanghai -i rm-uf6d4vg47vtr432z9')
            sys.exit()
        elif opt in ("-k", "--access_key"):
            access_key = arg
        elif opt in ("-s", "--access_secret"):
            access_secret = arg
        elif opt in ("-r", "--region"):
            region = arg
        elif opt in ("-i", "--db_instance_id"):
            db_instance_id = arg

    return access_key, access_secret, region, db_instance_id


def main():
    access_key, access_secret, region, db_instance_id = parser_args(sys.argv[1:])

    client = AcsClient(access_key, access_secret, region)
    request = DescribeBackupsRequest()
    request.set_accept_format('json')
    request.set_DBInstanceId(db_instance_id)  # RDS數據庫實例名

    try:
        response = client.do_action_with_exception(request)
        response = str(response, encoding='utf-8')
        response = json.loads(response)
        url = response["Items"]["Backup"][0]["BackupDownloadURL"]
        return url
    except ServerException as e:
        print(e)
    except ClientException as e:
        print(e)


if __name__ == '__main__':
    print(main())

定時任務

shell 腳本 back.shsql

cur_date="`date  -d "1 day ago" +%Y%m%d`"
url="`python3 mysql_db_back_url.py -k your_access_key -s your_secret_id -r cn-shanghai -i aliyun_db_id`"
wget "$url" -O data$cur_date.tar.gz
tar -izxvf data$cur_date.tar.gz -C ~/mysql/data

crontab定時任務,天天凌晨1分開始拉取鏡像shell

1 0 * * * ~/mysql/back.sh > ~/mysql/back.log 2>&1
相關文章
相關標籤/搜索