Skip to main content

Aliyun RDS Backup Tool

这是一个按照自定义配置,从阿里云RDS(目前只支持MySQL)下载数据库备份(包括全备和binlog备份)到本地的工具,并支持定时清理过期备份文件。

安装

pip install aliyun-rds-bkp

配置

配置文件为json格式。

{
    "AccessKeyId": "AccessKeyID Provided by Aliyun RDS",
    "AccessKeySecret": "AccessKeySecret Provided by Aliyun RDS",
    "Regions": [
        {
            "RegionID": "cn-hangzhou",
            "DBInstances": [
                {
                    "DBInstanceId": "rm-XXXXXXXXXXXXXXXXXX",
                    "LastFullBackup": {
                        "BackupEndTime": "2019-03-16 05:30:00"
                    },
                    "LastBinlogBackup": {
                        "BackupEndTime": "2019-03-16 05:30:00"
                    },
                    "BackupRetentionDays": 21,
                    "Schedule": {
                        "FullBackup": {
                            "Plan": "* * * * 2,4,6"
                        },
                        "BinlogBackup": {
                            "Plan": "* * * * *"
                        }
                    }
                }
            ]
        }
    ],
    "BackupHome": "Path/to/Backup/Directory",
    "BackupEncryptionKey": "<your-backup-encryption-key>",
    "FailedDownloads": "Path/to/Failed Downloads/Directory",
    "ErrorLog": "Path/to/Error/Log",
    "MailConfig": {
        "SMTPServer": "Your SMTP Server",
        "SMTPLogin": "Account to Login SMTP Server",
        "SMTPPassword": "Password to login SMTP Server",
        "SMTPPort": 25,
        "TTLS": false,
        "From": "email_from",
        "To": [
            "email_1",
            "email_2"
        ],
        "Cc": [
            "email_cc"
        ],
        "Subject": "E-Mail Subject"
    }
}

参数说明

  • AccessKeyId: 阿里云提供的AccessKeyId
  • AccessKeySecret: 阿里云提供的AccessKeySecret
  • RegionID: 参考https://help.aliyun.com/document_detail/40654.html
  • DBInstanceId: RDS实例ID
  • BackupEndTime: 上次备份的结束的UTC时间,用于增量,格式为YYYY-MM-DD HH:MI:SS
  • BackupRetentionDays: 备份保留天数
  • Plan: 备份计划(本地时间)。5个参数以空格分隔,分别代表触发备份的
    • 分钟
    • 小时
    • 一个月中的第几天
    • 月份
    • 一个星期中的第几天。1表示星期一,7表示星期日。
  • BackupEncryptionKey: 备份加密密钥,必填的非空字符串。请在部署配置中填写实际密钥,勿提交到版本库。已有备份仍需用生成时的密钥解密。
  • BackupHome: 放置备份文件的总目录。在总目录下会自动按照RegionID->InstanceID->Year->Month->Day创建目录
  • ErrorLog: 指定错误日志存放文件
  • SMTPServer: 邮件服务器地址,用于发送备份成功或失败的通知邮件
  • SMTPLogin: 邮件服务器登录账号
  • SMTPPassword: 邮件服务器登录密码
  • SMTPPort: 邮件服务器端口
  • TTLS: 邮件服务器是否使用TTLS,true或false
  • From: 邮件发送账户
  • To: 邮件接收账户列表
  • Cc: 邮件抄送账户列表
  • Subject: 通知邮件的主题

使用

  1. 编写调用脚本
import os

from aliyunrdsbkp.mysql_backup import MySQLBackup

"""
日常备份
"""
if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))
    config_file = os.path.join(
        dir_path,
        'config/settings.json'
    )  # 配置文件路径
    mysql_backup = MySQLBackup(config_file)
    mysql_backup.backup()
import os

from aliyunrdsbkp.retry_downloader import RetryDownloader

"""
下载重试
"""
if __name__ == '__main__':
    dir_path = os.path.dirname(os.path.realpath(__file__))
    config_file = os.path.join(
        dir_path,
        'config/settings.json'
    )  # 配置文件路径
    retry_downloader = RetryDownloader(config_file)
    retry_downloader.run()
  1. 在Linux下的crontab或Windows下的Task Scheduler配置定时执行以上调用脚本

企业微信群机器人通知

可在备份配置的顶层添加以下可选配置。两个任务共用同一群机器人和负责人列表。

"WeComConfig": {
    "Webhook": "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=<机器人密钥>",
    "MentionedList": ["owner_userid"]
}

MentionedList 使用企业微信成员 UserID。全部成功时发送简报,文件失败或任务中断时 @ 列表中的负责人。本轮无下载结果时跳过企微通知。Webhook 含密钥,请仅放在部署配置中。企微投递错误记录在现有日志中。

Metadata

Release files for aliyun-rds-bkp 0.1.31

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aliyun-rds-bkp 0.1.31
File Size Uploaded Provenance Actions
aliyun_rds_bkp-0.1.31.tar.gz 33.0 kB Without provenance Details for aliyun_rds_bkp-0.1.31.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aliyun-rds-bkp 0.1.31
File Interpreter ABI Platform Actions
aliyun_rds_bkp-0.1.31-py3-none-any.whl Python 3 none any Details for aliyun_rds_bkp-0.1.31-py3-none-any.whl

Total release size: 69.9 kB

Release files / aliyun_rds_bkp-0.1.31.tar.gz

Download URL aliyun_rds_bkp-0.1.31.tar.gz
Size 33.0 kB
Tags Source
SHA-256 checksum
How to use checksums
00bfd73b632eee282015857a440d1d401be736a0f3db58426d8301758aeece60
BLAKE2b-256 checksum
How to use checksums
ee6e078a13a10c08fd4589228f4cbf5d8fe56ec7b7a201d3d06976333bd0f0ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / aliyun_rds_bkp-0.1.31-py3-none-any.whl

Download URL aliyun_rds_bkp-0.1.31-py3-none-any.whl
Size 36.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fa1543a63d8585d37a4ba83f973d194c797f0d141b518584bdd061320e47adae
BLAKE2b-256 checksum
How to use checksums
abcb0113315e53706651e7ff45082e989bdc2f4faaaeb8fa8af636a0086ba484
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Security for aliyun-rds-bkp 0.1.31

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.31 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page