百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

干货!python实现mysql自动化备份、清理、通知

toyiye 2024-06-21 12:32 13 浏览 0 评论

前言

本文是阿里云RDS迁移后拓展,解决自动备份并通过邮件通知备份结果。

关于阿里云RDS迁移详见

阿里云RDS迁移,极简安装 MySQL TokuDB 引擎

阿里云RDS迁移,innobackupex 大数据迁移神器

放一张代码运行后的效果图


前置

  • 安装 python3.6
  • 安装 Percona XtraBackup (备份工具)

依赖模块

pip3 install jinja2 MarkupSafe

使用说明

1. 编辑配置文件 vim xb_back.cnf

2. 运行 xb_back.py 脚本

python3.6 ./xb_back.py -f ./xb_back.cnf

代码 xb_back.py

# -*- coding:utf-8 -*-
# edit by hoke

import argparse
import configparser
import logging
import shutil
import smtplib
import socket
import subprocess
import sys
import os
import jinja2
import time
import datetime
from email.mime.text import MIMEText
from os.path import isfile
from tempfile import TemporaryFile

# Set logger
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT)
logger = logging.getLogger(__name__)

# Get HOSTNAME
HOSTNAME = socket.gethostname()

# Get os time
CURRENT_TIME = time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime())

# Set statistics info
STATISTICS_INFO = {'备份主机': HOSTNAME}

# Set render html template
module_path = os.path.dirname(__file__)
html_template = os.path.join(module_path + '/table_template.html')


def render_to_template(html_path, html_context):
    path, filename = os.path.split(html_path)
    env = jinja2.Environment(
        loader=jinja2.FileSystemLoader(path)
    )
    if isinstance(html_context, dict):
        return env.get_template(filename).render({'data': html_context})
    else:
        return env.get_template(filename).render({'err_msg': html_context})


def get_arguments():
    """
    Get user input 
    """
    parser = argparse.ArgumentParser(description='This a backup help document.')
    parser.add_argument('-f', '--file', type=str, help='xtrabackup read config file')
    args = parser.parse_args()
    return args.file


def check_config_valid(file):
    """
    Check config file content valid
    """
    config = configparser.ConfigParser()
    try:
        config.read(file)
    except configparser.MissingSectionHeaderError:
        logger.warning('WARN: this first line must contains section headers')

    config_items = [x for x in config.keys()]

    config_keys = []
    for i in [config.items(x) for x in config.keys()]:
        for j in i:
            config_keys.append(j[0])

    keep_items = ['mysql', 'xtrabackup', 'compress', 'mail', 'expired']
    keep_keys = [
        'user',
        'host',
        'password',
        'port',
        'backup_tool',
        'defaults-file',
        'backupdir',
        'title',
        'mail_sender',
        'mail_receiver',
        'mail_host',
        'mail_port',
        'mail_user',
        'mail_pass',
        'expire_day'
    ]

    for header in keep_items:
        if header not in config_items:
            logger.error(f'FAILED: this section header [{header}] not found.')
            sys.exit(1)

    for key in keep_keys:
        if key not in config_keys:
            logger.error(f'FAILED: this argument [{key}] not found.')
            sys.exit(1)

    return True


class General(object):
    """
    Process the config file and Generate variables
    """

    def __init__(self, file):
        if isfile(file):
            config = configparser.ConfigParser(allow_no_value=True)
            config.read(file)

            Mysql = config['mysql']
            self.user = Mysql['user']
            self.host = Mysql['host']
            self.password = Mysql['password']
            self.port = Mysql['port']

            Xtrabackup = config['xtrabackup']
            self.backup_tool = Xtrabackup['backup_tool']
            self.defaults_file = Xtrabackup['defaults-file']
            self.backupdir = Xtrabackup['backupdir']
            self.fmt_backupdir = '/'.join((Xtrabackup['backupdir'], CURRENT_TIME))
            if 'xtra_options' in Xtrabackup:
                self.xtra_options = Xtrabackup['xtra_options']

            Compress = config['compress']
            if 'compress' in Compress:
                self.compress = Compress['compress']
            if 'compress_chunk_size' in Compress:
                self.compress_chunk_size = Compress['compress_chunk_size']
            if 'compress_threads' in Compress:
                self.compress_threads = Compress['compress_threads']

            Mail = config['mail']
            self.title = Mail['title']
            self.mail_sender = Mail['mail_sender']
            self.mail_receiver = Mail['mail_receiver']
            self.mail_host = Mail['mail_host']
            self.mail_port = Mail['mail_port']
            self.mail_user = Mail['mail_user']
            self.mail_pass = Mail['mail_pass']

            Expired = config['expired']
            self.expire_day = Expired['expire_day']

