標籤:基於mysqldump編寫自動全備增備的shell指令碼
基於mysqldump編寫自動全備增備的shell指令碼
線上上MySQLDatabase Backup分為全備和增備,而xtrabackup備份已經支援了增量備份了,但是mysqldump就不支援增量備份,所以我們需要寫一個shell指令碼對於mysqldump來自動全備和增備。
一下指令碼要求我們做一個全備的策略,然後如何做增量備份,自動完成每天執行增量備份,每個星期天執行全備。備份完刪除二進位檔案,減低磁碟壓力。
[[email protected] ~]# 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/mydatamysqlbak_log=/usr/local/mysql/mydata/mysqlbak.logDATE=`date +%Y%m%d`TIME=`date +"%y-%m %H:%M:%S"`DUMPBAK=$DATE.sqlGZDUMPBAK=$DATE.sql.gzWEEK=`date +%w`cd $mysqlbak_dirUSER=rootPASSWD=redhatbin_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_dirfi 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
##給指令碼許可權:
[[email protected] ~]# cd /usr/local/src/script/[[email protected] script]# lsmysqlback.sh[[email protected] script]# chmod a+x mysqlback.sh
##把它加入到crontab幾乎任務裡面:
[[email protected] script]# crontab -e1 0 * * * /usr/local/src/script/mysqlback.sh
本文出自 “第一個legehappy51cto部落格” 部落格,請務必保留此出處http://legehappy.blog.51cto.com/13251607/1980183
基於mysqldump編寫自動全備增備的shell指令碼