xtrabackup周全备+增备Python脚本
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
@version:0.1
@author:gaoquan
用法:
python /usr/local/percona-xtrabackup-2.3.3-Linux-x86_64/bin//backup.py --host=192.168.31.64 --port=3306 --user=bkpuser --password=bkppassword /data/backup/
"""
import os
import time
import datetime
import sys
import commands
import getopt
import shutil
innobackupex = '/usr/local/percona-xtrabackup-2.3.3-Linux-x86_64/bin/innobackupex'
# 全備函數(shù)
def full_backup(host, port, user, password, full_backup_dir):
? ? os.system(innobackupex + ' --host=' + host + ' --port=' + port + ' --user=' +
? ? ? ? ? ? ? user + ' --password=' + password + ' ' + full_backup_dir + ' --no-timestamp >/tmp/backup.log 2>&1')
# 增備函數(shù)
def incr_backup(host, port, user, password, incr_backup_dir, base_backup_dir):
? ? os.system(innobackupex + ' --incremental ' + incr_backup_dir + ' --incremental-basedir=' +
? ? ? ? ? ? ? base_backup_dir + ' --host=' + host + ' --port=' + port + ' ?--user=' + user + ' --password=' + password + ' >/tmp/backup.log ?2>&1')
# 主函數(shù)
if __name__ == '__main__':
? ? config = {
? ? ? ? "host": "",
? ? ? ? "port": "",
? ? ? ? "user": "",
? ? ? ? "password": "",
? ? ? ? 'backup_dir': "",
? ? }
? ? opts, args = getopt.getopt(sys.argv[1:], 'a:P:u:p:b',
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[
? ? ? ? 'host=',
? ? ? ? 'port=',
? ? ? ? 'user=',
? ? ? ? 'password=',
? ? ? ? 'backup_dir'
? ? ])
? ? for option, value in opts:
? ? ? ? if option in ["-a", "--host"]:
? ? ? ? ? ? config["host"] = value
? ? ? ? elif option in ['--port', '-P']:
? ? ? ? ? ? config["port"] = value
? ? ? ? elif option in ['--user', '-u']:
? ? ? ? ? ? config["user"] = value
? ? ? ? elif option in ['--password', '-p']:
? ? ? ? ? ? config["password"] = value
? ? host=config['host']
? ? port=config['port']
? ? user=config['user']
? ? password=config['password']
? ? backup_dir = args[0]
? ? print "備份主目錄是--------" + backup_dir
? ? wday = time.localtime().tm_wday
? ? week_of_dir = backup_dir + time.strftime("%U", time.localtime()) + "/" + port
? ? full_backup_dir = week_of_dir + "/" + "full"
? ? print "全備目錄是--------" + full_backup_dir
? ? # 增備尋找最新的上一次備份的基準(zhǔn)目錄
? ? base_backup_dir = commands.getoutput(
? ? ? ? 'find ' + week_of_dir + ' -mindepth 1 -maxdepth 1 -type d -printf "%P\n" ?| sort -nr | head -1')
? ? # 探測mysql實(shí)例是否存活,如果存活繼續(xù)下面的程序執(zhí)行,如果不存活則直接退出程序
? ? mysql_stat = commands.getoutput(
? ? ? ? '/bin/netstat -anp|grep ' + port + ' |grep -v unix|wc -l')
? ? if mysql_stat >= 1:
? ? ? ? print "mysql實(shí)例存活,可進(jìn)行備份操作!"
? ? else:
? ? ? ? print "mysql實(shí)例不存在,備份操作終止!"
? ? ? ? sys.exit()
? ? # 每周生成一個(gè)周備份目錄,全備和增備目錄都放在此目錄下面
? ? if os.path.exists(week_of_dir):
? ? ? ? print "周備份目錄已經(jīng)生成,可進(jìn)行相應(yīng)的全備或者增量備份"
? ? else:
? ? ? ? print "周備份目錄未產(chǎn)生,創(chuàng)建周備份目錄..."
? ? ? ? os.makedirs(week_of_dir)
? ? # 判斷是否周日,如果是周日,直接進(jìn)行全備,如果不是周日,先檢查全備是否存在,不存在則進(jìn)行全備,存在則進(jìn)行增備
? ? print "備份開始于: ?" + time.strftime( "%Y-%m-%d %H:%M:%S",time.localtime())
? ? if wday == 6:
? ? ? ? full_backup(host, port, user, password, full_backup_dir)
? ? else:
? ? ? ? if os.path.exists(full_backup_dir):
? ? ? ? ? ? incr_backup(host, port, user, password,week_of_dir,base_backup_dir)
? ? ? ? else:
? ? ? ? ? ? full_backup(host, port, user, password, full_backup_dir)
? ? print "備份結(jié)束,判斷備份是否成功"
? ? print "備份結(jié)束于: ?" + time.strftime( "%Y-%m-%d %H:%M:%S",time.localtime())
? ? try:
? ? ? ? with open("/tmp/backup.log") as f:
? ? ? ? ? ? f.seek(-14, 2)
? ? ? ? ? ? backup_results = f.readline().strip()
? ? ? ? ? ? if backup_results == "completed OK!":
? ? ? ? ? ? ? ? print "備份成功"
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print "備份失敗"
? ? ? ? ? ? ? ? shutil.rmtree(base_backup_dir)
? ? except Error:
? ? ? ? sys.exit()
轉(zhuǎn)載于:https://blog.51cto.com/gaoquan/1749931
總結(jié)
以上是生活随笔為你收集整理的xtrabackup周全备+增备Python脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java——递归调用
- 下一篇: python【5】-生成式,生成器