标签:
#!/bin/bash #日期转为天数 function date2days { echo "$*" | awk ‘{ z=int((14-$2)/12); y=$1+4800-z; m=$2+12*z-3; j=int((153*m+2)/5)+$3+y*365+int(y/4)-int(y/100)+int(y/400)-2472633; print j }‘ } #说明:脚本执行策略为每天执行一次,执行前需要先建立config文件,并在config文件 #中添加 #backup_full= #backup_pre_name= #即可,注意路径。 #备份策略,每七天一个循环,第一天为全备份,第二天至第六天为增量备份。 #后续会增加已备份文件压缩转移定期删除部分 ####################### db_user="root" db_passwd="" db_defaults_file="/home/backup/config/3306.cnf" db_socket="/var/lib/mysql/mysql_3306.sock" db_backup="/home/backup/" db_backup_fulldir="/home/backup/full/" db_backup_incrementaldir="/home/backup/incremental/" db_backup_gzfull="/home/backup/gzip/" db_backup_tarfull="/home/backup/tar.gzdb/" #得到当前时间和配置文件 time="$(date +"back_%d-%m-%Y")" source /home/backup/config/config #计算今天到上次全备份相隔天数 _Day=$(date2days `echo ${backup_full:5:10}|awk ‘BEGIN{FS="-"}{print $3,$2,$1}‘`) Day=$(date2days `date +"%Y %m %d"`) echo $_Day echo $Day let result=$Day-$_Day echo "相差$result天" if [ -z ${backup_full} ] || [ $result -ge 7 ] ; then #todo echo ‘全备份‘ backup_full=${time} innobackupex --defaults-file=$db_defaults_file --user=$db_user --password --no-timestamp --socket=$db_socket ${db_backup_fulldir}${backup_full}/ #更新配置文件 echo "backup_full=${backup_full}" >/home/backup/config/config echo "backup_pre_name=full/${backup_full}" >>/home/backup/config/config #自动删除两周以前的备份文件 #获取本次全备份两周前的文件夹名称 time_rm_f_7="$(date -d "7 days ago" +"back_%d-%m-%Y")" time_rm_i_6="$(date -d "6 days ago" +"back_%d-%m-%Y")" time_rm_i_5="$(date -d "5 days ago" +"back_%d-%m-%Y")" time_rm_i_4="$(date -d "4 days ago" +"back_%d-%m-%Y")" time_rm_i_3="$(date -d "3 days ago" +"back_%d-%m-%Y")" time_rm_i_2="$(date -d "2 days ago" +"back_%d-%m-%Y")" time_rm_i_1="$(date -d "1 days ago" +"back_%d-%m-%Y")" if test -d $db_backup_fulldir${time_rm_f_7} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_f_7}_full.tar.gz ${db_backup_fulldir}${time_rm_f_7}" su - root -c "rm -rf ${db_backup_fulldir}${time_rm_f_7}" echo "压缩目录rm $db_backup_fulldir${time_rm_f_7}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_6} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_6}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_6}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_6}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_6}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_5} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_5}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_5}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_5}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_5}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_4} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_4}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_4}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_4}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_4}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_3} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_3}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_3}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_3}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_3}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_2} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_2}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_2}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_2}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_2}" >>/home/backup/config/tar.log if test -d $db_backup_incrementaldir${time_rm_i_1} then su - root -c "tar -czvf ${db_backup_tarfull}${time_rm_i_1}_increment.tar.gz ${db_backup_incrementaldir}${time_rm_i_1}" su - root -c "rm -rf ${db_backup_incrementaldir}${time_rm_i_1}" echo "压缩目录rm $db_backup_incrementaldir${time_rm_i_1}" >>/home/backup/config/tar.log fi fi fi fi fi fi else echo "两周前备份不存在" >>/home/backup/config/tar.log fi echo ‘全备份‘ else #todo echo ‘增量备份‘ innobackupex --defaults-file=$db_defaults_file --socket=$db_socket --no-timestamp --incremental ${db_backup_incrementaldir}${time}/ --incremental-basedir=${db_backup}${backup_pre_name} #更新配置文件 echo "backup_full=${backup_full}" >/home/backup/config/config echo "backup_pre_name=/incremental/${time}" >>/home/backup/config/config echo ‘增量备份‘ fi
脚本使用说明
建立/home/backup目录
目录中包含 config full increment tar.gzdb 目录
再config目录中包含3306.cnf为配置文件 tar.log文件记录压缩记录
config文件包含当前备份文件名和全备份文件名,内容如下:
backup_full=back_27-10-2015
backup_pre_name=/incremental/back_28-10-2015
然后再crontab中配置每天晚上9点定时执行脚本即可
脚本备份说明
第一次执行为全备份,接下来六天为增量备份,下一次循环前会把全量和增量压缩移动到tar.gzdb文件夹中然后进行下一个周期
如果有不足的地方希望大家不吝赐教
标签:
原文地址:http://www.cnblogs.com/jiangwenju/p/4919633.html