從rds上下載備份和binlog
使用Python指令碼從rds上下載mysql的備份,該指令碼可以下載全備份,日誌備份.
主要用到以下幾個模板.
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest
這幾個模板是阿里雲提供的模板,使用者操作mysql的,具體用法可以參考阿里提供的api文件
附連結:
rds API的安裝和使用
rds 所有api例項
json:一種輕量級的資料轉換格式
urllib:讀取網頁資料
datetime:
以下指令碼正文:
#!/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備份檔案例項
rds API的安裝和使用
rds 所有api例項