MySQL 增量备份shell

#!/bin/bash
#set -x
Host=192.168.99.99
Host2=192.168.99.77
name=root
pass=12346
DATE=`date +"%Y%m%d-%H%M%S"`
DELDATE=`date -d -1day +%Y%m%d`
DELDATE2=`date -d -1day +%Y%m%d-%H`
DELDATE3=`date -d -3day +%Y%m%d-%H`
DELDATE4=`date -d -30day +%Y%m%d-%H`
DELDATE5=`date -d -2day +%Y%m%d-%H`
WAN_DIR="/data/wan_dir"
ZENG_DIR="/data/zeng_dir"
DATA_DIR="/data/data"
MYSQL_BIN="/usr/local/mysql/bin"
error_log="$WAN_DIR/backup_error_$DATE.log"
backup_log="$ZENG_DIR/backup_$DATE.log"
gzdumpfile="$DATE.sql.tar.gz"
db="/var/log/backup_$DATE.txt"
PASSWORD="P#6%IEd3qkW"
cd $DATA_DIR
rm -rf /var/log/backup_*.txt
ls -l $DATA_DIR | grep "^d" | awk -F " " '{print $9}' >>$db
find $WAN_DIR -name "*$DELDATE2*.tar.gz"   -exec rm -rf {} \; >>/root/wan_log
find $WAN_DIR -name "*$DELDATE3*.tar.gz" -exec rm -rf {} \; >>/root/wan_log
find $ZENG_DIR -name "*$DELDATE2*.sql.tar.gz"  -exec rm -rf {} \; >>/root/zeng_log
find $WAN_DIR -name "*$DELDATE5*.tar.gz" -exec rm -rf {} \; >>/root/wan_log

function wan() {
#检测完全备份目录是否存在,如果不存在就创建。
if [ -d $WAN_DIR ]
then
    echo "完全备份目录存在" >>$backup_log
else
    echo "完全备份目录不存在,开始创建......."
    /bin/mkdir $WAN_DIR
fi

eMailFile="/root/mail.log"
email=yedaihong@youkia.net
    echo "       " > $eMailFile
    echo "-----------------------" >> $eMailFile
    echo "`date +"%y-%m-%d %H:%M:%S"`" >> $eMailFile
    echo "-------------------------" >> $eMailFile
cd $WAN_DIR
    for dbname in $(cat $db)
    do
       /usr/local/mysql/bin/mysqldump --flush-logs -u$name -p$pass --skip-lock-tables --quick $dbname > $dbname.sql
        if [ $? = 0 ]
        then
             find $ZENG_DIR -name "*.log" -mtime +32 -exec rm {} \; >/dev/null 2>&1
             cd $WAN_DIR
             tar -zcvf $dbname.$gzdumpfile $dbname.sql
             rm -rf ./*.sql
             echo "$Host2 Backup MySQL succeed" >>$eMailFile
             #mail -s "MySQL Backup" $email < $eMailFile
        else
             echo "$Host2 Backup MySQL fail" >>$eMailFile
             mail -s "$Host2 MySQL Backup fail" $email < $eMailFile
        fi
    done
#完全备份后删除本地增量备份文件,只保留最近一个星期的增量备份文件

#将备份好的上传到FTP服务器
cd $WAN_DIR
for db_back in $(cat $db)
do
    ftp -nv $Host 2101 <<EOF
    user youkiaback "$PASSWORD"
    cd wan_dir
    binary
    put $db_back.$gzdumpfile
    mdelete $db_back.$DELDATE3*.tar.gz
    quit
EOF
done
}
function zeng() {
/bin/mkdir /data/zeng_dir

eMailFile="$ZENG_DIR/mail.log"
email=yedaihong@youkia.net

echo "       " > $eMailFile
echo "-----------------------" >> $eMailFile
echo "`date +"%y-%m-%d %H:%M:%S"`" >> $eMailFile
echo "-------------------------" >> $eMailFile

TIME=$(date "-d 10 day ago" +"%Y-%m-%d %H:%M:%S")
#StartTime=$(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")
StartTime=$(date "-d 1 hour ago" +"%Y-%m-%d %H:%M:%S")
Start="--start-datetime"
#删除10天前的二进制文件
mysql -u$name -p$pass -e "purge master logs before ${TIME}" && echo "delete 10 days before log" | tee -a $eMailFile
filename=`cat $DATA_DIR/mysql-bin.index | awk -F "/" '{print $2}'`
cd $ZENG_DIR

for i in $filename
do
     echo "$StartTime start backup binlog" >> $eMailFile

     for db_name in $(cat $db)
     do
        /usr/local/mysql/bin/mysqlbinlog -u$name -p$pass -d $db_name $Start="$StartTime" $DATA_DIR/$i >>$db_name.$DATE.sql
        if [ $? = 0 ]
        then
             tar -zcvf $db_name.$gzdumpfile $db_name.$DATE.sql
             rm -rf ./*.sql
             echo "Backup MySQL succeed" >>$eMailFile
             #mail -s "MySQL Backup" $email < $eMailFile
        else
             echo "$Host2 Backup MySQL fail" >>$eMailFile
             mail -s "$Host2 MySQL Backup fail" $email < $eMailFile
        fi
     done
done
find $ZENG_DIR -name "*.log" -name +32 -exec rm {} \; >/dev/null 2>&1
#删除上次备份的完整备份的文件

#将备份好的上传到FTP服务器
for db_back in $(cat $db)
do
    ftp -nv $Host 2101 <<EOF
    user youkiaback "$PASSWORD"
    cd zeng_dir
    binary
    put $db_back.$gzdumpfile
    mdelete $db_back.$DELDATE3*.tar.gz
    quit
EOF
done
}
backfile=`ls -l /data/wan_dir/*.tar.gz | wc -l`
if [ $backfile != 0 ]
then
    echo "完整备份已经存在,现在进行增量备份"
    sleep 5
    zeng
else
    echo "还没进行完整备份,现在进行完整备份"
    sleep 5
    wan
fi

posted @ 2018-04-13 10:11  带你去土耳其  阅读(253)  评论(0编辑  收藏  举报