码迷,mamicode.com
首页 > 其他好文 > 详细

输入时间参数获取rds备份集信息

时间:2019-02-25 15:02:17      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:retrieve   response   scl   end   save   def   from   tar   dbi   

 

1.脚本

[root@localhost tmp]# more geturl_test.py 
#!/usr/bin/env python
#coding=utf-8

import os, json, urllib, datetime, shutil,sys
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
from aliyunsdkrds.request.v20140815 import DescribeBinlogFilesRequest

today_time = datetime.datetime.now()
yes_time = today_time + datetime.timedelta(days = -1)
remain_time=today_time + datetime.timedelta(days = -2)
save_time = today_time + datetime.timedelta(days = -3)
instance_id=testid
backup_str = sys.argv[1] #传入的时间格式为YYYYMMDD
backup_date = datetime.datetime.strptime(backup_str, %Y%m%d) #转换成时间
end_backup_date=backup_date+datetime.timedelta(days = 1) #开始时间加上一天


def Downfullbackupfile(db_instanceid):
        startdate = datetime.datetime.strftime(backup_date,%Y-%m-%d) + T00:00Z
        enddate = datetime.datetime.strftime(end_backup_date, %Y-%m-%d) + T00:00Z

        clt = client.AcsClient(test,test,test-test)
        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)
        response = clt.do_action_with_exception(request)
        #print response
        result=json.loads(response)
        list = result[Items][Backup]
        for i in list:
                DBInstanceId = i[DBInstanceId]
                BackupSize = i[BackupSize]
                url = i[BackupDownloadURL]
                idx_start = url.index(hins)
                idx_end = url.index(tar.gz)
                file = url[idx_start:idx_end + 6].replace(/, _)
                pathname = /home/oracle/backup/%s/%s % (DBInstanceId, datetime.datetime.strftime(yes_time,%Y%m%d))
                if (os.path.isdir(pathname) == False):
                        os.makedirs(pathname)
                filename = %s/%s % (pathname, file)
                #urllib.urlretrieve(url, filename)
                print("%s|%s|%s"%(url ,BackupSize ,file))
        return BackupSize,file 

def record_log(instance_id,flag,source_filesize,dest_filesize):
  system_date = datetime.datetime.strftime(datetime.datetime.now(),%Y%m%d %H:%M:%S)
  backup_datetime = datetime.datetime.strftime(today_time,%Y%m%d %H:%M:%S)
  backup_date = datetime.datetime.strftime(yes_time,%Y%m%d)
  logfile="/home/oracle/scripts/backup_log/"+instance_id+".log"
  fp = open(logfile, "a+")
  fp.write(backup_date+"|"+instance_id+"|"+flag+"|"+backup_datetime+"|"+system_date+"|"+str(source_filesize)+"|"+str(dest_filesize)+"\n")
  fp.close

def check_backupfile_exist_flag(filepath):
  try:
    exist_flag=0
    for filename in os.listdir(filepath):
      if filename.strip():
        exist_flag=1
  finally:
    return exist_flag


def delete_file(instance_id):
  savepath = /home/oracle/backup/%s/%s % (instance_id, datetime.datetime.strftime(save_time,%Y%m%d))
  if (os.path.isdir(savepath)):
    shutil.rmtree(savepath)


def Backupdata():
        instance_list = [instance_id]
        for instance in instance_list:
                Downfullbackupfile(instance)

##new add
def get_FileSize(filePath):
    #filePath = unicode(filePath,utf8)
    fsize = os.path.getsize(filePath)
    return fsize


try:
        if __name__ == __main__:
                #filename_dir="/home/oracle/backup/"+instance_id+"/"+datetime.datetime.strftime(yes_time,%Y%m%d)
                #remain_filename_dir="/home/oracle/backup/"+instance_id+"/"+datetime.datetime.strftime(remain_time,%Y%m%d)
                #remain_filename_exist_flag=check_backupfile_exist_flag(remain_filename_dir)
                #if remain_filename_exist_flag==1:
                #  delete_file(instance_id)
                
                source_backupsize,source_filename=Downfullbackupfile(instance_id)
                #check_dest_filename=filename_dir+"/"+source_filename
                #dest_filesize=get_FileSize(check_dest_filename)

                #file_exist_flag=check_backupfile_exist_flag(filename_dir)
                #if (file_exist_flag==1) and (dest_filesize>=source_backupsize):
                #  record_log(instance_id,"ok",source_backupsize,dest_filesize)
                #if file_exist_flag==0:
                #  record_log(instance_id,"failed",source_backupsize,dest_filesize)
except Exception,e:
       record_log(instance_id,"failed",0,0)
       print str(Exception):\t, str(Exception)
       print str(e):\t\t, str(e)
       print repr(e):\t, repr(e)
       print e.message:\t, e.message

 

2.调用

[root@localhost tmp]# python geturl_test.py 20190220

输入时间参数获取rds备份集信息

标签:retrieve   response   scl   end   save   def   from   tar   dbi   

原文地址:https://www.cnblogs.com/hxlasky/p/10430796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!