前端時間街道一個臨時需求,要求根據每日的數據快照,統計計算出須要數據結果,並寫入數據庫,提供查詢接口。
遇到兩個本身沒有嘗試過的點:html
今天的主要說的是,怎麼使用自動腳本拉取阿里雲備份的數據庫快照前端
參考連接
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