• MySQL按天备份二进制日志


    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:guozhen.zhang
     
     
    import MySQLdb
    import time
    import os
     
    # 创建备份binlog目录
    project_path = '/data/binlog_back'  # 定义备份日志的目录
    binlog_file = "/data/binlog_back/binlog_file"  # 定义获取日志的存放文件
    last_binlog_file = "/data/binlog_back/last_binlog_file"  # 获取binlog日志的最后一个日志文件
    dir_time = time.strftime('%Y%m%d-%H%M', time.localtime(time.time()))  # 返回当前时间的年月日作为目录名称
    isExists = os.path.exists(project_path + '/' + dir_time)  # 判断该目录是否存在
    if not isExists:
        os.makedirs(project_path + '/' + dir_time)
        print(project_path + '/' + dir_time + "目录创建成功")

    # 定义执行备份脚本
    def back_binlog():
        # 建立MySQL连接
        conn = MySQLdb.connect(host='192.168.1.20', port=3306, user='root', passwd='123a456b')
        # 刷新master的二进制日志
        cursor = conn.cursor()
        cursor.execute("flush logs;")
        # 获取binlog的存放路径
        cursor1 = conn.cursor()
        cursor1.execute("show variables like  'log_bin_basename'")
        row1 = cursor1.fetchone()[1]
        # 获取master  binlog日志的最后一个日志文件
        cmd = 'ls %s* |grep -v index|tail -1 > %s' % (row1, last_binlog_file)
        os.popen(cmd).read()
        # 获取master前一天的二进制日志
        cmd = 'find %s*  -mtime 0    -exec ls {} ;|grep -v  `cat %s` |grep -v index> %s' % (
            row1, last_binlog_file, binlog_file)
        os.popen(cmd)
        f = open(binlog_file, mode="r")
        lines = f.readlines()
        for line in lines:
            fname = line.strip()
            cmd = 'cp ' + fname + ' ' + (project_path + '/' + dir_time)
            os.system(cmd)
        f.close()
        # 关闭数据库连接
        conn.close()

    # 备份二进制文件存在就执行备份,否则退出
    if os.path.exists(binlog_file):
        back_binlog()
        print("backup success!")
    else:
        print("binlog file not found")
        exit()
  • 相关阅读:
    【转】进程间通信方式总结(windows 和linux)
    Python-Analysis-Malware
    现场取证之流量分析总结
    木马通信与防护墙穿透
    【PE结构】恶意代码数字签名验证
    32位与64位架构上的区别
    【API】网络编程模型、多线程
    【API】遍历进程的几种方式
    【病毒取样】取证分析之逆向服务器提权开启3389远程连接工具
    【CTF WEB】XSS-https://alf.nu/alert1
  • 原文地址:https://www.cnblogs.com/manger/p/13531107.html
Copyright © 2020-2023  润新知