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

1,144次阅读

共计 3689 个字符,预计需要花费 10 分钟才能阅读完成。

需求

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

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

代码实现思路

获取 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 实现上传 RDS 备份至阿里云 OSS

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

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

OSS 控制台查看

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

正文完
 
mervinwang
版权声明:本站原创文章,由 mervinwang 2018-09-17发表,共计3689字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
文章搜索