在線上MySQL數據庫備份分爲全備和增備,而xtrabackup備份已經支持了增量備份了,可是mysqldump就不支持增量備份,因此咱們須要寫一個shell腳本對於mysqldump來自動全備和增備。mysql
一下腳本要求咱們作一個全備的策略,而後如何作增量備份,自動完成天天執行增量備份,每一個星期天執行全備。備份完刪除二進制文件,減低磁盤壓力。sql
[root@cml5 ~]# cat mysqlback.sh #!/bin/bash ##date=2017-11-7 ##name=cml ##email=********@qq.com ##title=mysqldump backup EMAIL="*******@qq.com" mysqlbak_dir=/usr/local/mysql/mydata mysqlbak_log=/usr/local/mysql/mydata/mysqlbak.log DATE=`date +%Y%m%d` TIME=`date +"%y-%m %H:%M:%S"` DUMPBAK=$DATE.sql GZDUMPBAK=$DATE.sql.gz WEEK=`date +%w` cd $mysqlbak_dir USER=root PASSWD=redhat bin_log=`ls -tr mysql-bin.0*| head -1` bin_log_dir=/usr/local/mysql/mydata/bin_log if [ ! -f $bin_log_dir ];then mkdir$bin_log_dir fi echo "--------------------------$TIME-----------------------------">> $mysqlbak_log if [ $WEEK == 0 ];then ##日期是星期天的時候執行 ##week=0~6 cd$mysqlbak_dir /usr/local/mysql/bin/mysqldump-u$USER -p$PASSWD --single-transaction -R --triggers -E --master-data --flush-logs--all-databases > $DUMPBAK 2>&1 echo"mysqldump backup success!!" >> $mysqlbak_log tar-czvf $GZDUMPBAK $DUMPBAK >> $mysqlbak_log 2>&1 echo"$GZDUMPBAK backup success!!" >> $mysqlbak_log # if[ -f $GZDUMPBAK ];then # echo"$GZDUMPBAK BACKUP SUCCESS!!" |mail -s "mysqldump backup"$EMAIL # fi rm-rf $DUMPBAK rm-rf $bin_log ##上面步驟是全備過程 else cd$mysqlbak_dir /usr/local/mysql/bin/mysqladmin-u$USER -p$PASSWD flush-logs cp-p $bin_log $bin_log_dir echo"copy $bin_log to $bin_log_dir" >> $mysqlbak_log rm-rf $bin_log echo"backup bin_log success!!" >> $mysqlbak_log # echo"backup bin_log $bin_log SUCCESS!" | mail -s "backupbin_log" $EMAIL ##下面是增備過程 fi
##給腳本權限:shell
[root@cml5 ~]# cd /usr/local/src/script/ [root@cml5 script]# ls mysqlback.sh [root@cml5 script]# chmod a+x mysqlback.sh
##把它加入到crontab幾乎任務裏面:數據庫
[root@cml5 script]# crontab -e 1 0 * * * /usr/local/src/script/mysqlback.sh