通過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部落

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章