xtrabackup备份脚本

说明:由于备份时使用--compress压缩,所以恢复时会调用linux的qpress命令解压,需要单独安装。下载:http://download.csdn.net/detail/u010587433/9518350

上传服务器后,添加执行权限即可使用。否则在恢复时报找不到qpress命令。恢复脚本:

#!/bin/sh
#
# recover scripts: need one input parameter--the absolutely path of full backup or incr backup
# execute repeatablely
# base-on  xtrabackup 2.3.3

# MYSQL environment variable
export MYSQL_BASE=/home/mariadb 
export MYSQL_HOME=$MYSQL_BASE/product/10.1/mariadb-1
export DATADIR=$MYSQL_BASE/data
export TMPDIR=/tmp 
export PATH=$MYSQL_HOME/bin:$PATH

# recover cmd
INNODB_DIR=/usr/local/xtrabackup/bin
INNODBBAK=$INNODB_DIR/innobackupex
DECO_PARAM="--decompress"
APPL_PARAM="--apply-log --use-memory=4G"
APPR_PARAM="--apply-log --redo-only --use-memory=4G"
COPY_PARAM="--copy-back"
CKPT=xtrabackup_checkpoints
TMPA=/tmp/xtrbakup_ckpt_tmpa
TMPB=/tmp/xtrbakup_ckpt_tmpb

# MYSQL db info
MYSQL_INFO="--host=10.128.11.90 --user=root --password=123456 --protocol=tcp --port=3306"  
MYSQL_STATUS=''
MYSQL_CNF=/etc/my.cnf
ERRORLOG=`grep -i "^log-error" $MYSQL_CNF | awk -F= '{print $2}'`

# backup dir
BAK_DIR=/tmp/mybak
FBAK_DIR=$BAK_DIR/full
IBAK_DIR=$BAK_DIR/incr

# time variable
DAT=`date +%Y%m%d%H%M`

# recover log
LOG_DIR=$BAK_DIR/log
FDLOG=$LOG_DIR/$DAT.f.decom.log
FALOG=$LOG_DIR/$DAT.f.apply.log
FCLOG=$LOG_DIR/$DAT.f.copy.log
IDLOG=$LOG_DIR/$DAT.i.decom.log
IALOG=$LOG_DIR/$DAT.i.apply.log

# echo error and quit
error()
{
    echo "$1" 1>&2
    exit 1
}


if [ ! -e $INNODBBAK ]; then
  error "Recover cmd: $INNODBBAK don't find."
else
  if [ ! -x $INNODBBAK ]; then
    error "Recover cmd: $INNODBBAK don't executable."
  fi
fi

if [ ! -d $BAK_DIR ]; then
  error "Backup dir: $BAK_DIR don't exist."
fi

