Shell-xtrabackup script

来源:互联网 发布:直筒和滚筒洗衣机 知乎 编辑:程序博客网 时间:2024/06/05 14:11
#!/bin/bash################################################################################### purpose:xtrabackup script # version:v1.5# date :2012-08-22# author :kin zhang##################################################################################PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHbackupdate=`date +%Y%m%d`scheduledate=`date +%a`fullbackup_date=(Wed Sun)incrementbackup_date=(Mon Tue Thu Sat Fri)backup_base=/db/backupfullbackup_path=fullbackup_${backupdate}incrementbackup_path=increment_${backupdate}fulllog=${backup_base}/log/full_${backupdate}.logincrementlog=${backup_base}/log/incr_${backupdate}.logexpired_day=7# check the log directory is exsitedfunction log_dir(){#solution 1:if [ -d ${backup_base}/log ]; thenecho > /dev/nullelsemkdir -p /db/backup/logfi#solution 2:# test -d ${backup_base}/log && echo > /dev/null || mkdir -p /db/backup/log}function mysql_fullbackup(){log_direcho " mysql fullbackup begin at `date`" >>${fulllog}echo "">>${fulllog}# fullbackup innobackupex --slave-info --no-timestamp ${backup_base}/${fullbackup_path} 2>> ${fulllog}echo "">>${fulllog}echo " mysql fullbackup end at `date`" >>${fulllog}cp /etc/my.cnf ${backup_base}/${fullbackup_path}/my.cnf.${backupdate}grep "to_lsn" ${backup_base}/${fullbackup_path}/xtrabackup_checkpoints|awk '{print $3}' > /db/backup/lsn.log# compress the backup filestar zcvf ${backup_base}/${fullbackup_path}.tar.gz ${backup_base}/${fullbackup_path} &> /dev/nullgenerate_report}function mysql_incrementbackup(){log_direcho " incrementbackup begin at `date`" >>${incrementlog}echo "">>${incrementlog}#incremental backupinnobackupex --no-timestamp --incremental-lsn=`cat ${backup_base}/lsn.log` \--incremental ${backup_base}/${incrementbackup_path} 2>>${incrementlog}################################################################################### increment backup option:#   --incremental-basedir=/fullbackup directory #   --incremental-lsn=to_lsn get to_lsn from xtrabackup_checkpoints file##################################################################################echo "">>${incrementlog}echo " incrementbackup end at `date`" >>${incrementlog}# compress the backup filestar zcvf ${backup_base}/${incrementbackup_path}.tar.gz ${backup_base}/${incrementbackup_path} &> /dev/nullgenerate_report}################################################################################### determine which backup type to be executed##################################################################################function schedule_job(){# solution 1:echo "${fullbackup_date[@]}"|grep -wq "${scheduledate}"\&& mysql_fullbackup || mysql_incrementbackup## solution 2: TBD##for (( i=0;i<=${#fullbackup_date[@]};i++ ))##do##        if [[ ${scheduledate} = ${fullbackup_date[i]} ]];then##                mysql_fullbackup##        for (( i=0;i<=${incrementbackup_date[@]};i++ ))##                do##                if [[ ${scheduledate} = ${incrementbackup_date[i]} ]];then##                        mysql_incrementbackup##                fi##                done##        fi ##done}function generate_report(){# create the report dirtest -d ${backup_base}/report && echo > /dev/null \|| mkdir -p ${backup_base}/report#generate report#determine the day whether in fullbackup loopecho "${fullbackup_date[@]}"|grep -wq "${scheduledate}"if [ $? -eq 0 ]; thentest -e ${backup_base}/fullbackup_${backupdate} && bak_size=`du -h ${backup_base}/fullbackup_${backupdate} |tail -1|awk '{print $1}'` ||\ bak_size="No"bak_status=`cat ${fulllog} |grep completed|grep -v 'prints'|awk '{print $4}'`         if [ ! -n "${bak_status}" ] ;then                 echo "${backupdate} ${bak_size} full Failed"\                 >>${backup_base}/report/his.log         else                     echo "${backupdate} ${bak_size} full ${bak_status}"\                  >>${backup_base}/report/his.log         fielse test -e ${backup_base}/increment_${backupdate} && bak_size=`du -h ${backup_base}/increment_${backupdate} |tail -1|awk '{print $1}'` ||\bak_size="No"bak_status=`cat ${incrementlog} |grep completed|grep -v 'prints'|awk '{print $4}'`if [ ! -n "${bak_status}" ] ;thenecho "${backupdate} ${bak_size} incremental Failed"\>>${backup_base}/report/his.logelseecho "${backupdate} ${bak_size} incremental ${bak_status}"\ >>${backup_base}/report/his.logfifi}function purge_backup(){# purge the backup files#solution 1:#backup_dir=`ls -p ${backup_base}|grep "/"|egrep "fullbackup|increment"`#solution 2:find ${backup_base} -maxdepth 1 -ctime +${expired_day} -type d|\ egrep "fullbackup|increment" |xargs rm -rf {} \;#solution 3:#backup_dir=`find ${backup_base} -maxdepth 1 -ctime +${expired_day} -type d|egrep "fu#llbackup|increment"`#for i in backup_dir#do#  rm -rf ${i}#done# purge the expired backup tar.gz filesfind ${backup_base}/*.tar.gz -ctime ${expired_day} -exec rm {} \;}function sendmail(){cat ${backup_base}/report/his.log|mail -v -s "backup report" kin.zhang@kinzhang.com}schedule_jobpurge_backupsendmail

原创粉丝点击