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

2015-01-23 22:03:59 · 作者: · 浏览: 14

Innobackupex是Xtrabackup的一部分,其实质也是调用xtrabackup。主要的不同是Xtrabackup除了支持innodb引擎外还支持xtradb引擎。本文主要封装了Innobackupex到shell脚本进行定期备份,供大家参考。

?

1、脚本描述

?

2、脚本内容

################################################################################
# File     : innobk.sh                                                         # 
# Author   : Leshami                                                           #     
# Blog     : http://blog.csdn.net/leshami                                      #
# Date     : 20141113                                                          #
# Description :                                                                #
#    The script will call innobackupex to                                      #
#    take a full or increment backup for mysql db.                             #
#    Currently it will take follow principal to backup:                        #
#       Sun,Wend take full backup.                                             #  
#       Mon,Tue,Thur,Fri,Sat take incremental backup.                          #
#                                                                              #
# Usage Example:                                                               #
#     innobk.sh --help|-?                                                      #
#     innobk.sh --backup-dir=/dbbak --defaults-file=/inst3606/my3606.cnf \     #
#                   --host=127.0.0.1 --port=3606 --user=xxx --password=xxx     #
#		                                                                       #
################################################################################
# Change History:                                                              #
# --------------------------------------------------                           #     
# Init Development	Leshami		2014-11-13                                     #
################################################################################

#!/bin/bash
#set -x

# Get the key value of input arguments format like '--args=value'.
function get_key_value()
{
    echo "$1" | sed 's/^--[a-zA-Z_-]*=//' 
}

# Usage will be helpful when you need to input the valid arguments.
function usage()
{
cat <           Set backup directory
  --defaults-file=[]        Set mysql configuration file directory
  --host=<>                 Set mysql host
  --port=<>                 Set mysql port
  --user=<>                 Set mysql user name
  --password=<>             Set mysql user password
EOF
}

# 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}"      >>${logFi