#!/usr/bin/python # -*- coding: utf-8 -*- import sys import os import time import tarfile import shutil # 获取备份文件列表,type=1返回目录,2返回路径列表 # 备份完成后的文件夹列表['2018-01-11_20-52-40', '2018-01-11_20-55-58', '2018-01-11_20-56-48'] def dir_List(type): if type == 1: dataDir = os.listdir(backup_path) # 路径所有目录及文件列表 dirList = [] for f in dataDir: if (os.path.isdir(backup_path + f)): dirList.append(f) print("------dirList:", sorted(dirList)) print("------dirListLen:", str(len(dirList))) return sorted(dirList) elif type == 2: dataDir = os.listdir(backup_path) return sorted(dataDir) # 压缩文件 def compress(dstname, filePath): tarHandle = tarfile.open(dstname, "w:gz") for dirpath, dirs, files in os.walk(filePath): for filename in files: tarHandle.add(os.path.join(dirpath, filename)) # print filename + " tar succeeded" tarHandle.close() return tarHandle # 全量备份 def full_Backup(): print("------全量备份-----------") # ---------- v2 ------------------- today2 = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) # 年月日[2018-01-10] dstname2 = today2 + '-full-bak.bz2' backupLog = backup_path + today2 + '/backup.' + today2 + '.log' backup_file = backup_path + dstname2 lsndir = backup_path + today2 + '/' rm = 'rm -rf ' + backup_path + '*.log' os.system(rm) os.mkdir(backup_path + today2) cmd2 = 'innobackupex --defaults-file=' + conf + ' -u' + mysql_user + ' -p' + mysql_password + ' --stream=xbstream --extra-lsndir="' + lsndir + '" ' + backup_path + ' 2>>"' + backupLog + '" | lbzip2 -kv -n 10 > "' + backup_file + '" 2>>"' + backupLog + '"' print("------cmd2:", cmd2) os.system(cmd2) dirList = dir_List(2) if len(dirList) >= 14: for i in range(len(dirList)): if i == (len(dirList) - 14): print("this num:", i) break print("i:", i) print(dirList[i]) if os.path.isdir(backup_path + dirList[i]): shutil.rmtree(backup_path + dirList[i]) # shutil.rmtree("/opt/backup/2018-01-28_14-18-33") else: os.remove(backup_path + dirList[i]) pass pass pass # ---------- end ------------------- # 增量备份 def incre_Backup(): print("------增量备份-----------") # ------------ v2 ------------------ today2 = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) # 年月日[2018-01-10] dstname2 = today2 + '-incre-bak.bz2' backupLog = backup_path + today2 + '/backup.' + today2 + '.log' backup_file = backup_path + dstname2 lsndir = backup_path + today2 + '/' print("------lsndir:", lsndir) dirList = dir_List(1) baseDir = backup_path + dirList[-1] os.mkdir(backup_path + today2) cmd2 = 'innobackupex --defaults-file=' + conf + ' -u' + mysql_user + ' -p' + mysql_password + ' --stream=xbstream --extra-lsndir="' + lsndir + '" --incremental --incremental-basedir="' + baseDir + '" ' + backup_path + ' 2>>"' + backupLog + '" | lbzip2 -kv -n 10 > "' + backup_file + '" 2>>"' + backupLog + '"' print("------cmd2:", cmd2) os.system(cmd2) # ------------ end ------------------ def new_report(test_report): lists = os.listdir(test_report) # 列出目录的下所有文件和文件夹保存到lists print(list) lists.sort(key=lambda fn: os.path.getmtime(test_report + "\\" + fn)) # 按时间排序 file_new = os.path.join(test_report, lists[-1]) # 获取最新的文件保存到file_new print(file_new) return file_new ''' 1 备份步骤 由于xbstream+压缩备份后,无备份目录,xtrabackup可指定--extra-lsndir目录,此目录只存放此次备份的xtrabackup_checkpoints文件;后面的增量备份时,--incremental-basedir就指向前一日的extra-lsndir目录便可。 1) 全量备份命令: innobackupex --user=dump --password=xxx --host=127.0.0.1 --slave-info --stream=xbstream --extra-lsndir="$lsndir" $baseDir 2>>"$backupLog" | lbzip2 -kv -n 10 > "$backup_file" 2>>"$backupLog" 2) 增量备份命令: innobackupex --user=dump --password=xxx --host=127.0.0.1 --slave-info --stream=xbstream --extra-lsndir="$lsndir" --incremental --incremental-basedir="$last_lsndir" $baseDir 2>>"$backupLog" | lbzip2 -kv -n 10 > "$backup_file" 2>>"$backupLog" 2、 还原步骤 备份后生成的为压缩了的文件,所以要解压,再用xbstream打开流体文件(较之原来还原,多了此步骤)。 1) 解压压缩的文件:lbzip2 -dkv -n 10 2013-07-14_bak_full.bz2 2) 解压液体文件为目录: xbstream -x < 2013-07-14_bak_full -C /work/bak/2013-07-14_full/ ''' conf = '/etc/my.cnf' backup_path = '/data/backup/db/op/' mysql_user = 'root' mysql_password = 'root' if __name__ == "__main__": # test_report = "." # 目录地址 # new_report(test_report) # test_oss("./2018-01-07_15-59-28-klzz-full-bak.bz2") # full_Backup() # 初始化第一次备份,否则后面的无法执行 # a = dir_List(1) # if a : # print "*********第一次执行此脚本,将执行下面的语句进行初始化**************" #os.system('yum install lbzip2 -y') #os.system('mkdir -p /opt/backup/2018-01-07_15-59-28') #os.system('innobackupex --defaults-file=/etc/my.cnf -uroot -p1234 --stream=xbstream --extra-lsndir="/opt/backup/2018-01-07_15-59-28/" /opt/backup/ 2>>"/opt/backup/2018-01-07_15-59-28/backup.2018-01-07_15-59-28.log" | lbzip2 -kv -n 10 > "/opt/backup/2018-01-07_15-59-28-klzz-full-bak.bz2" 2>>"/opt/backup/2018-01-07_15-59-28/backup.2018-01-07_15-59-28.log"') # -------- end ----------------- # print " " # pass wday = time.strftime("%w", time.localtime()) # 获取星期几[0--6,0代表星期日] day = time.strftime("%d", time.localtime()) # 当前每月几号[1--31] print(wday + ":wday---day:" + day) full_Backup() #if day == 1: # 每月1号和没周日全量备份 # full_Backup() # pass #else: # 每周一到周六增量备份 # incre_Backup() # pass pass