通过python定时下载阿里云RDS二进制日志

需求:数据库使用的阿里云RDS,上面设置的日志保存为一个月,需要保存一个月之后的二进制日志。
方法:通过python脚本对接阿里云api接口,结合crontab定时任务可实现定时下载即将被清除的日志。
脚本如下:

#!/usr/bin/env python3# -*- coding: utf-8 -*-# @Time: 2018-12-12 13:52# @Author: opsonly# @Site:# @File: rds_binlog.py# @Software: PyCharm'''下载阿里云rds binlog日志'''import base64,urllib.requestimport hashlibimport hmacimport osimport uuid,time,json,wgetimport time,datetimeclass RDS_BINLOG_RELATE(object):def __init__(self):self.access_id = 'xxxxxxxxx' #阿里云access_idself.access_key = 'xxxxxxxxx' #阿里云access_key#通过id和key来进行签名def signed(self):today = datetime.date.today()first = today.replace()lastMonth = first - datetime.timedelta(days=30)lastMonth2 = first - datetime.timedelta(days=29)starttime = lastMonth.strftime("%Y-%m-%dT%H:%M:%SZ")endtime = lastMonth2.strftime("%Y-%m-%dT%H:%M:%SZ")timestamp = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())header = {'Action': 'DescribeBinlogFiles','DBInstanceId': 'rm-xxxxxxxxxx', #RDS实例id'StartTime': starttime,'EndTime': endtime,'Format': 'JSON','Version': '2014-08-15','AccessKeyId': self.access_id,'SignatureVersion': '1.0','SignatureMethod': 'HMAC-SHA1','SignatureNonce': str(uuid.uuid1()),'TimeStamp': timestamp,}#对请求头进行排序sortedD = sorted(header.items(), key=lambda x: x[0])url = 'https://rds.aliyuncs.com'canstring = ''#将请求参数以#连接for k, v in sortedD:canstring += '&' + self.percentEncode(k) + '=' + self.percentEncode(v)#对请求连接进行阿里云要的编码规则进行编码stiingToSign = 'GET&%2F&' + self.percentEncode(canstring[1:])bs = self.access_key + '&'bs = bytes(bs, encoding='utf8')stiingToSign = bytes(stiingToSign, encoding='utf8')h = hmac.new(bs, stiingToSign, hashlib.sha1)stiingToSign = base64.b64encode(h.digest()).strip()#将签名加入到请求头header['Signature'] = stiingToSign#返回urlurl = url + "/?" + urllib.parse.urlencode(header)return url#按照规则替换def percentEncode(self,store):encodeStr = storeres = urllib.request.quote(encodeStr)res = res.replace('+', '%20')res = res.replace('*', '%2A')res = res.replace('%7E', '~')return str(res)#筛选出链接下载二进制日志文件def getBinLog(self):binlog_url = self.signed()req = urllib.request.urlopen(binlog_url)req = req.read().decode('utf8')res = json.loads(req)logDir = '/data/backup/mysqlbinlog/'ntoday = datetime.date.today()nfirst = ntoday.replace()nlastMonth = nfirst - datetime.timedelta(days=30)bakmonth = nlastMonth.strftime("%Y%m%d")backdir = logDir + bakmonthos.mkdir(backdir)os.chdir(backdir)for i in res['Items']['BinLogFile']:wget.download(i['IntranetDownloadLink'])s = RDS_BINLOG_RELATE()s.getBinLog()

脚本地址:https://github.com/opsonly,上面还有许多阿里云api脚本和常用shell脚本,欢迎star。
喜欢我写的东西的朋友可以关注一下我的公众号,上面有我的学习资源以及一些其他福利。:Devops部落
通过python定时下载阿里云RDS二进制日志
文章图片

    推荐阅读