#!/bin/sh
#备份主机
remote_ip=100.0.132.160
Master_ip=100.20.132.158
VIP=100.20.132.166
#备份用户
user='root'
#密码
password='00000'
# 返回年月日
backup_date=`date +%F`
# 返回时分秒
backup_time=`date +%H-%M-%S`
# 返回今天是这周的第几天
backup_week_day=`date +%u`
backup_ok=0
#备份目录
socket=/data/mysql/mysql.sock
# 备份路径
backup_dir=/data/xtrabackup
backup_dir_local=/data/xtrabackup_local
# 数据目录
datadir=/data/mysql
# percona-xtrabackup 备份软件路径
xtrabackup_dir=/usr/bin
# 全备是在一周的第几天
full_backup_week_day=6
#周期性全量增量开始日期# 全量备信息名称前缀
full_backup_prefix=full
# 增量备信息名称前缀
increment_prefix=incr
# mysql配置文件
mysql_conf_file=/etc/my.cnfcycle=$backup_date
cycle_record=$backup_dir/cycle_record.txtif [ ! -f $backup_dir/cycle_record.txt ];thenindex=$backup_date
elseif [ "$full_backup_week_day" -eq `date +%u` ]; thenindex=$backup_dateelseindex=`cat $backup_dir/cycle_record.txt`fi
fi
index_file=$backup_dir/backup_$index.index
index_file_local=$backup_dir_local/backup_$index.indexlog_dir=$backup_dir/log
if [ ! -d "$backup_dir" ];thenmkdir -p $backup_dir
fi
if [ ! -d "$backup_dir_local" ];thenmkdir -p $backup_dir_local
fi
if [ ! -d "$log_dir" ];thenmkdir -p $log_dir
fifunction append_index_to_file() {echo "{week_day:$backup_week_day, \dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, \type:${1}, \date:${backup_date}}" >> $index_file
}
function append_index_to_file_local() {echo "{week_day:$backup_week_day, \dir:${1}_${backup_date}_${backup_time}_${backup_week_day}, \type:${1}, \date:${backup_date}}" >> $index_file_local
}
# 判断是应该全备还是增量备份
# 0:full, 1:incr
function get_backup_type() {full_backup_week_day=$full_backup_week_daybackup_type=0if [ ! -f "$index_file" ]; thentouch "$index_file"fiif [ "$full_backup_week_day" -eq `date +%u` ]; thenbackup_type=0elsebackup_type=1fiif [ ! -n "`cat $index_file`" ]; thenbackup_type=0fireturn $backup_type
}
#推送远程全量备份
function full_backup (){backup_folder=${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day}if [ ! -d $backup_dir_local/$cycle/ ]; thenmkdir $backup_dir/$cycle/fiecho $cycle>$backup_dir/cycle_record.txtssh $user@$remote_ip "if [ ! -d $backup_dir/$cycle/ ];then mkdir -p $backup_dir/$cycle; fi "innobackupex --defaults-file=$mysql_conf_file --no-timestamp --user=$user \--password=$password --host=${VIP} --port=3306 --extra-lsndir=$backup_dir/$cycle/$backup_folder --compress \--stream=xbstream $backup_dir | ssh $user@$remote_ip "gzip ->$backup_dir/$cycle/$backup_folder.tar.gz" if [ $? -eq 0 ];thenappend_index_to_file $full_backup_prefixlog_info 0 fullelselog_info 1 fullfi
}#推送远程增量备份{week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr}
incremental (){backup_folder=${increment_prefix}_${backup_date}_${backup_time}_${backup_week_day}incr_record=`cat $backup_dir/cycle_record.txt`echo $backup_dir/$incr_record#cd $backup_dir/$incr_recordincr_base_folder=`sed -n '$p' $index_file | \awk -F '[, {}]*' '{print $3}' | \awk -F ':' '{print $2}'`echo $backup_dir/$incr_record/${incr_base_folder}innobackupex --defaults-file=$mysql_conf_file \--no-timestamp --user=$user --password=$password --host=${VIP} --port=3306 \--stream=xbstream --compress --extra-lsndir=$backup_dir/$incr_record/$backup_folder \--incremental backup_folder --incremental-basedir=$backup_dir/$incr_record/${incr_base_folder} \|ssh $user@$remote_ip "gzip ->$backup_dir/$incr_record/$backup_folder.tar.gz" \if [ $? -eq 0 ];thenlog_info 0 incrappend_index_to_file $increment_prefixelselog_info 1 incrfi
}function full_backup_local (){backup_folder=${full_backup_prefix}_${backup_date}_${backup_time}_${backup_week_day}if [ ! -d $backup_dir_local/$cycle/ ]; thenmkdir $backup_dir_local/$cycle/fiecho $cycle>$backup_dir_local/cycle_record.txtinnobackupex --defaults-file=$mysql_conf_file --no-timestamp --user=$user \--password=$password --host=${VIP} --port=3306 $backup_dir_local/$cycle/$backup_folderif [ $? -eq 0 ];thenecho "全量备份成功"append_index_to_file_local $full_backup_prefixlog_info_local 0 fullelselog_info_local 1 fullfi
}#本地增量备份{week_day:1,dir:full/incr_2015-12-29_00-00-00_7,type:full/incr}
incremental_local (){backup_folder=${increment_prefix}_${backup_date}_${backup_time}_${backup_week_day}incr_record=`cat $backup_dir_local/cycle_record.txt`echo $backup_dir_local/$incr_record#cd $backup_dir/$incr_recordincr_base_folder=`sed -n '$p' $index_file_local | \awk -F '[, {}]*' '{print $3}' | \awk -F ':' '{print $2}'`echo $incr_base_folderecho $backup_dir_local/$incr_record/${incr_base_folder}innobackupex --defaults-file=$mysql_conf_file \--no-timestamp --user=$user --password=$password --host=${VIP} --port=3306 \--incremental $backup_dir_local/$incr_record/$backup_folder --incremental-basedir=$backup_dir_local/$incr_record/$incr_base_folder if [ $? -eq 0 ];thenlog_info_local 0 incrappend_index_to_file_local $increment_prefixelselog_info_local 1 incrfi
}
function log_info (){
if [[ "$1" = "0" ]];thenif [ "$2" = "full" ];thenecho "全量备份成功" >$backup_dir/successelseecho "增量备份成功" >$backup_dir/failfi
elseif [ "$2" = "full" ];thenecho "全量备份失败" >$backup_dir/successelseecho "增量备份失败" >$backup_dir/failfi
fi
}
function log_info_local ()
{
echo "全量备份成功"
if [[ "$1" = "0" ]];thenif [ "$2" = "full" ];thenecho "全量备份成功" >$backup_dir/successelseecho "增量备份成功" >$backup_dir/failfi
elseif [ "$2" = "full" ];thenecho "全量备份失败" >$backup_dir/successelseecho "增量备份失败" >$backup_dir/failfi
fi
}
function run_auto() {get_backup_typebackup_type=$?echo $backup_typecase $backup_type in0)full_backup ;;1)incremental ;;*)echo "Please use it this way. Usage:$0 {0|1}";;esac
}
function run_manual() {case $1 infull)full_backup;;incremental)incremental;;*)echo "Please use it this way. Usage:$0 {Full|incremental}";;esac
}
function run_auto_local() {get_backup_typebackup_type=$?case backup_type in0)full_backup_local;;1)incremental_local;;*)echo "Please use it this way. Usage:$0 {Full|incremental}";;esac
}
function run_manual_local() {#echo $1case $1 infull)full_backup_local;;incremental)incremental_local;;*)echo "Please use it this way. Usage:$0 {full|incremental}";;esac
}
run_auto
#run_auto_local
#run_manual_local $1
#run_manual $1