rds下载备份集

python版本
[testuser@localhost tmp]$ python -V
Python 2.7.5

需要提前安装RDS
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install python-pip
[root@localhost ~]# pip install aliyun-python-sdk-core
[root@localhost ~]# pip install aliyun-python-sdk-rds
[root@localhost ~]# pip install --upgrade pip

 

以下脚本需要提前创建备份日志文件:

echo>/home/test/scripts/backup_log/实例id.log

 

#!/usr/bin/env python
#coding=utf-8

import os, json, urllib, datetime, shutil
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'

def Downfullbackupfile(db_instanceid):
        startdate = datetime.datetime.strftime(yes_time,'%Y-%m-%d') + 'T00:00Z'
        enddate = datetime.datetime.strftime(today_time, '%Y-%m-%d') + 'T00:00Z'
        clt = client.AcsClient('test','test','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)
        response = clt.do_action_with_exception(request)
        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/test/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)
        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/test/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/test/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/test/backup/"+instance_id+"/"+datetime.datetime.strftime(yes_time,'%Y%m%d')
                remain_filename_dir="/home/test/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

 

posted @ 2019-01-18 10:15  slnngk  阅读(429)  评论(0编辑  收藏  举报