西数超哥博客
运维经验教程分享的博客

基于innobackupex备份工具的mysql全量备份和增量备份shell脚本

参考网上的资料和根据自己服务器的mysql多实例实际情况,搞了一个基于innobackupex备份工具的mysql全量备份和增量备份脚本,暂定版本V1.0,后续还有很多需要改进和优化的。思路和方式很多,这里目前用做学习练习用,以后会改善脚本和功能实现。
该脚本基本可以实现需要的功能,但不够简洁和智能,代码过于臃肿。

#!/bin/bash
# innobackupex mysql full and incr
#author:21yunwei
#version:V1.0

BACKTIME=`date +%Y%m%d%H%M%S`
BACK_DIR=/home/mysqlbak
FULLBACKUP_DIR=/home/mysqlbak/full/
INCRBACKUP_DIR=/home/mysqlbak/incr/
MYSQL=/usr/local/mysql/bin/mysql
MYSQLADMIN=/usr/local/mysql/bin/mysqladmin
MYSQL_UP=`mysqladmin -uroot  -photmail.com  -S  /data/mysql_3306/mysql_3306.sock  status | grep  Uptime`
MYSQL_CNF=/data/test/mysql3306.cnf  
MYSQl_CMD=`mysql  -uroot  -pxxxxxx  -S /data/mysql_3306/mysql_3306.sock`
#INDEXFILE=/home/mysqlbak/full/index
OPRATE_LOGFILE=/home/mysqlbak/mysqlbak.log
LOGFILE=/home/mysqlbak/mysqlbak.log
INNOBACKUPEX=/usr/bin/innobackupex
INNOBACK_taillog=/home/mysqlbak/innobackupex.log
FULLBACK_INTERVAL=604800
STARTED_TIME=`date +%s`

if [ $MYSQL_UP -ne  0 ]
	then
	echo  "Mysql is down,please check"
	exit 1
fi

if [ ! -d $BACK_DIR ] 
	then
	echo  "$BACK_DIR is not exists,creating"
	/bin/mkdir -p $BACK_DIR
fi

if [ ! -d $FULLBACKUP_DIR ]
	then
	echo   "$FULLBACKUP_DIR is not exists,creating" 
	/bin/mkdir -p $FULLBACKUP_DIR
fi

if [ ! -d $INCRBACKUP_DIR ] 
	then
	 echo "INCRBACKUP_DIR is $INCRBACKUP_DIR"
	 /bin/mkdir -p $INCRBACKUP_DIR 
fi

if [ ! -f $LOGFILE ] 
	then
	echo "$LOGFILE  is not exists,creating "
	touch  $LOGFILE 
fi

if [ ! -f $INDEXFILE ] 
	then
	echo "$INDEXFILE  is not exists,creating "
	touch  $INDEXFILE
fi

echo "----------------------------"
echo
echo "$0: MySQL备份脚本"
echo "备份开始于: $BAKTIME "
echo
echo "----------------------------"

#find latest full and give latest value
LATEST_FULL_BACK=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n" | sort -nr | head -1`

#find last full and get mtime
LATEST_FULL_BACK_CREATED_TIME=`stat -c %Y $FULLBACKUP_DIR/$LATEST_FULL_BACK`

#judge full or not ,then decide to make full or incr
if [ "$LATEST_FULL_BACK" -a `expr $LATEST_FULL_BACK_CREATED_TIME + $FULLBACK_INTERVAL` -ge $STARTED_TIME ] 
	then
	echo -e "完全备份$LATEST_FULL_BACK未过期,将根据$LATEST_FULL_BACK名字作为增量备份目录命名"
	echo "        "
	NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACK
	/home/mysqlbak/incr/2016-06-13_15-07-31
	mkdir -p $NEW_INCRDIR
		LATEST_INCR_BACK=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`  #获取最新增量备份目录名
		if [ ! $LATEST_INCR_BACK ] 
		then
			INCRBASEDIR=$FULLBACK_DIR/$LATEST_FULL_BACK      #定义增量基础备份目录名$INCRBASEDIR,还没有做增量目录情况下。
			INCRBASEDIR=/home/mysqlbak/full/2016-06-13_15-07-31
			echo -e "增量备份将以$INCRBASEDIR作为备份基础"
			echo "        "
			else
			INCRBASEDIR=$LATEST_INCR_BACK			 #定义增量基础备份目录名$INCRBASEDIR,已经存在增量目录情况下。
			echo -e "增量备份将以$INCRBASEDIR作为备份基础"
			echo "        "
		fi
	echo "使用$INCRBASEDIR作为基础做新的增量备份."
	$INNOBACKUPEX --defaults-file=$MYSQL_CNF  $MYSQL_CMD --incremental-basedir=$INCRBASEDIR  --incremental $NEW_INCRDIR > $INNOBACK_taillog 2>&1
        if [$? -eq 0  ]
                then
                echo "incremental back sucess."
          else
                echo "incremental back error,please check."
        fi
	
  else
	echo  "*********************************"
	echo -e "Do new full bakup,please wait..."
	echo  "*********************************"
	$INNOBACKUPEX --defaults-file=$MYSQL_CNF   $MYSQL_CMD --compress $FULLBACKUP_DIR > $INNOBACK_taillog 2>&1 
	if [$? -eq 0  ]
		then
		echo "full back sucess."
	  else
		echo "full back error,please check."
	fi
fi


echo -e "find expired file and delete:">>$OPRATE_LOGFILE
for expire_full_file in $(/usr/bin/find $FULLBACK_DIR/ -mtime +7)
do
 if [ -d $expire_full_file ]
	 then
		 rm -rf $expire_full_file
		 echo -e "delete full expired dir:$expire_full_file" >>$OPRATE_LOGFILE
	 elif [ -f $expire_full_file ]
		 then
		 rm -rf $expire_full_file
		 echo -e "delete full expired file:$expire_full_file" >>$OPRATE_LOGFILE
 fi
done

for expire_incr_file in $(/usr/bin/find $INCRBACKUP_DIR/ -mtime +7)
do
 if [ -d $expire_incr_file ]
         then
                 rm -rf $expire_incr_file
                 echo -e "delete incr expired dir:$expire_incr_file" >>$OPRATE_LOGFILE
         elif [ -f $expire_incr_file ]
                 then
                 rm -rf $expire_incr_file
                 echo -e "delete incr  expired file:$expire_incr_file" >>$OPRATE_LOGFILE
 fi
done
tree   $BACK_DIR
echo "Mysql 3306 full and incr backup finished." >>$OPRATE_LOGFILE

后期会进行更为智能和高效方式进行实现。请期待 V 2.0、V3.0 。。。。V N.0.该脚本是之前测试用的,线上的脚本没有拿出来。思想是一样的,只是目录统一了具体变量。可以自己优化。

 

转载请注明:西数超哥博客www.ysidc.top» 基于innobackupex备份工具的mysql全量备份和增量备份shell脚本

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0) 打赏
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥 » 基于innobackupex备份工具的mysql全量备份和增量备份shell脚本

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