资源简介
一键 使用 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读取Las与转换为TXT.zip
- BSTestRunner.pypython3
- SI模型,影响力传播模型,传染病模型
- python自动抓取网页中的pdf文件
- python爬虫网站图片
- Anaconda3 for MacOSX x64百度云
- python16to8
- freetype的python代码
- selenium+python 自动化测试 ---登陆界面测
- OpenCV-Python实现的图片拼接源代码
- 多商品流点弧模型 python+gurobi
- dbfpy操作dbf文件
- python实现果蝇优化算法,测试函数为
- 通过python实现批量excel转pdf代码
- python实现BM匹配算法
- 新浪财经24小时python爬虫程序内置企业
- 基于python实现的ICA过程
- LSTM股价预测(python).zip
- 支持向量机SVM python源代码
- txt.py python 对比不同的txt文档数据 不
- python_获取百度搜索结果(标题和URL)
- 桌面清理工具
- 第十届蓝桥杯大赛青少年创意编程P
- 正规式转NFA转DFA转MFA
- pygame动态图 & 以及动态图片的移动
- 粒子群算法 Python源程序
- python最小二乘法代码
- python基于winpcap的抓包和发包
- python3.dll——python3.5.2
- python 爬取图片
评论
共有 条评论