基于Innobackupex的MySQL备份脚本

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

1、脚本描述
a、支持增量备份以及全备
b、需要传递到备份脚本(如备份路径,连接相关参数等)
c、基于周日,周三的全量备份,其他增量备份
d、可根据需要调整脚本,比如压缩备份的文件夹以及rsync等

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 <<EOF
Usage: $0 [configure-options]
  -?, --help                Show this help message.
  --backup-dir=<>           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 "[email protected]"
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 {} \;

# Send mail for backup result.
echo "" >>${logFile}
echo "Below is detail log for current innobackup.">>${logFile}
cat ${logDir}/bak_${ts}.log >>${logFile}
mailadd=‘[email protected],[email protected]‘

if [ -e "${logFile}" ]; then
   status=`grep -i "innobackupex: completed OK!" ${logFile}`
   if [ -n "${status}" ]; then
      cat ${logFile} |mutt -s "Successful backup for MySQL hotbackup on `hostname`." $mailadd
   else
      cat ${logFile} |mutt -s "Failed backup for MySQl hotbackup on `hostname`." $mailadd
   fi
else
   echo "The hotbackup logfile was not found on `hostname`." | \
   mutt -s "Failed backup for MySQl hotbackup on `hostname`." $mailadd
fi

exit

3、调用示例

SHELL> more call_innobk.sh
#!/bin/bash
/db_scripts/innobk.sh --backup-dir=/data/backup --host=127.0.0.1 --port=3306 --user=innobk --password=InnoBK

SHELL> crontab -l
0 3 * * * /db_scripts/call_innobk.sh

SHELL> cd /data/backup
SHELL> ls
checkpoint  log  physical
SHELL> cd physical/
SHELL> ls
20141228  20141231  20150104  20150107
SHELL> cd 20150107
SHELL> ls -hltr
total 16K
drwxr-xr-x 9 root root 4.0K Jan  7 03:05 base_20150107    #全备
drwxr-xr-x 9 root root 4.0K Jan  8 03:04 inc_20150108     #增备
drwxr-xr-x 9 root root 4.0K Jan  9 03:03 inc_20150109     #增备
drwxr-xr-x 9 root root 4.0K Jan 10 03:03 inc_20150110     #增备

时间: 2024-10-13 03:08:40

基于Innobackupex的MySQL备份脚本的相关文章

mysql 备份脚本(xtrabackup)

mysql备份脚本,记录一下: 全备: #!/bin/sh  set -x                                                             USER=root  PASSWORD=123456 time=`date +"%y-%m-%d-%H-%M"`   date >> /data/mysqlbackup/logs/$time'_allbackup.log'    #1 begin Full Backups  ech

Windows下MySQL备份脚本

1 @echo off 2 rem MySQL备份脚本 支持全备份 3 rem 增量备份需要MySQL服务加--log-bin参数执行 4 rem 数据还原 mysql -u root -p < backup.sql 5 rem ===================== 6 rem 全局变量 7 set dump=mysqldump.exe 8 set rand=%random% 9 rem ===================== 10 11 rem 检测MySQL路径是否添加到PATH变

两套mysql备份脚本

数据备份其重要性无需多言,再细想一步,我们至少需要两种备份:一种逻辑备份(mysqldump生成sql文件):一种物理备份(xtrabackup可很好完成).逻辑备份在出问题时能提供更细粒度的恢复和对比:物理备份在整库范围的数据恢复或者增加从库方面有着更高额效率. 首先看下逻辑备份脚本,比较简单 #!/bin/sh ###每天运行一次 ###定义用户 密码 备份目录等信息 user=mysqldump psd=mysqldump backup_base=/data/mysql_backup da

xtrabackup的mysql备份脚本

一.安装xtrabackup工具 安装方法见:xtrabackup安装 二.配置备份脚本 1.拷贝mysql_backup_shell到mysql业务服务器目录,比如:/data/backup_shell/mysql_backup_shell 2.进入拷贝后mysql_backup_shell所在目录, 然后执行chmod 777 mysql_backup.sh 3.修改backup.conf 需要修改的项: mysql_conf_file=/usr/local/meb/my.cnf.3309

mysql备份脚本

对extmail数据库进行每天01:00完全热备份,并可以完全恢复! 一.先建立备份脚本,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 vi mysql_extmail_bak.sh #!/bin/bash # Program #    use mysqldump to Fully backup mysql data per week! BakDir=/mysqlback#要备份到的目录 LogFile=/var/log/bak.log#备份记录日志

mysql 备份脚本以及定时任务

数据是企业最宝贵的资源,作为运维人员备份数据库的数据是特别重要的一项工作,以下是个人查找资料以及个人总结所记录的mysql数据库备份脚本,记录在此作为学习和复习笔记. (1)分库备份脚本 #/bin/sh #version 0.1 MYUSER=root MYPASS=123123 #SOCKET=/data/3306/mysql.sock MYLOGIN="mysql -u$MYUSER -p$MYPASS " MYDUMP="mysqldump -u$MYUSER -p$

线上MySQL备份脚本

最近在研究数据库备份,定时执行备份任务,这里直接把备份脚本设置为crontab命令定时执行,脚本内容如下: [[email protected] ~]# cat mysql-backup.sh #!/bin/bash dbpwd='possw0rd123' dbuser=root host=127.0.0.1 port=3306 dbarg=" -u$dbuser -p$dbpwd -h$host -P$port --default-character=utf8 " DATE=`dat

mysql备份脚本(shell)

mysql数据库shell备份脚本 任何项目的运行都离不开数据,持久化数据对于一个企业尤为重要,任何时候都不可掉以轻心,下面是我自己写的一个shell脚本,用来备份数据,分享给大家. #!/bin/bash # 数据库连接信息 MY_user="" MY_pass="" MY_port="3306" # 环境变量 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/

mysql备份脚本及备份失败报警

[[email protected] myfile]# cat mysqlback_zabbix.sh  #!/bin/bash USER=root DB=zabbix #PASSWORD=123456 SERVER=172.16.9.129 DIR_HOME=/vol/myfile/mysql_$SERVER LOG_FILE=$DIR_HOME/logs/mysqlback.log if [ ! -d $DIR_HOME ];then mkdir $DIR_HOME ;fi if [ ! -