#!/bin/sh
# add ling
INNOBACKUPEX=innobackupex
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
TODAY=`date +%Y%m%d%H%M%S`
USEROPTIONS="--user=root --password=root"
TMPFILE="/logs/mysql/innobackup_$TODAY.$$.tmp"
TMPFILEDIR="/logs/mysql/"
MYCNF=/etc/my.cnf
MYSQL=/usr/bin/mysql
MYSQLADMIN=/usr/bin/mysqladmin
BACKUPDIR=/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 proceeding
if [ ! -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 output
echo "----------------------------"
echo
echo "$0: MySQL backup script"
echo "started: `date`"
echo
# Create full and incr backup directories if they not exist.
if [ ! -d $FULLBACKUPDIR ]; then
mkdir -pv $FULLBACKUPDIR
fi
if [ ! -d $TMPFILEDIR ];then
mkdir -pv $TMPFILEDIR
fi
# find latest backup file
LATEST_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-00
Fathername=`basename $TMPINCRDIR`
mkdir -p $TMPINCRDIR
BACKTYPE="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_FULL
else
INCRBASEDIR=$LATEST_INCR
fi
echo "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"
fi
if [ -n "`tail -1 $TMPFILE | grep 'completed OK!'`" ] ; then
echo "$INNOBACKUPEX succeed:"
else
echo "---------- ERROR OUTPUT from $INNOBACKUPEX ----------"
# cat $TMPFILE
rm -f $TMPFILE
exit 1
fi
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."
fi
exit 0
复制代码
mysql xtrabackup自动增量备份shell脚本
最新推荐文章于 2022-12-14 10:09:06 发布