#!/bin/sh# add ling INNOBACKUPEX=innobackupexINNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEXTODAY=`date +%Y%m%d%H%M%S`USEROPTIONS="--user=root --password=root"TMPFILE="/logs/mysql/innobackup_$TODAY.$$.tmp"TMPFILEDIR="/logs/mysql/"MYCNF=/etc/my.cnfMYSQL=/usr/bin/mysqlMYSQLADMIN=/usr/bin/mysqladminBACKUPDIR=/backup # 备份的主目录FULLBACKUPDIR=$BACKUPDIR/full INCRBACKUPDIR=$BACKUPDIR/incr # 增量备份的目录# 全库备份的目录# Grab start time############################################################################## Display error message and exit#############################################################################error(){ echo "$1" 1>&2 exit 1} # Check options before proceedingif [ ! -x $INNOBACKUPEXFULL ]; then error "$INNOBACKUPEXFULL does not exist."fi if [ ! -d $BACKUPDIR ]; then error "Backup destination folder: $BACKUPDIR does not exist."fi if [ -z "`$MYSQLADMIN $USEROPTIONS status | grep 'Uptime'`" ] ; then error "HALTED: MySQL does not appear to be running."fi if ! `echo 'exit' | $MYSQL -s $USEROPTIONS` ; then error "HALTED: Supplied mysql username or password appears to be incorrect (not copied here for security, see script)."fi# Some info outputecho "----------------------------"echoecho "$0: MySQL backup script"echo "started: `date`"echo # Create full and incr backup directories if they not exist.if [ ! -d $FULLBACKUPDIR ]; then mkdir -pv $FULLBACKUPDIRfiif [ ! -d $TMPFILEDIR ];then mkdir -pv $TMPFILEDIRfi# find latest backup fileLATEST_FULL=`find $FULLBACKUPDIR -mindepth 1 -maxdepth 1 -type d -printf "%P\n"`echo "LATEST_FULL=$LATEST_FULL" # TMPINCRDIR 以全局备份的名字作为目录存放增量备份TMPINCRDIR=$INCRBACKUPDIR/$LATEST_FULL #/backup/incr/2018-08-12_23-06-00Fathername=`basename $TMPINCRDIR`mkdir -p $TMPINCRDIRBACKTYPE="incr"# Find latest incremental backup.LATEST_INCR=`find $TMPINCRDIR -mindepth 1 -maxdepth 1 -type d | sort -nr | head -1`echo "LATEST_INCR=$LATEST_INCR" # If this is the first incremental, use the full as base. Otherwise, use the latest incremental as base.if [ ! $LATEST_INCR ] ; then INCRBASEDIR=$FULLBACKUPDIR/$LATEST_FULLelse INCRBASEDIR=$LATEST_INCRfiecho "Running new incremental backup using $INCRBASEDIR as base." innobackupex --defaults-file=$MYCNF $USEROPTIONS --incremental $TMPINCRDIR --incremental-basedir $INCRBASEDIR > $TMPFILE 2>&1 # innobackupex --defaults-file=$MYCNF $USEROPTIONS $FULLBACKUPDIR > $TMPFILE 2>&1 # 如果这里获取这次备份的目录 但是这里使用打包的方法 获取到的不是 #/backup/2018-08-11_23-28-46而是/backup是一个目录 显示的不是文件无法判断文件是否创建成功 所以用下面的方法THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPFILE`if [ $? = 0 ] then echo "THISBACKUP=$THISBACKUP" Filename=`basename $THISBACKUP` #2018-08-13_02-25-18 echo "Filename=$Filename" # rm -f $TMPFILE echo "Databases backed up successfully to: $THISBACKUP"else echo "backup failed"fiif [ -n "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then echo "$INNOBACKUPEX succeed:"else echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"# cat $TMPFILErm -f $TMPFILE exit 1fi cd $TMPINCRDIR tar -czvf - $Filename | openssl des3 -salt -k 123456 -out $TMPINCRDIR/$Filename.tar.gz # tar -zcvf $TODAY.tar.gz ./full/ scp -P 22 $TMPINCRDIR/$Filename.tar.gz root@192.168.117.130:/backup/$Fathername/$Filename.tar.gz # rm -rf $FULLBACKUPDIR if [ $? = 0 ] then echo "scp succeed" # rm -rf $BACKUPDIR/full else echo "Error with scp." fiexit 0复制代码