实战:INNOBACKUPEX for mysql 5.6自己主动备份脚本

时间:2022-06-11 14:42:38

#backup.sh

#!/bin/sh

#

# 第一次运行它的时候它会检查是否有全然备份,否则先创建一个全库备份

# 当你再次执行它的时候。它会依据脚本中的设定来基于之前的全库备份进行增量备份

#ocpyang@126.com

INNOBACKUPEX_PATH=innobackupex  #INNOBACKUPEX的命令

INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX_PATH  #INNOBACKUPEX的命令路径

#mysql目标server以及username和password

MYSQL_CMD="--host=192.168.2.188 --user=system --password=password --port=3306"

MYSQL_UP="--user=system --password=password -ppassword"  #mysql的username与password

TMPLOG="/tmp/innobackupex.$$.log"

MY_CNF=/usr/local/mysql/my.cnf #mysql的配置文件

MYSQL=/usr/bin/mysql

MYSQL_ADMIN=/usr/bin/mysqladmin

BACKUP_DIR=/backup # 备份的主文件夹

FULLBACKUP_DIR=$BACKUP_DIR/full # 全库备份的文件夹

INCRBACKUP_DIR=$BACKUP_DIR/incre # 增量备份的文件夹

FULLBACKUP_INTERVAL=86400 # 全库备份的间隔周期,时间:秒

KEEP_FULLBACKUP=1 # 至少保留几个全库备份

logfiledate=backup.`date +%Y%m%d%H%M`.txt

#開始时间

STARTED_TIME=`date +%s`

#############################################################################

# 显示错误并退出

#############################################################################

error()

{

    echo "$1" 1>&2

    exit 1

}

# 检查运行环境

if [ ! -x $INNOBACKUPEXFULL ]; then

  error "$INNOBACKUPEXFULL未安装或未链接到/usr/bin."

fi

if [ ! -d $BACKUP_DIR ]; then

  error "备份目标目录:$BACKUP_DIR不存在."

fi

if [ -z "`$MYSQL_ADMIN $MYSQL_UP status | grep 'Uptime'`" ] ; then

 error "MySQL 没有启动执行."

fi

if ! `echo 'exit' | $MYSQL -s $MYSQL_CMD` ; then

 error "提供的数据库username或password不对!"

fi

# 备份的头部信息

echo "----------------------------"

echo

echo "$0: MySQL备份脚本"

echo "開始于: `date +%F' '%T' '%w`"

echo

#新建全备和差异备份的文件夹

mkdir -p $FULLBACKUP_DIR

mkdir -p $INCRBACKUP_DIR

#查找最新的全然备份

LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`

# 查找近期改动的最新备份

LATEST_FULL_BACKUP_CREATED_TIME=`stat -c %Y $FULLBACKUP_DIR/$LATEST_FULL_BACKUP`

#假设全备有效进行增量备份否则运行全然备份

if [ "$LATEST_FULL_BACKUP" -a `expr $LATEST_FULL_BACKUP_CREATED_TIME + $FULLBACKUP_INTERVAL + 5` -ge $STARTED_TIME ] ; then

# 假设最新的全备未过期则以最新的全备文件名称命名在增量备份文件夹下新建文件夹

echo -e "全然备份$LATEST_FULL_BACKUP未过期,将依据$LATEST_FULL_BACKUP名字作为增量备份文件夹命名"

echo "        "

NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP

mkdir -p $NEW_INCRDIR

# 查找最新的增量备份是否存在.指定一个备份的路径作为增量备份的基础

LATEST_INCR_BACKUP=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`

if [ ! $LATEST_INCR_BACKUP ] ; then

INCRBASEDIR=$FULLBACKUP_DIR/$LATEST_FULL_BACKUP

echo -e "增量备份将以$INCRBASEDIR作为备份基础"

echo "        "

else

INCRBASEDIR=$LATEST_INCR_BACKUP

echo -e "增量备份将以$INCRBASEDIR作为备份基础"

echo "        "

fi

echo "使用$INCRBASEDIR作为基础做新的增量备份."

$INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD --incremental $NEW_INCRDIR --incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1

else

echo  "*********************************"

echo -e "正在运行全新的全然备份...请稍等..."

echo  "*********************************"

$INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G  $MYSQL_CMD $FULLBACKUP_DIR > $TMPLOG 2>&1


fi

#保留一份备份的具体日志

cat $TMPLOG>/backup/$logfiledate

if [ -z "`tail -1 $TMPLOG | grep 'innobackupex: completed OK!'`" ] ; then

 echo "$INNOBACKUPEX命令运行失败:"; echo

 echo -e "---------- $INNOBACKUPEX_PATH错误 ----------"

 cat $TMPLOG

 rm -f $TMPLOG

 exit 1

fi

THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPLOG`

rm -f $TMPLOG

echo -n "数据库成功备份到:$THISBACKUP"

echo

# 提示应该保留的备份文件起点

LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`

echo -e "必须保留$KEEP_FULLBACKUP份全备和全备$LATEST_FULL_BACKUP以后的全部增量备份."

#删除过期的全备

echo -e "寻找过期的全备文件并删除">>/backup/$logfiledate

for efile in $(/usr/bin/find $FULLBACKUP_DIR/ -mtime +6)

do

 if [ -d $efile ]; then

 rm -rf $efile

 echo -e "删除过期全备文件:$efile" >>/backup/$logfiledate

 elif [ -f $efile ]; then

 rm -rf $file

 echo -e "删除过期全备文件:$efile" >>/backup/$logfiledate

 fi;

 

done

if [ $?

-eq "0" ];then

   echo

   echo -e "未找到能够删除的过期全备文件"

fi

echo

echo "完毕于: `date +%F' '%T' '%w`"

exit 0