Mysql 备份脚本

#!/bin/bash
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin
DATADIR=/data/mydata
BACKUPDIR=/data/dbbackup/`hostname`
BACKHOST=192.168.1.226
MAILBODY=/data/dbbackup/`hostname`/mail
USER=root
TIME=`date +%Y-%m-%d_%H-%M-%S`
INCREMENTAL=/data/dbbackup/localhost.localdomain/incremental
FULLTIME=`ls  /data/dbbackup/localhost.localdomain/|grep "2014-12-11*"`
FULLBACKDIR=/data/dbbackup/localhost.localdomain/$FULLTIME


if [ -z $1 ];then
DATE=`date +%a`
else
DATE=Sun
fi

if [ ! -d "$BACKUPDIR" ]
then
echo "NO BACKUP DIR!" 
exit
fi

>$MAILBODY

echo "`hostname`'s Backup Time is `date +%Y/%m/%d_%H:%M`" >>$MAILBODY
echo "--------------------------------------------------" >>$MAILBODY

full_backup(){
innobackupex  --user=root --password=redhat --host=127.0.0.1  --no-timestamp --databases=jrd --defaults-file=/usr/local/mysql/my.cnf  "$BACKUPDIR/$TIME" && innobackupex --apply-log  "$BACKUPDIR/$TIME"
if [ $? -eq 0 ] 
then
echo "`hostname`'s DB local backup SUCCESS(full)" >>$MAILBODY
LOCALDUMPMD5=`md5sum $BACKUPDIR/$TIME/jrd/*|awk '{print $1}'`
else
echo "WARNING......WARNING......WARNING......" >>$MAILBODY
echo "`hostname`'s DB dump FAILED" >>$MAILBODY
echo "`hostname`'s Backup script TERMINAL" >>$MAILBODY
echo " "  >>$MAILBODY
#!send mail
exit
fi

rsync -az $BACKUPDIR/$TIME $USER@$BACKHOST:$BACKUPDIR/ >/dev/null 2>&1

REMOTEDUMPMD5=`ssh $USER@$BACKHOST md5sum $BACKUPDIR/$TIME/jrd/*|awk '{print $1}'`

if [ "${LOCALDUMPMD5}" == "${REMOTEDUMPMD5}" ]
then
echo "`hostname`'s DB save to remote host SUCCESS" >>$MAILBODY
else
echo "WARNING......WARNING......WARNING......" >>$MAILBODY
echo "`hostname`'s DB save to remote host FAILED" >>$MAILBODY
fi
echo "`hostname`'s DB Backup END">>$MAILBODY
echo "--------------------------------------------------" >>$MAILBODY
}




increase_backup(){
if [ -d  "${INCREMENTAL}" ] 
then
innobackupex --user=root --password=redhat --host=127.0.0.1  --databases=jrd --defaults-file=/usr/local/mysql/my.cnf  --no-timestamp --incremental --incremental-basedir="$FULLBACKDIR" "$INCREMENTAL/$TIME" 
if [ $? -eq 0 ]
then
echo "`hostname`'s DB local backup SUCCESS(increase)" >>$MAILBODY
LOCALBINLOGMD5=`md5sum  $INCREMENTAL/$TIME/jrd/*|awk '{print $1}'`
else
echo "WARNING......WARNING......WARNING......" >>$MAILBODY
echo "`hostname`'s DB dump FAILED" >>$MAILBODY
echo "`hostname`'s Backup script TERMINAL" >>$MAILBODY
echo " "  >>$MAILBODY
#!send mail
exit
fi
fi
rsync -az  $INCREMENTAL/$TIME  $USER@$BACKHOST:"$INCREMENTAL" >/dev/null 2>&1
REMOTEBINLOGMD5=`ssh $USER@$BACKHOST md5sum "$INCREMENTAL/$TIME/jrd/*"|awk '{print $1}'`
if [ "${LOCALBINLOGMD5}" == "${REMOTEBINLOGMD5}" ]
then
echo "`hostname`'s DB-BIN-LOG save to remote host SUCCESS" >>$MAILBODY
else
echo "WARNING......WARNING......WARNING......" >>$MAILBODY
echo "`hostname`'s DB-BIN-LOG save to remote host FAILED" >>$MAILBODY
fi
echo "`hostname`'s DB Backup END">>$MAILBODY
echo "--------------------------------------------------" >>$MAILBODY
}


if [ $DATE == Sun ]
then
full_backup
else
increase_backup
fi
if file $BACKUPDIR/`date -d "30 day ago" +%Y-%m-%d*` >/dev/null 2>&1
then
rm -rf  $BACKUPDIR/`date -d "30 day ago" +%Y-%m-%d*` 
fi

sudo rsync -az $MAILBODY $USER@$BACKHOST:$BACKUPDIR >/dev/null 2>&1

 

posted @ 2015-01-29 15:04  想想宝宝  阅读(251)  评论(0编辑  收藏  举报