通过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.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部落

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天早上我的学员终于生了,女孩,特别结实,特别可爱。 第一胎,十二点多去医院,两点开两指,四点开四指,五点半十指全...
    涡孩缇阅读 1,683评论 0 1
  • 女人买衣服,永远都不够。衣柜里,总少了那件最想穿的。但是,怎么给宝宝买衣服,也变成这样了呢?我翻着宝宝的衣柜,这越...
    珍妮LEE阅读 4,112评论 0 0
  • 042.昨天开联席会,虽然感慨这次考的差,但是老师们也都肯定了班级纪律好很多了,尤其物理老师说,上课心情好多了,看...
    黎明初晓阅读 1,027评论 0 0
  • 2018 年11 月14日 阴 在透过阳光的缝隙中,我在休息室爬楼听写作训练营的第三课,谈自己写作的初心,内心有点...
    寻梦如初阅读 3,153评论 0 5
  • 其实迷茫就是人生的一种常态,不管你是什么身份,也不管你从事什么工作。 不论是你少年,青年,中年,或者是老年,人生中...
    安乐无忧阅读 2,910评论 0 5