1.以下是我最近完成的一个xtrabackup用于备份mysql数据库的脚本
#!/bin/bash
source /etc/profile
#定时任务可以设置成周日执行start_full全备份,然后周一至周六执行start_add增量备,每小时执行start_diff差异备份
PRG="$0"
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`/"$link"
fi
done
BASEDIR=`dirname "$PRG"`
####################################################
#授权xtrabackup用户权限如下:
#GRANT SELECT, RELOAD, SUPER, REPLICATION CLIENT ON *.* TO 'xtrabackup'@'localhost' IDENTIFIED BY '密码'
DB_USER=xtrabackup
#数据库密码文件
DB_PASSWD=`cat $BASEDIR/.passwd`
#要备份的本地数据库的端口号,配置文件以及SOCK文件
DB_PORT=32266
DB_SOCKET=/tmp/mysql.sock
DB_DEF_FILE=/etc/my.cnf
#数据库全备份保存路径
DB_BACKUP_DIR_FULL=/home/db_backup_full
#数据库增量备保存路径
DB_BACKUP_DIR_ADD=/home/db_backup_add
#数据小时增量备份保存路径
DB_BACKUP_DIR_DIFF=/home/db_backup_diff/$(date +%F)
#备份生成的日志名称
LOG_FILE=backup.log
#数据库全备的头文件名
DB_BACKUP_NAME_FULL=mysql_full
DB_BACKUP_NAME_ADD=mysql_add
TIME=$(date +%F)
TIME3=$(date -d -3day +%F)
COMM=innobackupex-1.5.1
#我这里用的是mailx,具体路径根据自己的环境调整
COMM_MAILX=/usr/src/mailx-12.4/mailx
#邮件的消息内容
MAILX_TITLE_SUCCESS='服务器数据库备份成功提醒'
MAILX_TITLE_FAILED='服务器数据库备份异常'
MAILX_TITLE_ADD_SUCCESS='数据库增量备成功提醒'
MAILX_TITLE_DIFF_SUCCESS='数据库差异备成功提醒'
MAILX_CONTENTS_ADD='数据库增量备成功,数据大小为'
MAILX_CONTENTS_DIFF='数据库差异备成功,数据大小为'
MAILX_TITLE_ADD_FAILES='数据库增量备异常提醒'
MAILX_TITLE_DIFF_FAILES='数据库差异备异常提醒'
MAILX_ADD_LSN_NO_EXSIT='增量备份的LSN没有提取出来,或者不存在'
MAILX_ADD_LSN_NULL='增量备份的LSN为空,无法进行后续备份'
MAILX_FULL_LSN_NO_EXSIT='全备份的LSN没有提取出来,或者不存在'
MAILX_FULL_LSN_NULL='全备份的LSN为空,无法进行后续备份'
#邮件联系人列表,每行一个联系人
MAILX_CONTACTS=`cat $BASEDIR/contacts_list`
#用于获取当前小时时间
TIME_HOURS=`echo $(date +%H)`
#指定程序可以运行的时间点,例如:凌晨两点02,下午3点就写15,这样的格式来写
APPOINT_HOURS=*
APPOINT_HOURS_ADD=*
#对端远程服务器的登录用户,IP,端口号,以及用于存放备份文件的路径
SSH_USER=root
SSH_IP=192.168.10.200
SSH_PORT=2222
SSH_DIR=/mysql_backup_100
start_full () {
if [[ $UID -ne 0 ]];then
echo "请以root身份执行该程序."
exit 1
else
if [[ $TIME_HOURS != $APPOINT_HOURS ]];then
echo "程序的执行时间不对,请确认时间后重新执行."
exit 1
else
if [[ -e $DB_BACKUP_DIR_FULL ]];then
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --stream=tar $DB_BACKUP_DIR_FULL 2>>$DB_BACKUP_DIR_FULL/$LOG_FILE |gzip >$DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz
if [[ $? -eq 0 ]];then
BACKUP_FULL_STATUS_OK=`tail -1 $DB_BACKUP_DIR_FULL/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_FULL_STATUS_OK = 'OK!' ]];then
DB_BACKUP_SIZE=`du -sh $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz|awk '{print $1}'`
cd $DB_BACKUP_DIR_FULL
tar -izxvf $DB_BACKUP_NAME_FULL.$TIME.tar.gz xtrabackup_checkpoints
cat xtrabackup_checkpoints |grep "to_lsn"|awk '{print $3}' >./lsn.txt
mv xtrabackup_checkpoints xtrabackup_checkpoints.$TIME
echo "数据库备份成功,当前数据大小为$DB_BACKUP_SIZE."|$COMM_MAILX -s "$MAILX_TITLE_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库全备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
echo "mysql数据库全备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_FAILED" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,数据库备份失败,异常原因退出,请登录服务器查看" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
echo "数据库备份失败,异常原因退出,请登录服务器查看"|$COMM_MAILX -s "$MAILX_TITLE_FAILED" $MAILX_CONTACTS
exit 2
fi
echo "$(date)|数据开始转移至远程服务器" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
ssh -p $SSH_PORT -o GSSAPIAuthentication=no $SSH_USER@$SSH_IP "if [[ -e '$SSH_DIR' ]];then echo "检测远程上传目录存在,将继续进行如下操作";else mkdir '$SSH_DIR';fi" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
scp -P $SSH_PORT -o GSSAPIAuthentication=no $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz $SSH_USER@$SSH_IP:$SSH_DIR
if [[ $? -eq 0 ]];then
echo "$(date)|数据库上传至远程服务器" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
/bin/rm -rf $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz &
echo "$(date)|删除临时备份文件" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
exit 0
else
echo "数据库转移失败"|$COMM_MAILX -s "数据库转移失败" $MAILX_CONTACTS
echo "$(date)|数据库转移失败" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
exit 2
fi
else
mkdir -p $DB_BACKUP_DIR_FULL
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --stream=tar $DB_BACKUP_DIR_FULL 2>$DB_BACKUP_DIR_FULL/$LOG_FILE |gzip >$DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz
if [[ $? -eq 0 ]];then
BACKUP_FULL_STATUS_OK=`tail -1 $DB_BACKUP_DIR_FULL/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_FULL_STATUS_OK = 'OK!' ]];then
DB_BACKUP_SIZE=`du -sh $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz|awk '{print $1}'`
cd $DB_BACKUP_DIR_FULL
tar -izxvf $DB_BACKUP_NAME_FULL.$TIME.tar.gz xtrabackup_checkpoints
cat xtrabackup_checkpoints |grep "to_lsn"|awk '{print $3}' >./lsn.txt
mv xtrabackup_checkpoints xtrabackup_checkpoints.$TIME
echo "数据库备份成功,当前数据大小为$DB_BACKUP_SIZE."|$COMM_MAILX -s "$MAILX_TITLE_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库全备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
echo "mysql数据库全备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_FAILED" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,数据库备份失败,异常原因退出,请登录服务器查看" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
echo "数据库备份失败,异常原因退出,请登录服务器查看"|$COMM_MAILX -s "$MAILX_TITLE_FAILED" $MAILX_CONTACTS
exit 2
fi
echo "$(date)|数据开始转移至远程服务器" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
ssh -p $SSH_PORT -o GSSAPIAuthentication=no $SSH_USER@$SSH_IP "if [[ -e '$SSH_DIR' ]];then echo "检测远程上传目录存在,将继续进行如下操作";else mkdir '$SSH_DIR';fi" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
scp -P $SSH_PORT -o GSSAPIAuthentication=no $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz $SSH_USER@$SSH_IP:$SSH_DIR
if [[ $? -eq 0 ]];then
echo "$(date)|数据库上传至远程服务器" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
/bin/rm -rf $DB_BACKUP_DIR_FULL/$DB_BACKUP_NAME_FULL.$TIME.tar.gz &
echo "$(date)|删除临时备份文件" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
exit 0
else
echo "数据库转移失败"|$COMM_MAILX -s "数据库转移失败" $MAILX_CONTACTS
echo "$(date)|数据库转移失败" >>$DB_BACKUP_DIR_FULL/$LOG_FILE.$TIME
exit 2
fi
fi
fi
fi
}
start_add () {
if [[ $UID -ne 0 ]];then
echo "请以root身份执行该程序."
exit 2
else
if [[ $TIME_HOURS != $APPOINT_HOURS_ADD ]];then
echo "程序的执行时间不对,请确认时间后重新执行."
exit 2
else
if [[ -e $DB_BACKUP_DIR_ADD ]];then
if [[ -e $DB_BACKUP_DIR_ADD/lsn.txt ]];then
if [[ -s $DB_BACKUP_DIR_ADD/lsn.txt ]];then
WC_LSN_COUNTS=`wc -l $DB_BACKUP_DIR_ADD/lsn.txt|awk '{print $1}'`
if [[ $WC_LSN_COUNTS -ge 1 ]]&&[[ $WC_LSN_COUNTS -le 4 ]];then
LSN=`tail -1 $DB_BACKUP_DIR_ADD/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN $DB_BACKUP_DIR_ADD 2>>$DB_BACKUP_DIR_ADD/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_ADD_STATUS_OK=`tail -1 $DB_BACKUP_DIR_ADD/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_ADD_STATUS_OK = 'OK!' ]];then
find $DB_BACKUP_DIR_ADD -type f -mmin -1 -name "xtrabackup_checkpoints" -exec cat {} \;|grep to_lsn|awk '{print $3}' >>$DB_BACKUP_DIR_ADD/lsn.txt
DATA_ADD_SIZE=`find $DB_BACKUP_DIR_ADD -type d -mmin -1 -name "$(date +%F_*)" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_ADD,$DATA_ADD_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_ADD_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
elif [[ $WC_LSN_COUNTS -eq 5 ]];then
LSN=`tail -1 $DB_BACKUP_DIR_ADD/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN $DB_BACKUP_DIR_ADD 2>>$DB_BACKUP_DIR_ADD/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_ADD_STATUS_OK=`tail -1 $DB_BACKUP_DIR_ADD/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_ADD_STATUS_OK = 'OK!' ]];then
mv $DB_BACKUP_DIR_ADD/lsn.txt $DB_BACKUP_DIR_ADD/lsn_backup.$TIME
DATA_ADD_SIZE=`find $DB_BACKUP_DIR_ADD -type d -mmin -1 -name "$(date +%F_*)" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_ADD,$DATA_ADD_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_ADD_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
echo "未定义" 2>>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
exit 2
fi
else
#增量备份的LSN为空,无法进行后续备份
echo "$(date)|$MAILX_ADD_LSN_NULL" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_ADD_LSN_NULL"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
if [[ -e $DB_BACKUP_DIR_FULL/lsn.txt ]];then
if [[ -s $DB_BACKUP_DIR_FULL/lsn.txt ]];then
LSN=`tail -1 $DB_BACKUP_DIR_FULL/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN $DB_BACKUP_DIR_ADD 2>>$DB_BACKUP_DIR_ADD/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_ADD_STATUS_OK=`tail -1 $DB_BACKUP_DIR_ADD/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_ADD_STATUS_OK = 'OK!' ]];then
find $DB_BACKUP_DIR_ADD -type f -mmin -1 -name "xtrabackup_checkpoints" -exec cat {} \;|grep to_lsn|awk '{print $3}' >>$DB_BACKUP_DIR_ADD/lsn.txt
DATA_ADD_SIZE=`find $DB_BACKUP_DIR_ADD -type d -mmin -1 -name "$(date +%F_*)" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_ADD,$DATA_ADD_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_ADD_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN为空,无法进行后续备份
echo "$(date)|$MAILX_FULL_LSN_NULL" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NULL"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN文件存在或者未提取出来,退出执行
echo "$(date)|$MAILX_FULL_LSN_NO_EXSIT" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NO_EXSIT"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
fi
else
mkdir -p $DB_BACKUP_DIR_ADD
if [[ -e $DB_BACKUP_DIR_FULL/lsn.txt ]];then
if [[ -s $DB_BACKUP_DIR_FULL/lsn.txt ]];then
LSN=`cat $DB_BACKUP_DIR_FULL/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN $DB_BACKUP_DIR_ADD 2>>$DB_BACKUP_DIR_ADD/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_ADD_STATUS_OK=`tail -1 $DB_BACKUP_DIR_ADD/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_ADD_STATUS_OK = 'OK!' ]];then
find $DB_BACKUP_DIR_ADD -type f -mmin -1 -name "xtrabackup_checkpoints" -exec cat {} \;|grep to_lsn|awk '{print $3}' >>$DB_BACKUP_DIR_ADD/lsn.txt
DATA_ADD_SIZE=`find $DB_BACKUP_DIR_ADD -type d -mmin -1 -name "$(date +%F_*)" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_ADD,$DATA_ADD_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_ADD_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库增量备失败,请具体查看失败原因" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库增量备失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN为空,无法进行后续备份
echo "$(date)|$MAILX_FULL_LSN_NULL" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NULL"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN文件存在或者未提取出来,退出执行
echo "$(date)|$MAILX_FULL_LSN_NO_EXSIT" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NO_EXSIT"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
fi
fi
fi
}
start_diff () {
if [[ $UID -ne 0 ]];then
echo "请以root身份执行该程序."
exit 2
else
if [[ -e $DB_BACKUP_DIR_DIFF ]];then
if [[ -e $DB_BACKUP_DIR_FULL/lsn.txt ]];then
if [[ -s $DB_BACKUP_DIR_FULL/lsn.txt ]];then
LSN=`tail -1 $DB_BACKUP_DIR_FULL/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN --stream=xbstream $DB_BACKUP_DIR_DIFF >$DB_BACKUP_DIR_DIFF/diff_$(date +%F:%H:%M:%S).xbstream 2>>$DB_BACKUP_DIR_DIFF/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_STATUS_OK=`tail -1 $DB_BACKUP_DIR_DIFF/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_STATUS_OK = 'OK!' ]];then
DATA_DIFF_SIZE=`find $DB_BACKUP_DIR_DIFF -type f -mmin -1 -name "diff_$(date +%F):*" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_DIFF,$DATA_DIFF_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库差异备份失败,请具体查看失败原因" >>$DB_BACKUP_DIR_DIFF/$LOG_FILE.$TIME
echo "mysql数据库差异备份失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_FAILED" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库差异备份失败,请具体查看失败原因" >>$DB_BACKUP_DIR_DIFF/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库差异备份失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_FAILED" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN为空,无法进行后续备份
echo "$(date)|$MAILX_FULL_LSN_NULL" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NULL"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN文件存在或者未提取出来,退出执行
echo "$(date)|$MAILX_FULL_LSN_NO_EXSIT" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NO_EXSIT"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
mkdir -p $DB_BACKUP_DIR_DIFF
if [[ -e $DB_BACKUP_DIR_FULL/lsn.txt ]];then
if [[ -s $DB_BACKUP_DIR_FULL/lsn.txt ]];then
LSN=`tail -1 $DB_BACKUP_DIR_FULL/lsn.txt`
$COMM --user=$DB_USER --password=$DB_PASSWD --port=$DB_PORT --socket=$DB_SOCKET --defaults-file=$DB_DEF_FILE --incremental --incremental-lsn=$LSN --stream=xbstream $DB_BACKUP_DIR_DIFF >$DB_BACKUP_DIR_DIFF/diff_$(date +%F:%H:%M:%S).xbstream 2>>$DB_BACKUP_DIR_DIFF/$LOG_FILE
if [[ $? -eq 0 ]];then
BACKUP_STATUS_OK=`tail -1 $DB_BACKUP_DIR_DIFF/$LOG_FILE|awk '{print $5}'`
if [[ $BACKUP_STATUS_OK = 'OK!' ]];then
DATA_DIFF_SIZE=`find $DB_BACKUP_DIR_DIFF -type f -mmin -1 -name "diff_$(date +%F):*" -exec du -sh {} \;|awk '{print $1}'`
echo "$MAILX_CONTENTS_DIFF,$DATA_DIFF_SIZE"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_SUCCESS" $MAILX_CONTACTS
else
echo "$(date)|mysql数据库差异备份失败,请具体查看失败原因" >>$DB_BACKUP_DIR_DIFF/$LOG_FILE.$TIME
echo "mysql数据库差异备份失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_FAILED" $MAILX_CONTACTS
exit 2
fi
else
echo "$(date)|程序异常退出,mysql数据库差异备份失败,请具体查看失败原因" >>$DB_BACKUP_DIR_DIFF/$LOG_FILE.$TIME
echo "程序异常退出,mysql数据库差异备份失败,请具体查看失败原因"|$COMM_MAILX -s "$MAILX_TITLE_DIFF_FAILED" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN为空,无法进行后续备份
echo "$(date)|$MAILX_FULL_LSN_NULL" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NULL"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
else
#全备份的LSN文件存在或者未提取出来,退出执行
echo "$(date)|$MAILX_FULL_LSN_NO_EXSIT" >>$DB_BACKUP_DIR_ADD/$LOG_FILE.$TIME
echo "$MAILX_FULL_LSN_NO_EXSIT"|$COMM_MAILX -s "$MAILX_TITLE_ADD_FAILES" $MAILX_CONTACTS
exit 2
fi
fi
fi
}
case $1 in
start_full)
start_full &
;;
start_add)
start_add &
;;
start_diff)
start_diff &
;;
*)
echo "USAGE $0 start_full|start_add|start_diff的方式启动全备份和增量备份差异备份,不能同时进行"
esac
以上是脚本的内容,将此写入到xtrabackup.sh的文件中,然后赋予执行权限chmod +x xtrabackup.sh,放置到一个目录
2.然后在脚本的目录里面还要创建.passwd和contacts_list两个文件,用于存放数据库的密码和邮件联系人名单
3.全部完成以后即可执行测试
例如:我的环境是/home/xtrabackup/xtrabackup.sh
全备份:/home/xtrabackup/xtrabackup.sh start_full
增量备:/home/xtrabackup/xtrabackup.sh start_add
差异备:/home/xtrabackup/xtrabackup.sh start_diff
本文出自 “潜力股” 博客,转载请与作者联系!