- A+
所属分类:python脚本
需求:全量备份数据库,但是数据库比较大,如果采用mysqldump在其他机器备份会大量占用mysql,所以采用全量备份,可以采用登录RDS控制台来手动点击备份,全量备份。但是太麻烦,所以研究了阿里api文档写了python脚本放在linux下用定时任务自动备份
说明:
1.每天最多备份10次
2.RDS必须支持全量备份(集群版都支持,单机版不支持)
python环境
py3.6
pip install aliyun-python-sdk-rds
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#/usr/bin/env python from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkrds.request.v20140815.CreateBackupRequest import CreateBackupRequest from aliyunsdkcore.profile import region_provider region_provider.modify_point('Rds', 'cn-shenzhen', 'rds.aliyuncs.com') import json client = AcsClient( "**********", "*************", "cn-shenzhen" ) #备份RDS request = CreateBackupRequest() request.set_DBInstanceId("**********") request.set_action_name("CreateBackup") try: response = client.do_action_with_exception(request) except Exception as e: print(e) |
参数说明
1.client里面AcsClient填写的参数
1 2 3 4 |
client = AcsClient( "<access-key-id>", "<access-key-secret>", "<region-id>") <access-key-id><access-key-secret>是阿里云RAM编程管理的 region-id参考https://help.aliyun.com/document_detail/100865.html?spm=a2c4g.11186623.6.1362.1f4f38875goRqN下表1,如我是深圳就是cn-shenzhen |
2.modify_point参数说明,第二个是<region-id>
1 2 |
region_provider.modify_point('Rds', 'cn-shenzhen', 'rds.aliyuncs.com') region-id参考https://help.aliyun.com/document_detail/100865.html?spm=a2c4g.11186623.6.1362.1f4f38875goRqN下表1 |
执行截图
然后发现在控制台自动就创建了备份
参考文档:
https://help.aliyun.com/document_detail/100865.html?spm=a2c4g.11186623.6.1362.1f4f38875goRqN
https://help.aliyun.com/document_detail/26272.html?spm=a2c4g.11186623.6.1318.537f30e5J31yTe
https://help.aliyun.com/document_detail/67115.html?spm=a2c4g.11186623.6.602.4dac10f0zIte1Z
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