Python 实现上传 RDS 备份至阿里云 OSS

7,911 views次阅读
2条评论

需求

RDS 备份仅提供下载链接,需要将 RDS 的备份文件上传至 OSS 永久保存

Python

代码实现思路

获取 RDS 下载链接 --> 下载文件至本地 --> 上传至 OSS

代码

获取 RDS 下载链接

def get_RDS_info(self):
    print("[Message]  Fount Instance ID: %s" % self.db_instanceid)
    startdate = start_date
    enddate = end_date
    clt = client.AcsClient(self.secret_id, self.secret_key)
    request = DescribeBackupsRequest.DescribeBackupsRequest()
    request.set_accept_format('json')
    request.set_action_name('DescribeBackups')
    request.set_DBInstanceId(self.db_instanceid)
    request.set_StartTime(startdate)
    request.set_EndTime(enddate)
    result = clt.do_action_with_exception(request)
    s = json.loads(result.decode('utf-8'))
    datadir = "D:/tmp"
    list = s['Items']['Backup']
    URL = list[0]['BackupDownloadURL']
    idx = URL.index('tar.gz')
    filename = URL[8:idx + 6].replace('/', '_')
    savename = "%s/%s" % (datadir, filename)
    print("[Message]  Save to: %s" % filename)
    self.downfile(URL, savename)
    return savename

下载至本地

def downfile(self, url, filename):
    urllib.request.urlretrieve(url, filename, self.Schedule)
    return 1

上传至 OSS

def upload2oss(self, file):
    auth = oss2.Auth(self.secret_id, self.secret_key)
    bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com', self.bucket_name)
    objname = self.db_instanceid + '_' + file.split('_')[-1]
    try:
        oss2.resumable_upload(bucket, objname, file)
        print('[Message]  Upload to OSS Successfully.')
    except Exception as e:
        print(e)

最终代码

代码

# -*- coding:utf-8 -*-
from aliyunsdkcore import client
from aliyunsdkrds.request.v20140815 import DescribeBackupsRequest
import oss2
import configparser
import json,datetime,urllib.request
class RDS(object):
    def __init__(self):
        config = configparser.ConfigParser()
        config.read('config.txt')
        self.secret_id = config['common']['access_id']
        self.secret_key = config['common']['access_key']
        self.rds_region = config['rds']['region']
        self.db_instanceid = config['rds']['dbinstanceid']
        self.oss_region = config['oss']['region']
        self.bucket_name = config['oss']['bucket_name']
    def getdate(self):
        global start_date
        global end_date
        ti = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M')
        time1 = datetime.datetime.strptime(ti, '%Y-%m-%d %H:%M')
        time11 = time1 - datetime.timedelta(hours=8)
        end_date = datetime.datetime.strftime(time11, '%Y-%m-%dT%H:%MZ')
        time22 = time11 - datetime.timedelta(days=1)
        start_date = datetime.datetime.strftime(time22, '%Y-%m-%dT%H:%MZ')
        return 0
    def get_RDS_info(self):
        print("[Message]  Fount Instance ID: %s" % self.db_instanceid)
        startdate = start_date
        enddate = end_date
        clt = client.AcsClient(self.secret_id,self.secret_key)
        request = DescribeBackupsRequest.DescribeBackupsRequest()
        request.set_accept_format('json')
        request.set_action_name('DescribeBackups')
        request.set_DBInstanceId(self.db_instanceid)
        request.set_StartTime(startdate)
        request.set_EndTime(enddate)
        result = clt.do_action_with_exception(request)
        s = json.loads(result.decode('utf-8'))
        datadir = "D:/tmp"
        list = s['Items']['Backup']
        URL = list[0]['BackupDownloadURL']
        idx = URL.index('tar.gz')
        filename = URL[8:idx + 6].replace('/', '_')
        savename = "%s/%s" % (datadir, filename)
        print("[Message]  Save to: %s" % filename)
        self.downfile(URL, savename)
        return savename
    def Schedule(slef,a, b, c):
        per = 100.0 * a * b / c
        if per > 100:
            per = 100
            print('%.2f%%' % per)
    def downfile(self,url, filename):
        urllib.request.urlretrieve(url, filename, self.Schedule)
        return 1
    def upload2oss(self, file):
        auth = oss2.Auth(self.secret_id, self.secret_key)
        bucket = oss2.Bucket(auth, 'http://oss-cn-shanghai.aliyuncs.com', self.bucket_name)
        objname = self.db_instanceid + '_' + file.split('_')[-1]
        try:
            oss2.resumable_upload(bucket, objname, file)
            print('[Message]  Upload to OSS Successfully.')
        except Exception as e:
            print(e)
if __name__ == '__main__':
    RDSOper = RDS()
    RDSOper.getdate()
    file = RDSOper.get_RDS_info()
    RDSOper.upload2oss(file)

配置文件

[common]
# access_id
access_id = LTA

# access_key
access_key = GwfAMvR4K2EL

[rds]
# RDS Region
region = cn-shanghai

# RDS DB instance ID
dbinstanceid = rm-uf6l45

[oss]
# OSS Region
region = cn-shanghai

# OSS bucketname
bucket_name = anchnet

运行测试

RDS 控制台备份文件

Python

脚本执行,获取当天备份链接

Python

OSS 控制台查看

Python

Honest1y
版权声明:本站原创文章,由Honest1y于2018年09月17日发表,共计6327字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(2条评论)
头像
闲鱼 评论达人LV.1
2018-09-17 19:37:26 回复

阿里云 OSS之前有用来备份网站,后来用了阿里云快照之后就没用了

    Clannad
    Clannad 博主
    2018-09-18 09:09:50 回复

    @闲鱼 阿里的快照不是已经商业化开始收费了吗?