class ToolsUtils(General):
    """
    Define some tools
    """

    def __init__(self, file):
        self.file = file
        General.__init__(self, self.file)

        self.xb_output_log = '/'.join((self.fmt_backupdir, 'xb_output.log'))

        STATISTICS_INFO['备份目录'] = self.fmt_backupdir
        STATISTICS_INFO['备份工具'] = self.backup_tool

    def create_backup_dir(self):
        """ create backup directory """
        if not os.path.exists(self.fmt_backupdir):
            os.makedirs(self.fmt_backupdir)
            logger.info(f'OK: the backup dir not exisit, create {self.fmt_backupdir}')

    FMT_INFO = '{:.2f}GB'

    def get_backup_file_size(self):
        size = 0
        for root, dirs, files in os.walk(self.fmt_backupdir):
            size += sum([os.path.getsize(os.path.join(root, name))
                         for name in files])
        logger.info(f'OK: get backup file size')
        STATISTICS_INFO['备份大小'] = self.FMT_INFO.format(float(size / 1024 / 1024 / 1024))
        return True

    def get_partition_size(self):
        vfs = os.statvfs(self.fmt_backupdir)
        free = (vfs.f_bavail * vfs.f_bsize) / (1024 * 1024 * 1024)
        total = (vfs.f_blocks * vfs.f_bsize) / (1024 * 1024 * 1024)
        partition_free_size = self.FMT_INFO.format(free)
        partition_total_size = self.FMT_INFO.format(total)
        logger.info(f'OK: get disk partition usage')
        STATISTICS_INFO['可用空间'] = partition_free_size
        STATISTICS_INFO['总空间'] = partition_total_size
        return True

    def send_mail(self, data):
        """ 
        Send mail notice 
        Read TemporaryFile content
        """
        msg = MIMEText(data, _subtype='html', _charset='utf-8')
        msg['Subject'] = '{} from {}'.format(self.title, HOSTNAME)
        msg['From'] = self.mail_sender
        msg['To'] = ";".join(list(self.mail_receiver.split(',')))
        mail_receiver = list(self.mail_receiver.split(','))

        try:
            server = smtplib.SMTP()
            server.connect(self.mail_host, self.mail_port)
            # server.ehlo()
            # enable tls encrypt
            server.starttls()
            server.set_debuglevel(1)
            server.login(self.mail_user, self.mail_pass)
            server.sendmail(self.mail_sender, mail_receiver, msg.as_string())
            server.close()
            logger.info(f'OK: send mail success')
        except Exception as err:
            logger.error(f'FAILED: send mail fail')
            logger.error(err)

    def remove_expired_directory(self):
        #当前时间
        today = datetime.datetime.now()
        today_init = int(today.strftime('%Y%m%d'))
        print("today_init:", today_init)
        # #n天前时间
        # n_days = datetime.timedelta(days=int(expire_time))
        # n_days_agos = today - n_days
        # n_days_agos_init = int(n_days_agos.strftime('%Y%m%d'))
        remove_dir = []
        print("backupdir", self.backupdir)
        list = os.listdir(self.backupdir)
        print("list:", list)
        for directory_name in list:
            abs_dir = os.path.join(self.backupdir, directory_name)
            print("abs_dir:", abs_dir)
            dir_timestamp = os.path.getctime(abs_dir)
            dir_init = int(datetime.datetime.fromtimestamp(dir_timestamp).strftime('%Y%m%d'))
            print("dir_init:", dir_init)
            print("expire_day:", int(self.expire_day))
            print(today_init - dir_init)
            if today_init - dir_init >= int(self.expire_day):
                print("remove:", os.path.join(self.backupdir, directory_name))
                shutil.rmtree(os.path.join(self.backupdir, directory_name))
                logger.info(f"OK: the directory {directory_name} remove success")
                remove_dir.append(directory_name)
                
            STATISTICS_INFO['过期备份'] = remove_dir

