资源简介
一键 使用 python mysql xtrabackup备份脚本,实测可用!,可以定制添加更多功能
代码片段和文件信息
#!/usr/bin/env python
# encoding: utf-8
# 支持mysql5.7 # wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.4-1.el7.x86_64.rpm
import datetime
import os
import pymysql
import subprocess
import re
import sys
import time
import logging.handlers
innobackupex = ‘/usr/bin/innobackupex‘
mysql_user = ‘root‘
mysql_password = ‘123‘
defaults_file = ‘/etc/my.cnf‘
mysql_host = ‘127.0.0.1‘
mysql_port = 3306
fullback_dir = ‘/backup/full‘
increback_dir = ‘/backup/incre‘
log_text = ‘/backup/backup.‘ + datetime.datetime.now().strftime(‘%Y%m%d‘) + ‘.txt‘
if not os.path.exists(fullback_dir):
os.makedirs(fullback_dir)
if not os.path.exists(increback_dir):
os.makedirs(increback_dir)
class Logging():
__flag = None
def __new__(cls *args **kwargs):
if not cls.__flag:
cls.__flag = super().__new__(cls)
return cls.__flag
def __init__(selflogname):
logger = logging.getLogger()
logger.setLevel(level=logging.INFO)
handler = logging.handlers.RotatingFileHandler(lognamemode=“a“maxBytes=1024*1024*2backupCount=1encoding=“utf-8“)
formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(console)
self.logger = logger
def Write_error_log(selfmsg):
logging.error(str(msg)exc_info = True)
def Write_info_log(selfmsg):
logging.info(str(msg))
def Write_warn_log(selfmsg):
logging.warning(str(msg))
# 环境检查:
def check_env():
log_obj.Write_info_log(‘日志文件为%s:‘%log_text)
msg=““
chk_1 = not os.path.exists(innobackupex)
chk_2 = not os.path.exists(fullback_dir)
chk_3 = not os.path.exists(increback_dir)
if chk_1:
msg=innobackupex + “文件不存在“
log_obj.Write_info_log(get_now_time() + ‘:‘ + innobackupex + ‘文件不存在‘)
if chk_2:
msg=fullback_dir + “全量备份目录不存在“
log_obj.Write_info_log(get_now_time() + ‘:‘ + fullback_dir + ‘全量备份目录不存在‘)
if chk_3:
msg = increback_dir +“增量备份目录不存在“
log_obj.Write_info_log(get_now_time() + ‘:‘ + increback_dir + ‘增量备份目录不存在‘)
conn = None
chk_4 = False
try:
conn = pymysql.connect(
host=mysql_host
port=mysql_port
user=mysql_user
passwd=mysql_password
charset=“utf8“
)
except Exception as err:
msg=“mysql数据库连接错误“
log_obj.Write_info_log(get_now_time() + ‘:‘ + str(err))
chk_4 = True
if (chk_1 or chk_2 or chk_3 or chk_4):
# 检查命令,目录是否存在,检查Mysql是否可以连接如果否终止备份程序
log_obj.Write_info_log(get_now_time() + ‘:‘ + msg)
log_obj.Write_inf
相关资源
- Python-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
评论
共有 条评论