ReRDS备份文件自动下载工具-v1.0已更新支持交互式设置
系统 CENTOS6.5
当python getfile.py时,正常运行;
把getfile.py交互改为固定值,也正常运行;
一旦用crontab定时任务跑getfile.py时 (crontab脚步运行其他脚步正常,运行getfile.py时跑到38行报错),报错;
错误代码:
decode() argument 1 must be string , not None
出错行在(
38):
# -*- coding: utf-8 -*-'''Created on 2015-1-27@author: muyuan'''import sys,os,shutil,stringimport reimport urllib,urllib2,requestsimport datetime,timeimport aliyun.api#这里写id 和 密钥aliyun.setDefaultAppInfo("id", "secret")print u"请先设置aliyun.setDefaultAppInfo的Access Key ID和Access Key Secret"print u"请输入您的RDS所在区域,如cn-hangzhou"prompt = "Aliyun>"my_diqu = raw_input(prompt)print u"请输入您的RDS实例名"my_rdsname = raw_input(prompt)print u"请输入您要下载备份的开始时间如2015-01-15"my_begintime = raw_input(prompt)print u"请输入您要下载的备份结束时间如2015-01-18"my_overtime = raw_input(prompt)#查询备份信息,设置相关条件a = aliyun.api.Rds20140815DescribeBackupsRequest()a.RegionId = my_diqu #% (diqu)a.DBInstanceid = my_rdsname # % (rdsname)a.StartTime = my_begintime + "T00:00Z" # % (begintime)a.EndTime = my_overtime + "T00:00Z" #获取备份列表try: f = a.getResponse() # 此处报错: decode() argument 1 must be string , not None if("Code" in f): print("Fasle") print(f["Code"]) print(f["Message"]) else: f = a.getResponse() # 此处报错: decode() argument 1 must be string , not None # print(f) f = str(f) #格式化输出 # b=re.compile(r"(http://.*?)',") my_re = re.compile(r"(http://.*?)',") #正则匹配 print my_re.findall(f) my_data = my_re.findall(f) my_len = len(my_data) #计算list的元素数 ###开始下载 # print "%r~%r,you have %r backup file ,start download,please wait," % (a.StartTime, a.EndTime, d) print u"从%r到%r,有%r个备份文件可以下载 ,正在下载,请稍候," % (a.StartTime, a.EndTime, my_len) my_url = my_data for m in my_url: # u = urllib2.urlopen(m) # y = u.read() urllib.urlretrieve(m, "muyuan.tar.gz") now = time.strftime("%Y%m%d-%H%M%S") newname = now + ".tar.gz" shutil.move("muyuan.tar.gz",newname) print u"下载完成,请查看脚本运行的当前目录下是否已经成功下载文件"except Exception,e: print(e)
代码中引用包: aliyun.api.Rds20140815DescribeBackupsRequest()
包内init code :
'''Created by auto_sdk on 2014-10-22 12:49:31'''from aliyun.api.base import RestApiclass Rds20140815DescribeBackupsRequest(RestApi): def __init__(self,domain='rds.aliyuncs.com',port=80): RestApi.__init__(self,domain, port) self.OwnerId = None self.OwnerAccount = None self.ResourceOwnerAccount = None self.BackupId = None self.BackupMode = None self.BackupStatus = None self.DBInstanceId = None self.EndTime = None self.PageNumber = None self.PageSize = None self.StartTime = None def getapiname(self): return 'rds.aliyuncs.com.DescribeBackups.2014-08-15'