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

Posted hxlasky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了输入时间参数获取rds备份集信息相关的知识,希望对你有一定的参考价值。

 

1.脚本

[[email protected] 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)+"
")
  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):	, str(Exception)
       print str(e):		, str(e)
       print repr(e):	, repr(e)
       print e.message:	, e.message

 

2.调用

[[email protected] tmp]# python geturl_test.py 20190220

以上是关于输入时间参数获取rds备份集信息的主要内容,如果未能解决你的问题,请参考以下文章

云图说|云数据库RDS跨区域备份

需要通过 Terraform 在 AWS RDS 中启用备份复制功能

如何在 CDK 应用程序中获取 RDS 实例主机名?

如何将阿里云的mysql(RDS)数据备份到本地

阿里云RDS恢复数据到本地上

如何让RDS 下的 Mysql 支持 Emoji 表情存储