使用Python脚本从rds上下载mysql的备份,该脚本可以下载全备份,日志备份.
主要用到以下几个模板.
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest
这几个模板是阿里云提供的模板,用户操作mysql的,具体用法可以参考阿里提供的api文档
附链接:
rds API的安装和使用
https://develop.aliyun.com/sdk/python?spm=5176.doc26319.2.2.aA2CCR
rds 所有api实例
https://help.aliyun.com/document_detail/26228.html?spm=5176.doc26223.6.259.ZagfU7
json:一种轻量级的数据转换格式
http://www.cnblogs.com/kaituorensheng/p/3877382.html
urllib:读取网页数据
http://www.111cn.net/phper/python/68713.htm
datetime:
http://blog.sina.com.cn/s/blog_6c3748830100ypt9.html
以下脚本正文:
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest
import json
import urllib
import datetime
# 获得时间需要备份的时间范围
def getdate():
today_time = datetime.datetime.now()
date1 = datetime.datetime.strftime(today_time, ‘%Y-%m-%d‘) # +‘T00:00:00Z‘
yes_time = today_time + datetime.timedelta(days=-2)
date2 = datetime.datetime.strftime(yes_time,‘%Y-%m-%d‘) # +‘T00:00:00Z‘
global start_date
global end_date
start_date = date2
end_date = date1
return 0
# 拉取指定db_instanceid的备份文件
def downfullbackupfile(db_instanceid):
startdate = start_date+‘T00:00Z‘
enddate = end_date+‘T00:00Z‘
clt = client.AcsClient(‘0ozYjkWe123456‘,‘kSVNVc89123456SY5tkFpUFXwPH‘,‘cn-hangzhou‘)
request = DescribeBackupsRequest.DescribeBackupsRequest()
request.set_accept_format(‘json‘)
request.set_action_name(‘DescribeBackups‘)
request.set_DBInstanceId(db_instanceid)
request.set_StartTime(startdate)
request.set_EndTime(enddate)
result = clt.do_action(request)
s=json.loads(result)
list = s[‘Items‘][‘Backup‘]
for i in list:
DBInstanceId = i[‘DBInstanceId‘]
url = i[‘BackupDownloadURL‘]
idx = url.index(‘tar.gz‘)
filename = url[7:idx+6].replace(‘/‘,‘_‘)
filename = "/backup/databackup/%s_%s" % (DBInstanceId,filename)
urllib.urlretrieve(url,filename)
# 拉取指定db_instanceid的备份文件
def downbinlogfile(db_instanceid):
startdate = start_date+‘T00:00:00Z‘
enddate = end_date+‘T00:00:00Z‘
clt = client.AcsClient(‘0ozYjkWeiHULnOjK‘,‘kSVNVc89zdFIMw4VPSY5tkFpUFXwPH‘,‘cn-hangzhou‘)
request = DescribeBinlogFilesRequest.DescribeBinlogFilesRequest()
request.set_accept_format(‘json‘)
request.set_action_name(‘DescribeBinlogFiles‘)
request.set_DBInstanceId(db_instanceid)
request.set_StartTime(startdate)
request.set_EndTime(enddate)
result = clt.do_action(request)
s=json.loads(result)
list = s[‘Items‘][‘BinLogFile‘]
for i in list:
DBInstanceId = db_instanceid
url = i[‘DownloadLink‘]
idx = url.index(‘.tar?OSSAccessKeyId‘)
filename = url[7:idx+4].replace(‘/‘,‘_‘)
filename = "/backup/binlogbackup/%s_%s" % (DBInstanceId,filename)
urllib.urlretrieve(url,filename)
#需要备份的db_instanceid
def dblist():
dblist = [‘rm-bp1tg9vcgd0z775jj11pfi‘,‘rdss5r8cmjgisdf52zkx290‘]
for db in dblist:
downfullbackupfile(db)
downbinlogfile(db)
try:
if __name__ == "__main__":
getdate()
dblist()
except Exception,e:
print e
python下载rds备份文件实例
http://www.tuicool.com/articles/eAvyu2
rds API的安装和使用
https://develop.aliyun.com/sdk/python?spm=5176.doc26319.2.2.aA2CCR
rds 所有api实例
https://help.aliyun.com/document_detail/26228.html?spm=5176.doc26223.6.259.ZagfU7
本文出自 “SQLServer MySQL” 博客,请务必保留此出处http://dwchaoyue.blog.51cto.com/2826417/1859833
原文地址:http://dwchaoyue.blog.51cto.com/2826417/1859833