#!/bin/bash set -x anynowtime="date +'%Y-%m-%d %H:%M:%S'" NOW="echo [\`$anynowtime\`][PID:$$]" TimeD=$1 TimeS=$2 ##### 可在脚本开始运行时调用,打印当时的时间戳及PID。 function job_start { echo "`eval $NOW` job_start" ####### 获取备份IP ###### localIp=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"` echo $localIp srcIp="" source /data/mmogrun/mysql_back/ipCfg.sh for element in ${ipCfg[@]} do echo $element array=(`echo $element | tr '-' ' '` ) if [[ ${array[0]} == $localIp ]];then srcIp=${array[1]} echo "$srcIp srcIp" fi done ####### 根据服务器时区 获取时间 ################ ### timezone ##### tzs="date +'%z'" tz="`eval $tzs`" ## 东 西 tzwe=${tz:0:1} ## 几区### tzH=${tz:1:2} tzS=${tz:3:2} echo "$tzwe $tzH $tzS" ## 输入的回滚到的北京时间 echo $TimeD $TimeS signTime=`eval "date -d '$TimeD $TimeS'"` echo $signTime ## 东 if [[ "${tzwe}" == + ]]; then if [[ $((10#$tzH))>8 ]]; then echo "$((10#$tzH)) 222 " hourlate=$(($((10#$tzH)) - 8)) lasttime=`eval "date -d '${signTime} ${hourlate} hour'"` elif [[ $((10#$tzH))<8 ]];then echo "$((10#$tzH)) 111 " hourlate=$((8 - $((10#$tzH)))) echo " 345 ${signTime} ${hourlate}" lasttime=`eval "date -d '${signTime} ${hourlate} hour ago'"` else lasttime=`eval "date -d '${signTime}'"` fi ## 西 elif [ "${tzwe}" == - ]; then hourlate=$((8 + $((10#$tzH)))) lasttime=`eval "date -d '${signTime} ${hourlate} hour ago'"` fi echo $lasttime llasttime="date -d '${lasttime}' +'%Y%m%d_%H'" ##回滚到当前时区的天 targetD=`eval "date -d '${lasttime}' +'%Y-%m-%d'"` ## 回滚到当前时区的秒 targetS=`eval "date -d '${lasttime}' +'%H:%M:%S'"` echo " 123 $targetD $targetS" ###再加上前五个小时,一共去找六个消失的,不多,所以没遍历加 llasttime_1H="date -d '${lasttime} 1 hour ago' +'%Y%m%d_%H'" llasttime_2H="date -d '${lasttime} 2 hour ago' +'%Y%m%d_%H'" llasttime_3H="date -d '${lasttime} 3 hour ago' +'%Y%m%d_%H'" llasttime_4H="date -d '${lasttime} 4 hour ago' +'%Y%m%d_%H'" llasttime_5H="date -d '${lasttime} 5 hour ago' +'%Y%m%d_%H'" ## 塞到数组里面,遍历 arrTime=("`eval $llasttime`" "`eval $llasttime_1H`" "`eval $llasttime_2H`" "`eval $llasttime_3H`" "`eval $llasttime_4H`" "`eval $llasttime_5H`") cd ~ fileTimes=0; for element in ${arrTime[@]} do echo $element scp -P2009 mmog@${srcIp}:/data/mysql_backup/${element}-dbmaccount.sql.tgz /data/mmogrun/mysql_back/data/ scp -P2009 mmog@${srcIp}:/data/mysql_backup/${element}-dbmgame.sql.tgz /data/mmogrun/mysql_back/data/ if [ -f ~/${element}-dbmaccount.sql.tgz ];then targetGame="${element}-dbmgame.sql" targetAccount="${element}-dbmaccount.sql" echo "account game ::: $targetAccount $targetGame" break fi fileTimes=$fileTimes+1 done ##### 六次都没找到 出问题了 if [ $fileTimes == 6 ];then job_fail exit -1; fi scp -P2009 mmog@${srcIp}:/var/log/mysql/mysql-bin.* /data/mmogrun/mysql_back/data/ cd /data/mmogrun/mysql_back/data/ tar -zxvf "${targetGame}.tgz" cp ./home/mmog/mysql_backup/${targetGame} ./ tar -zxvf "${targetAccount}.tgz" cp ./home/mmog/mysql_backup/${targetAccount} ./ cd /data/mmogrun/mysql_back/ echo "rollback ./data/${targetGame} $targetD $targetS" ./mysql_recover.sh $targetGame $targetD $targetS echo "rollback $targetAccount $targetD $targetS" ./mysql_recover.sh ./data/${targetAccount} $targetD $targetS job_success } ##### 可在脚本执行成功的逻辑分支处调用,打印当时的时间戳及PID。 function job_success { MSG="$*" echo "`eval $NOW` job_success:[$MSG]" exit 0 } ##### 可在脚本执行失败的逻辑分支处调用,打印当时的时间戳及PID。 function job_fail { MSG="$*" echo "`eval $NOW` job_fail:[$MSG]" exit 1 } job_start ###### 作业平台中执行脚本成功和失败的标准只取决于脚本最后一条执行语句的返回值 ###### 如果返回值为0,则认为此脚本执行成功,如果非0,则认为脚本执行失败 ###### 可在此处开始编写您的脚本逻辑代码