from aliyunsdkcore.client import AcsClient
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
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 = "xxxx"
self.access_key_secret="xxxxxxxxxxxxxx"
self.region_id="cn-beijing"
self.db_id="xxxxxxxxxxxxx"
self.product_code='RDS'
self.star_time = (datetime.datetime.utcnow() - datetime.timedelta(days=1)).strftime('%Y-%m-%dT%H:%MZ')
self.end_time = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%MZ')
self.accept_format = "json"
self.accept_name = "DescribeBackups"
self.bakup_time = datetime.datetime.now().strftime('%Y-%m-%d')
self.rds_bakup_dir = "/data/rdsbak/fullbak/{}".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 = DescribeBackupsRequest.DescribeBackupsRequest()
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)
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']['Backup']:
for k,v in bak_item.items():
if k == "BackupIntranetDownloadURL" :
file_down_url = v
file_name = v.split('?')[0].split('/')[-1]
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('{0}/{1}'.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(s))
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:
while True:
data = hobj.read(chunk)
if not data:
break
hash_digest.update(data)
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()
logging.info("下载成功 !!!")
except Exception as e:
logging.info("下载出错 {}".format(e))
利用阿里云RDS PYTHON-SDK进行数据库全量备份下载
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本案例是利用书中的示例美国农业部(USDA)制作了一份有关食物营养信息的数据库。这边提供一个JSON格式的文件 f...
- 第十四章 数据分析示例 注:本章示例数据集可在附带的GitHub仓库(http://github.com/wesm...
- 黑色的海岛上悬着一轮又大又圆的明月,毫不嫌弃地把温柔的月色照在这寸草不生的小岛上。一个少年白衣白发,悠闲自如地倚坐...