#coding:utf8
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest
import json
import urllib
import datetime
import os
def binlogmobile(db_instanceid):
today_time = datetime.datetime.now()
date1 = datetime.datetime.strftime(today_time, '%Y-%m-%d') # +‘T00:00:00Z‘
yes_time = today_time + datetime.timedelta(days=-1)
date2 = datetime.datetime.strftime(yes_time,'%Y-%m-%d') # +‘T00:00:00Z‘
start_date = date2
end_date = date1
startdate = start_date+'T00:00:00Z'
enddate = end_date+'T00:00:00Z'
cur_dir = '/logs/mysql/mobile/'
folder_name = date1
os.mkdir(os.path.join(cur_dir, folder_name))
clt = client.AcsClient('accesskeyid','密码','cn-beijing')
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:
url = i['DownloadLink']
idx = url.split('/')[5].split('?')[0]
filename = "/logs/mysql/mobile/%s/%s" % (date1,idx)
urllib.urlretrieve(url,filename)
binlogmobile('rdsid')