当前位置:Gxlcms > 数据库问题 > mysql增备

mysql增备

时间:2021-07-01 10:21:17 帮助过:10人阅读

!/usr/local/bin/python3 # -*- coding: UTF-8 -*- # ==================================================== # Author: changbo - 541330702@qq.com # Last modified: 2017-9-3 # Filename: mysqlincrement.py # Description: backup mysql files,base percona xtrabackup # http://www.cnblogs.com/changbo # ==================================================== import stomp import time import threading nowtime = time.strftime("%H%M", time.localtime()) def product(timestamp): conn = stomp.Connection10([(xxxxx, xxxx)]) conn.start() conn.connect() # 如果时间等于0030则传送一个‘bakold’,否则传‘startjob’ if timestamp == 0030: conn.send(SampleQueue, bakold) else: conn.send(SampleQueue, startjob) conn.disconnect() if __name__ == __main__: count = 0 while True: if nowtime == 0030: t = threading.Thread(target=product, args=(nowtime,)) t.start() count -= 5 elif count == 5: t = threading.Thread(target=product, args=(nowtime,)) t.start() count -= 5 else: count += 1 time.sleep(60)

 

4,consumer

#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-

# ====================================================
# Author: changbo - 541330702@qq.com
# Last modified: 2017-9-3
# Filename: mysqlincrement.py
# Description: backup mysql files,base percona xtrabackup
# http://www.cnblogs.com/changbo
# ====================================================

import stomp
import mysqlsingle
import threading
import os
import time

nowday = time.strftime("%Y%m%d", time.localtime())
nowtime = time.strftime("%H%M", time.localtime())
basedir = 
mysqlbakdir =  class SampleListener(object):
    def on_message(self, headers, message):
        try:
            if message == startjob:
                if not os.path.exists(mysqlbakdir):
                    mysqlsingle.createDir()
                else:
                    mysqlsingle.incremBak()
            elif message == bakold:
                mysqlsingle.mvBak()
            else:
                pass
        except Exception as e:
            pass


def resiveMessage():
    conn = stomp.Connection10([(xxxxx, xxxxx)])
    conn.set_listener(SampleListener, SampleListener())
    conn.start()
    conn.connect()
    conn.subscribe(SampleQueue)
    conn.disconnect()


if __name__ == __main__:
    while True:
        t = threading.Thread(target=resiveMessage)
        t.start()

 

5,mysql增备脚本

#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-

# ====================================================
# Author: changbo - 541330702@qq.com
# Last modified: 2017-9-3
# Filename: mysqlincrement.py
# Description: backup mysql files,base percona xtrabackup
# http://www.cnblogs.com/changbo
# ====================================================

import time
import datetime
import os
import subprocess
import logging

logging.basicConfig(level=logging.DEBUG,
                    format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s,
                    datefmt=%a, %d %b %Y %H:%M:%S,
                    filename=backup.log,
                    filemode=a)

nowday = time.strftime("%Y%m%d", time.localtime())
nowtime = time.strftime("%H%M", time.localtime())
basedir = 
mysqlbakdir =  
perfectdir =  
incremdir =  
childdir = # 获取昨日备份文件目录
def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = str((today - oneday)).replace(-, ‘‘)
    return yesterday


def createDir():
    # 创建备份根目录
    if not os.path.exists(basedir):
        os.mkdir(basedir)

    # 创建日备份目录
    if not os.path.exists(mysqlbakdir):
        os.mkdir(mysqlbakdir)

    # 创建全备目录并执行全备
    if not os.path.exists(perfectdir):
        os.mkdir(perfectdir)
        command1 = innobackupex --defaults-file=xxxxx --no-timestamp %s % perfectdir
        os.system(command1)

    # 创建增备目录
    if not os.path.exists(incremdir):
        os.mkdir(incremdir)

    # 创建分备份目录并增备
    if not os.path.exists(childdir):
        os.mkdir(childdir)
        command2 = innobackupex --defaults-file=xxxxx --no-timestamp --incremental-basedir=%s --incremental %s % (
        perfectdir, childdir)
        os.system(command2)


def incremBak():
    # 获取最后被创建的文件夹目录名
    filename = (subprocess.Popen("ls " + incremdir + "| head -2 | tail -1 | awk ‘{print $9}‘", shell=True,
                                 stdout=subprocess.PIPE).stdout.read()).decode()
    # 创建增备目录
    os.mkdir(childdir)
    command3 = innobackupex --defaults-file=xxxxx --no-timestamp --incremental-basedir=%s/%s --incremental %s % (
        incremdir, filename, childdir)
    os.system(command3)


def mvBak():
    filetime = getYesterday()
    # 压缩昨天备份目录
    command4 = "cd %s && tar czf hkmysqlbak%s.tar.gz %s" % (basedir, filetime, filetime)
    logging.debug(os.system(command4))
    # 移除昨天备份目录
    command5 = "cd %s && rm -rf %s" % (basedir, filetime)
    logging.debug(os.system(command5))


if __name__ == __main__:
    if not os.path.exists(mysqlbakdir):
        createDir()
    incremBak()

 

 python新手,欢迎吐槽

END!

 

mysql增备

标签:roc   popen   sql   ctime   def   rm -rf   imp   path   product   

人气教程排行