美文网首页Devops
通过python定时下载阿里云RDS二进制日志

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

作者: 别玩游戏知道吗 | 来源:发表于2019-03-13 18:39 被阅读1次

    需求:数据库使用的阿里云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.request
    
    import hashlib
    
    import hmac
    
    import os
    
    import uuid,time,json,wget
    
    import time,datetime
    
    class RDS_BINLOG_RELATE(object):
    
        def __init__(self):
    
            self.access_id = 'xxxxxxxxx' #阿里云access_id
    
            self.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
    
            #返回url
    
            url = url + "/?" + urllib.parse.urlencode(header)
    
            return url
    
        #按照规则替换
    
        def percentEncode(self,store):
    
            encodeStr = store
    
            res = 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 + bakmonth
    
            os.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二进制日志

        本文链接:https://www.haomeiwen.com/subject/lsntmqtx.html