from aliyunsdkcore.client import AcsClient
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest
import hashlib
import json
import requests
import datetime
import logging
import os
from urllib.request import urlopen
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
class Rds_Bakup:
def __init__(self):
self.access_key_id = "xxxxxxxxx"
self.access_key_secret="xxxxxxxxxxxxx"
self.region_id="cn-beijing"
self.db_id="xxxxxxxx"
self.product_code='RDS'
self.star_time = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ')
self.end_time = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
self.accept_format = "json"
self.accept_name = "DescribeBinlogFiles"
self.page_size = 100
self.page_num = 1
self.rds_bakup_info = {}
self.bakup_time = datetime.datetime.now().strftime('%Y-%m-%d')
self.rds_bakup_dir = "/data/rdsbak/binlog/{}".format(self.bakup_time)
if not os.path.exists(self.rds_bakup_dir):
os.mkdir(self.rds_bakup_dir)
def client_request(self):
client = AcsClient(self.access_key_id, self.access_key_secret, self.region_id)
request = DescribeBinlogFilesRequest.DescribeBinlogFilesRequest()
request.set_accept_format(self.accept_format)
request.set_action_name(self.accept_name)
request.set_DBInstanceId(self.db_id)
request.set_StartTime(self.star_time)
request.set_EndTime(self.end_time)
request.set_PageSize(self.page_size)
request.set_PageNumber(self.page_num)
return client, request
def rds_down(self):
result = self.client_request()[0].do_action_with_exception(self.client_request()[1]).decode()
s=json.loads(result)
for bak_item in s['Items']['BinLogFile']:
file_down_url = bak_item.get('IntranetDownloadLink')
file_name = "{}-{}-{}-{}".format(bak_item.get('HostInstanceID'), bak_item.get('LogBeginTime'), bak_item.get('LogEndTime'), bak_item.get('LogFileName')).replace(':', '_')
print(file_name, file_down_url)
req = urlopen(file_down_url)
log_file = "{0}/{1}_down.log".format(self.rds_bakup_dir, file_name)
file_handle = logging.FileHandler(log_file, encoding='utf-8')
self.logger = logging.getLogger(log_file)
self.logger.addHandler(file_handle)
chunk = 32 * 1024
try:
with open('{}/{}'.format(self.rds_bakup_dir, file_name), 'wb') as fobj:
while True:
data = req.read(chunk)
if not data:
break
fobj.write(data)
self.logger.info("下载成功 {}".format(bak_item))
sha_file = "{0}/{1}.SHA256".format(self.rds_bakup_dir, file_name)
sha_handle = logging.FileHandler(sha_file, encoding='utf-8')
self.logger = logging.getLogger(sha_file)
self.logger.addHandler(sha_handle)
hash_digest = hashlib.sha256()
with open('{}/{}'.format(self.rds_bakup_dir, file_name), 'rb') as hobj:
hash_digest.update(hobj.read())
self.logger.info("SHA256: {}".format(hash_digest.hexdigest()))
except Exception as e:
self.logger.info("下载出错 {}".format(e))
if __name__ == '__main__':
getfile = Rds_Bakup()
try:
getfile.rds_down()
except Exception as e:
logging.info("下载出错 {}".format(e))
利用阿里云RDS PYTHON-SDK进行数据库BinLog备份下载
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 问题:阿里云RDS数据库Binlog日志一直增大,撑爆磁盘 最后结果,是因为MySQL语句问题,没有where的条...
- 本案例是利用书中的示例美国农业部(USDA)制作了一份有关食物营养信息的数据库。这边提供一个JSON格式的文件 f...
- 第十四章 数据分析示例 注:本章示例数据集可在附带的GitHub仓库(http://github.com/wesm...
- 5月以来,哪怕对市场风向再不敏感的人,也感觉到阵阵凉意。二级市场连续下挫,一级市场融资环境恶化,不论企业融资数量还...