#!/bin/sh
#victory chen
INNOBACKUPEX=innobackupex
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX
TODAY=`date +%Y%m%d%H%M%S`
USEROPTIONS="--user=root --password=toot"
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
rm -rf $FULLBACKUPDIR
fi
if [ -d $INCRBACKUPDIR ];then
rm -rf $INCRBACKUPDIR
fi
if [ ! -d $FULLBACKUPDIR ]; then
mkdir -pv $FULLBACKUPDIR
fi
if [ ! -d $TMPFILEDIR ];then
mkdir -pv $TMPFILEDIR
fi
/usr/bin/innobackupex --defaults-file=$MYCNF --user=root --password=root --host=127.0.0.1 --port=3306 $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`
# 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 $FULLBACKUPDIR
tar -czvf - $Filename | openssl des3 -salt -k 123456 -out $Filename.tar.gz
# tar -zcvf $TODAY.tar.gz ./full/
ssh 192.168.117.130 mkdir /backup/$Filename
scp -P 22 $Filename.tar.gz root@192.168.117.130:/backup/$Filename/$Filename.tar.gz
# rm -rf $FULLBACKUPDIR
if [ $? = 0 ]
then
echo "scp succeed"
# rm -rf $BACKUPDIR/full
else
echo "Error with scp."
fi
echo "completed: `date`"
exit 0