基于Innobackupex的MySQL备份脚本(2)

# Parse the input arguments and get the value of the input argument.
if [ $# -eq 0 ];then
      usage
#      print_default
      exit 0;
fi

function parse_options()
{
  while test $# -gt 0
  do
    case "$1" in
    --backup-dir=*)
      backupDir=`get_key_value "$1"`;;
    --defaults-file=*)
      defaultFile=`get_key_value "$1"`;;
    --host=*)
      Host=`get_key_value "$1"`;;
    --port=*)
      mysqlPort=`get_key_value "$1"`;;
    --user=*)
      mysqlUser=`get_key_value "$1"`;;
    --password=*)
      mysqlPassword=`get_key_value "$1"`;;
    -? | --help )
      usage
#      print_default
      exit 0;;
    *)
      echo "Unknown option '$1'"
      exit 1;;
    esac
    shift
  done
}

# Call the parse_options function to parse the input arguments and initialisze env.
parse_options "$@"
physicalBackupDir=${backupDir}/physical
logDir=${backupDir}/log
checkPointDir=${backupDir}/checkpoint
cmdInno=/usr/bin/innobackupex
sock=/tmp/mysql.sock

day=`date +%w`
lastday=`date -d '1 days ago' +%Y%m%d`
dt=`date +%Y%m%d`
ts=`date +%Y%m%d%H%M%S`
logFile=${backupDir}/log/innobak_${ts}.log

if [ "${day}" -eq 0 ] || [ "${day}" -eq 3 ];then
  if [ ! -d "$physicalBackupDir/$dt" ];then
      echo "mkdir -p $physicalBackupDir/$dt"
      mkdir -p $physicalBackupDir/$dt   
  fi
fi

if [ -z "$defaultFile" ]; then
    defaultFile=/etc/my.cnf
fi

if [ ! -d "${logDir}" ]; then
    mkdir -p ${logDir}
fi

if [ ! -d "${checkPointDir}" ]; then
    mkdir -p ${checkPointDir}
fi 

echo "Start innobackup at `date`."              >>${logFile}
echo "Current defaults file is : ${defaultFile}" >>${logFile}
echo "Current host is : ${Host}"                >>${logFile}
echo "Current port is : ${mysqlPort}"            >>${logFile} 
echo "Current mysql user is : ${mysqlUser}"      >>${logFile}   
echo "Current password is : ${mysqlPassword}"   
echo "Current log directory is : ${logDir}"      >>${logFile}
echo "Current log file is : ${logFile}"          >>${logFile}

# Define backup function for full and incremental backup type.
function back_full()
{
echo "$cmdInno --user=$mysqlUser --password=$mysqlPassword  --no-timestamp \
      --defaults-file=$defaultFile $physicalBackupDir/$dt/base_${dt} \
      --socket=$sock 2> ${logDir}/bak_$ts.log" >>${logFile}

$cmdInno --user=$mysqlUser --password=$mysqlPassword  --no-timestamp \
  --defaults-file=$defaultFile $physicalBackupDir/$dt/base_$dt --socket=$sock 2> ${logDir}/bak_${ts}.log
  grep last_lsn $physicalBackupDir/$dt/base_$dt/xtrabackup_checkpoints|cut -b 12- >$checkPointDir/ckp_${dt}
}

function back_inc()
{
echo "  $cmdInno --user=$mysqlUser --password=$mysqlPassword --socket=$sock --no-timestamp \ 
  --defaults-file=$defaultFile --incremental $basedir/inc_$dt \
  --incremental-lsn=`cat $checkPointDir/ckp_$lastday` 2>${logDir}/bak_${ts}.log " >>${logFile}

$cmdInno --user=$mysqlUser --password=$mysqlPassword --port=${mysqlPort} --socket=$sock --no-timestamp \
 --defaults-file=$defaultFile --incremental $basedir/inc_$dt \
 --incremental-lsn=`cat $checkPointDir/ckp_$lastday` 2>${logDir}/bak_${ts}.log
  grep last_lsn $basedir/inc_$dt/xtrabackup_checkpoints|cut -b 12- >$checkPointDir/ckp_$dt
}

case $day in
    0)
        # Sunday Full backup
        back_full
        ;;
    1)
        # Monday Relatively Sunday's incremental backup
        basedir=$physicalBackupDir/`date -d "1 days ago" +%Y%m%d`
        back_inc
        ;;
    2)
        # Tuesday Compared with Monday's incremental backup
        basedir=$physicalBackupDir/`date -d "2 days ago" +%Y%m%d`
        back_inc
        ;;
    3)
        # Wednesday Full backup
        back_full
        ;;
    4)
        # Thursday  Relatively Wednesday's incremental backup
        basedir=$physicalBackupDir/`date -d "1 days ago" +%Y%m%d`
        back_inc
        ;;
    5)
        # Friday Compared with Thursday's incremental backup
        basedir=$physicalBackupDir/`date -d "2 days ago" +%Y%m%d`
        back_inc
        ;;
    6)
        # Saturday Compared with Friday's incremental backup
        basedir=$physicalBackupDir/`date -d "3 days ago" +%Y%m%d`
        back_inc
        ;;
esac

# Check backup log ,remove history logfile and bacupset.
retention=5

find ${physicalBackupDir} -type f -mtime +$retention -exec rm {} \;
find ${logDir} -type f -mtime +$retention -exec rm {} \;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/ad182560eb1d58a6a6f4efdd89adfbc5.html