if [ $# != 1 ] ; then
    error "Must input parameter and only one parameter." 
fi

if [ ! -d $1 ]; then
  error "backup dir $1 don't exist."
fi

echo "---------------------------------------------"
MYSQL_STATUS=`mysqladmin $MYSQL_INFO ping`
if [ "$MYSQL_STATUS" = "mysqld is alive" ];then
  echo
  echo "MYSQL is running."
  echo "Shutdown MYSQL server..."
  mysqladmin $MYSQL_INFO Shutdown
  if [ $? = 0 ]; then
    echo "Successed shutdown MYSQL server."
    echo "Time: `date +%F' '%T' '%A`"
  else
    error "Failed fhutdown MYSQL server."
  fi
fi

PAR_DIR=`dirname $1`


if [ $PAR_DIR = $FBAK_DIR ]; then
  # only recover full backup when input is full backup dir or incr backup dir that have not incr backup
  echo "This is a full backup dir."
  echo "To recover full backup `basename $1`."
  echo
  FBAK=$1
  echo 
  echo "Begin time `date +%F' '%T`"
  echo 
  echo "Decompress full backup..."
  $INNODBBAK --defaults-file=$MY_CNF $DECO_PARAM $FBAK > $FDLOG 2>&1
  if [ ! -z "`tail -1 $FDLOG | grep 'completed OK!'`" ]; then
    echo "Successed decompress full backup."
  fi
  echo 
  echo "Prepare full backup..."
  $INNODBBAK --defaults-file=$MY_CNF $APPL_PARAM $FBAK > $FALOG 2>&1
  if [ ! -z "`tail -1 $FALOG | grep 'completed OK!'`" ]; then
    echo "Successed prepare full backup."
  fi

else
  if [ $PAR_DIR = $IBAK_DIR ]; then
    echo 
    echo "This is a incr backup path."
    echo "Begin to recover from full backup."
    FBAK=`echo $1 | sed 's/incr/full/'`
    if [ ! -d $FBAK ]; then
      error "Full backup $FBAK don't exist."
    fi

    # for repeat recover
    # the xbackup ckpt will change after recover
    # it is modified the xtrabackup_checkpoints file if execute recover scripts again 
    full_lsn=`grep -i "^last_lsn" ${FBAK}/$CKPT | awk '{print $3}'`
    first_incr=`find $1 -mindepth 1 -maxdepth 1 -type d | sort -n | head -1`
    first_lsn=`grep -i "^last_lsn" ${first_incr}/$CKPT | awk '{print $3}'`
    first_from_lsn=`grep -i "^from_lsn" ${first_incr}/$CKPT | awk '{print $3}'`

    if [ $full_lsn -ge $first_lsn ]; then

      echo
      echo "This is not the first execute recover scripts."
      echo "The xbackup ckpt have been changed."
      echo "It is modified last_lsn and to_lsn to first incr backup ckpt from_lsn in xtrabackup_checkpoints when execute again."
      echo
      echo "Full backup last_lsn is : $full_lsn."
      echo
      echo "The first incr backup last_lsn is : $first_lsn."
      echo
      echo "The first incr backup from_lsn is : $first_from_lsn."

      # modify xtrabackup_checkpoints with tmp file
      mv -f ${FBAK}/$CKPT $TMPA
      sed "s/to_lsn = $full_lsn/to_lsn = $first_from_lsn/"     $TMPA > $TMPB
      sed "s/last_lsn = $full_lsn/last_lsn = $first_from_lsn/" $TMPB > ${FBAK}/$CKPT
      rm -r $TMPA
      rm -r $TMPB
    else
      echo 
      echo "This is the first execute recover scripts."
    fi

    echo 
    echo "Decompress full backup..."
    $INNODBBAK --defaults-file=$MY_CNF $DECO_PARAM $FBAK > $FDLOG 2>&1
  if [ ! -z "`tail -1 $FDLOG | grep 'completed OK!'`" ]; then
    echo "Successed prepare full backup."
  fi
    echo 
    echo "Prepare full backup..."
    $INNODBBAK --defaults-file=$MY_CNF $APPR_PARAM $FBAK > $FALOG 2>&1
  if [ ! -z "`tail -1 $FALOG | grep 'completed OK!'`" ]; then
    echo "Successed prepare full backup."
  fi

    # 
    INCR_CNT=`find $1 -mindepth 1 -maxdepth 1 -type d | wc -l`
    echo
    echo "There are $INCR_CNT incr backup:"
    CNT=1
    for i in `find $1 -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`;
    do
      # echo all incr backup info
      echo "$CNT:$i"
      CNT=`expr $CNT + 1`
    done

    # input recover to incr backup dir
    echo
    read -t 10 -p "Input one number correspond to the above [default $INCR_CNT]: " INCR_NUM

    # recover all incr backup dir when not input
    if [ -z $INCR_NUM ]; then
      INCR_NUM=$INCR_CNT
    fi


    CNT=1
    for i in `find $1 -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -n`;
    do
      incr_lsn=`grep -i "^last_lsn" ${1}/${i}/$CKPT | awk '{print $3}'`
      # turn on recover incr backup 
      if [ $INCR_NUM -ge $CNT ];then
        echo
        echo "The incr backup $CNT last_lsn is: $incr_lsn."
        echo "Decompress incr backup $i."
        $INNODBBAK --defaults-file=$MYSQL_CNF $DECO_PARAM $1/$i  > $IDLOG.$CNT 2>&1
        if [ ! -z "`tail -1 $IDLOG.$CNT | grep 'completed OK!'`" ]; then
          echo "Successed decompress."
        fi
        echo "Prepare incr backup $i."
        $INNODBBAK --defaults-file=$MYSQL_CNF $APPR_PARAM $FBAK --incremental-dir=${1}/$i > $IALOG.$CNT 2>&1
        if [ ! -z "`tail -1 $IALOG.$CNT | grep 'completed OK!'`" ]; then
          echo "Successed prepare."
        fi
        CNT=`expr $CNT + 1`
      else
        break
      fi
    done
  else
    error "Unkonw backup info."
 fi

fi


echo
echo "Delete data dir $DATADIR..."
rm -rf $DATADIR/*
echo "Successed delete data dir $DATADIR."

echo 
echo "Copy-back full backup..."
$INNODBBAK --defaults-file=$MYSQL_CNF $COPY_PARAM $FBAK > $FCLOG 2>&1
if [ ! -z "`tail -1 $FCLOG | grep 'completed OK!'`" ]; then
  echo "Successed copy-back full backup."
fi

echo 
echo "End time `date +%F' '%T' '%A`"

# change the owner of datadir
echo
echo "Change the owner of mysql datadir..."
chown -R mysql:mysql $DATADIR/*
echo "Successed change the owner of datadir."

# start mysql server
echo
service mariadbd start
MYSQL_STATUS=`mysqladmin $MYSQL_INFO ping`
if [ "$MYSQL_STATUS" = "mysqld is alive" ];then
  echo
  echo "Successed start MySQL server."
else
  echo
  echo "MySQL server start failed."
  echo "See MySQL error log $ERRORLOG."
fi

echo
echo "`date +%F' '%T' '%A`"
echo "---------------------------------------------"
exit 0


参考:http://blog.csdn.net/yangzhawen/article/details/44857097

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海阿丽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值