#!/bin/bash #mysql热备脚本 #备份策略:每周一进行完整备份,以后每一天在前一天的基础上进行增量备份 # #备份目录 back_dir="/data/backup/mysql" #备份数据库信息 host="" dbuser="" dbpasswd="" #日志文件 back_log="${back_dir}/log/mysql_back.log" #week day week_day=`date +%w` #yesterday yesterday=`date +%F -d "1 days ago"` #日志记录函数 function log(){ echo "`date` $1" | tee -a ${back_log} } #判断当前日期,是周一进行全备份,其他时间进行增量备份 if [ ${week_day} -eq 1 ]; then #完整备份 log "周${week_day}开始完全备份..." innobackupex --defaults-file=/etc/my.cnf --host=${host} --user=${dbuser} --password=${dbpasswd} ${back_dir} &>> ${back_log} if [ $? -eq 0 ]; then log "完全备份完毕." else log "完全备份出错,请检查." exit 1 fi else let dir_num=`find ${back_dir} -type d -name "${yesterday}*" | wc -l` if [ ${dir_num} -ne 1 ]; then log "昨天增量目录未找到或昨天备份目录大于等于2个,请确认后再次备份." exit 1 fi incremental_dir=`find ${back_dir} -type d -name "${yesterday}*"` log "周${week_day}开始增量备份..." #增量备份 innobackupex --host=${host} --user=${dbuser} --password=${dbpasswd} --incremental ${back_dir} --incremental-basedir=${incremental_dir} &>> ${back_log} if [ $? -eq 0 ]; then log "增量备份成功." else log "备份失败,请检查日志..." exit 1 fi fi exit $?