#!/usr/bin/python # coding=utf-8 from __future__ import print_function import datetime import json import sys try: import urllib.request as urllib_req except ImportError: import urllib2 as urllib_req from alibabacloud_rds20140815.client import Client as Rds20140815Client from alibabacloud_rds20140815.models import DescribeBackupsRequest from alibabacloud_tea_openapi import models as open_api_models if (len(sys.argv) != 5): print('use help:') print(''' get_rds_backup.py rm-xxxxxxx xxxxxxx xxxxxxxxx /mnt/ get_rds_backup.py RDS实例ID key secret 备份保存位置 key: Access Key ID secret:Access Key Secret 默认下载昨天的备份,时间可以修改,对应脚本中的变量 starttime 和 endtime ''') sys.exit(0) key = sys.argv[2] key_secret = sys.argv[3] rds_id = sys.argv[1] back_path = sys.argv[4] # back_path = "/mnt/" yesterday = str(datetime.date.today() + datetime.timedelta(days=-1)) today = str(datetime.date.today()) starttime = yesterday + "T00:00Z" print(yesterday, today) endtime = today + "T00:00Z" # key = '' # key_secret = '' region = "cn-hangzhou" # rds_id = ['rmmhlc,'rm-mc'] def download_rds_backfile(instanceid): config = open_api_models.Config( access_key_id=key, access_key_secret=key_secret, endpoint='rds.aliyuncs.com' ) clt = Rds20140815Client(config) req_bakup = DescribeBackupsRequest( dbinstance_id=instanceid, start_time=starttime, end_time=endtime ) backup = clt.describe_backups(req_bakup) # print (backup) jsload = backup.to_map()["body"] num = jsload["PageRecordCount"] print("backfiles:" + str(num)) if num == 0: print("no backupsets available in duration: " + str(starttime) + " to " + str(endtime)) print("download complete!") return i = 0 while i < num: bak_url = jsload["Items"]["Backup"][i]["BackupDownloadURL"] bak_host = jsload["Items"]["Backup"][i]["HostInstanceID"] bak_id = jsload["Items"]["Backup"][i]["BackupId"] print("BackupId:" + str(bak_id), "HostInstanceID:" + str(bak_host), "downloadurl:" + bak_url) save_name = back_path + bak_url.split('?')[0].split('/')[-1] u = urllib_req.urlopen(bak_url) f_header = u.info() print(f_header) bak_size = 0 try: bak_size = int(f_header.getheaders("Content-Length")[0]) except AttributeError: bak_size = int(f_header.get("Content-Length")[0]) print("backup file size: %s M ,file name: %s" % (bak_size / 1024 / 1024, save_name)) with open(save_name, "wb") as f: file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / bak_size) # status = status + chr(8) * (len(status) + 1) print(status) i = i + 1 print("download complete!") download_rds_backfile(rds_id)