class Prepare(General):
    """
    Generate command
    """

    def __init__(self, file):
        self.file = file
        General.__init__(self, self.file)

    @property
    def generate_xb_cmd(self):
        mysql_cmd = f"--user={self.user} --password={self.password} --host={self.host} --port={self.port}"

        cmd_list = []
        if hasattr(self, 'compress') and hasattr(self, 'compress_chunk_size') and hasattr(self, 'compress_threads'):
            compress_cmd = f"--compress={self.compress} --compress-chunk-size={self.compress_chunk_size} --compress-threads={self.compress_threads}"
            cmd_list.append(compress_cmd)
            STATISTICS_INFO['是否压缩'] = 'Yes'
        else:
            STATISTICS_INFO['是否压缩'] = 'No'

        if hasattr(self, 'xtra_options'):
            xtra_options = self.xtra_options
            cmd_list.append(xtra_options)

        xb_cmd = f"{self.backup_tool} --defaults-file={self.defaults_file}" 

        return ' '.join((xb_cmd, mysql_cmd, ' '.join(cmd_list), self.fmt_backupdir)) 

class RunCommand(object):
    def __init__(self, command):
        self.command = command

    @property
    def runner(self):
        status, output = subprocess.getstatusoutput(self.command)
        return {'status': status, 'output': output}


def main():
    start_time = time.time()

    config_file = get_arguments()

    # check whether the config file is valid
    check_config_valid(config_file)

    # instance Prepare and Toolskit
    prepare = Prepare(config_file)
    tools = ToolsUtils(config_file)

    xb_cmd = prepare.generate_xb_cmd
    logger.info(f'OK: generate xtrabackup command \n {xb_cmd}')

    # exec backup
    backup_result = RunCommand(xb_cmd).runner
    logger.info(f'OK: perform backup process, please waiting.')

    with TemporaryFile('w+t', encoding='gbk') as f:
        if backup_result['status'] == 0:
            logger.info(f'OK: xtrabackup backup success')
            tools.get_backup_file_size()
            tools.get_partition_size()
            tools.remove_expired_directory()

            end_time = time.time()
            STATISTICS_INFO['备份耗时'] = '{:0.2f}s'.format(end_time - start_time)

            result = render_to_template(html_template, STATISTICS_INFO)
            f.write(result)
        else:
            logger.error(f"ERROR: {backup_result['output']}")
            f.write(backup_result['output'])

        f.seek(0)
        TEXT_DATA = f.read()

        tools.send_mail(TEXT_DATA)

if __name__ == '__main__':
    main()

配置文件 xb_back.cnf

[mysql]
# mysql backup user
user = root
password = 
host = 127.0.0.1
port = 3306

[xtrabackup]
backup_tool = /usr/bin/innobackupex
defaults-file = /etc/my.cnf
backupdir = /home/hoke/zabbixDB-backup/AutoBackup
xtra_options = --no-version-check --rsync

[compress]
# Optional
# Enable only if you want to use compression.
compress = quicklz
compress_chunk_size = 64k
compress_threads = 4

[mail]
# mail setting
title = [ZabbixDB AutoBackup]
mail_sender = rd_sys@xxx.xxx
mail_receiver = runchain_ops@xxx.xxx,hoke58@qq.com
mail_host = smtp.263.net
mail_port = 25
mail_user = rd_sys@xxx.xxx
mail_pass = password

[expired]
# Enable only if you want to clean expired backup.
expire_day = 5

邮件模板 table_template.html

<html lang="en">
<head>
    <meta charset="gbk">
    <style>
        body {
            font-size: 14px;
            line-height: 1.42857143;
            color: #333;
            background-color: #fff;
        }
        .row {
            margin-right: -15px;
            margin-left: -15px;
        }
        .col-sm-10 {
            width: 85%;
        }
        .col-sm-2 {
            width: 15%;
        }
        .table {
            width: 85%;
            max-width: 85%;
            margin-bottom: 1rem;
            background-color: transparent;
            border-collapse: collapse;
        }
        .table td, .table th {
            padding: .65rem;
            vertical-align: top;
            border-top: 1px solid #dee2e6;
            word-wrap: break-word;
            word-break: break-all;
        }
    </style>
</head>

<body>
<div>
    {% if data %}
    <table class="table">
        <tbody>
        {% for key in data %}
        <tr class="row">
            <td class="col-sm-2">{{ key }}</td>
            <td class="col-sm-10">{{ data[key] }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    {% elif err_msg %}
        <p>err_msg</p>
    {% endif %}
</div>
</body>
</html>

调试好后别忘了crontab 定期执行


相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码