1. 程式人生 > >MySQL自動化(全量+增量)備份指令碼

MySQL自動化(全量+增量)備份指令碼

文章轉自:http://www.it-hack.cn/forum.php?mod=viewthread&tid=220&extra=page%3D1

一、MySQL的日常備份方案:

全備+增量備份:

1、週日凌晨三點進行全備;

2、週一到週日增量備份。

不是往常的週日全備份,週一到週六增量備份,這樣如果週日資料庫在完全備份前出問題,恢復完成後,會少週日一天的資料量,所以七天增量備份,週日全備可以更好的保全資料。

這是備份週期演示表:

Sun 3:00------Mon 3:00-----------------Tue 3:00----------Wed 3:00----------Thu 3:00----------Fri 3:00----------Sat 3:00----------Sun 3:00
(flush)Sun full---(flush)Sun->Mon binlog---(flush)Mon->Tue---(flush)Tue->Wed---(flush)Wed->Thu---(flush)Thu->Fri---(flush)Fri->Sat---(flush)Sun full---(flush)Sun->Mon binlog---(flush)Mon->Tue---(flush)Tue->Wed---(flush)Wed->Thu---(flush)Thu->Fri---(flush)Fri->Sat---(flush)Sun full

二、備份指令碼:

模組化定製,可以隨意移動,調節備份策略!

變數欄的帳號密碼,檔案路徑根據自己實際環境可以進行修改,自由度比較高,模組函式全變數,適用度較高,但是可能還有不完善的地方

vim /root/mysql_bakup.sh

#!/bin/bash
#Date:2017/5/2
#Author:wangpengtai
#Blog:http://wangpengtai.blog.51cto.com
#At Sunday, we will backup the completed databases and the incresed binary log during Saturday to Sunday.
#In other weekdays, we only backup the increaing binary log at that day!
################################
#the globle variables for MySQL#
################################
DB_USER='root'
DB_PASSWORD='123456'
DB_PORT='3306'
BACKUPDIR='/tmp/mysqlbakup'
BACKUPDIR_OLDER='/tmp/mysqlbakup_older'
DB_PID='/data/mysql/log/mysqld.pid'
DB_SOCK='/data/mysql/log/mysql.sock'
LOG_DIR='/data/mysql/log'
BACKUP_LOG='/tmp/mysqlbakup/backup.log'
DB_BIN='/usr/local/mysql/bin'
#time variables for completed backup
FULL_BAKDAY='Sunday'
TODAY=`date +%A`
DATE=`date +%Y%m%d`
###########################
#time variables for binlog#
###########################
#liftcycle for saving binlog
DELETE_OLDLOG_TIME=(

date"d14dayago"+ (date"−d14dayago"+(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")

#BINLOG_LIST=`cat /data/mysql/log/mysql-bin.index`
BINLOG_INDEX='/data/mysql/log/mysql-bin.index'
##############################################
#Judge the mysql process is running or not. #
#mysql stop return 1, mysql running return 0.#
##############################################
function DB_RUN(){
if test -a DB_PID && test -a
DB_PID && test -a
DB_SOCK;then
return 0
else
return 1
fi
}
###################################################################################################
#Judge the bacup directory is exsit not. #
#If the mysqlbakup directory was exsited, there willed return 0. #
# If there is no a mysqlbakup directory, the fuction will create the directory and return value 1.#
###################################################################################################
function BACKDIR_EXSIT(){
if test -d BACKUPDIR;then # echo "BACKUPDIR;then # echo "BACKUPDIR was exist."
return 0
else
echo "BACKUPDIRisnotexist,nowcreateit."mkdirpv BACKUPDIRisnotexist,nowcreateit."mkdir−pvBACKUPDIR
return 1
fi
}
###################################################################################################
#Judge the binlog is configed or not. #
#If the mysqlbakup directory was exsited, there willed return 0. #
# If there is no a mysqlbakup directory, the fuction will create the directory and return value 1.#
###################################################################################################
function BINLOG_EXSIT(){
if test -f BINLOG_INDEX;then # echo "BINLOG_INDEX;then # echo "BACKUPDIR was exist."
return 0
fi
}
###################################################
#The full backup for all Databases #
#This function is use to backup the all databases.#
###################################################
function FULL_BAKUP(){
echo "At `date +%D\ %T`: Starting full backup the MySQL DB ... "
# rm -fr BACKUPDIR/db_fullbak_BACKUPDIR/db_fullbak_DATE.sql #for test !!
DB B IN/mysqldumplockalltablesflushlogsmasterdata=2u DBBIN/mysqldump−−lock−all−tables−−flush−logs−−master−data=2−uDB_USER -pDB P ASSWORDP DBPASSWORD−PDB_PORT -A |gzip > BACKUPDIR/db_fullbak_BACKUPDIR/db_fullbak_DATE.sql.gz
FULL_HEALTH=`echo ?if[[ ?‘if[[FULL_HEALTH == 0 ]];then
echo "At `date +%D\ %T`: MySQL DB incresed backup successfully"
else
echo "MySQL DB full backup failed!"
fi
}
#python
# >>> with open('/data/mysql/log/mysql-bin.index','r') as obj:
# ... for i in obj:
# ... print os.path.basename(i)
# ...
# mysql-bin.000006
# mysql-bin.000007
# mysql-bin.000008
# mysql-bin.000009
function INCREASE_BAKUP(){
echo "At `date +%D\ %T`: Starting increased backup the MySQL DB ... "
DB B IN/mysqladminu DBBIN/mysqladmin−uDB_USER -pDB P ASSWORDP DBPASSWORD−PDB_PORT flush-logs
DB B IN/mysqlu DBBIN/mysql−uDB_USER -pDB P ASSWORDP DBPASSWORD−PDB_PORT -e "purge master logs before DELETE O LDLOG T IME"foriincat DELETEOLDLOGTIME"foriin‘catBINLOG_INDEX`
do
DB B IN/mysqlbinlogu DBBIN/mysqlbinlog−uDB_USER -pDB P ASSWORDP DBPASSWORD−PDB_PORT --start-datetime="START B ACKUPBINLOG T IMEPOINT" STARTBACKUPBINLOGTIMEPOINT"i |gzip >> BACKUPDIR/db_daily_BACKUPDIR/db_daily_DATE.sql.gz
done
# DB B IN/mysqlbinlogu DBBIN/mysqlbinlog−uDB_USER -pDB P ASSWORDP DBPASSWORD−PDB_PORT --start-datetime="START B ACKUPBINLOG T IME" STARTBACKUPBINLOGTIME"LOG_DIR/mysql-bin.[0-9]* |gzip >> BACKUPDIR/db_daily_BACKUPDIR/db_daily_DATE.sql.gz
INCREASE_HEALTH=`echo ?if[[ ?‘if[[INCREASE_HEALTH == 0 ]];then
echo "At `date +%D\ %T`: MySQL DB incresed backup successfully"
else
echo "MySQL DB incresed backup failed!"
fi
}
function OLDER_BACKDIR_EXSIT(){
if test -d BACKUPDIR_OLDER;then # echo "BACKUPDIR_OLDER;then # echo "BACKUPDIR_OLDER was exist."
return 0
else
echo "BACKUPDIR O LDERisnotexist,nowcreateit."mkdirpv BACKUPDIROLDERisnotexist,nowcreateit."mkdir−pvBACKUPDIR_OLDER
# return 1
fi
}
function BAKUP_CLEANER(){
#move the backuped file that created time out of 7 days to the BACKUPDIR_OLDER directory
returnkey=`find BACKUPDIRname".sql.gz"mtime+7execlslhreturnkey o ld=find BACKUPDIR−name"∗.sql.gz"−mtime+7−execls−lh‘returnkeyold=‘findBACKUPDIR_OLDER -name "*.sql.gz" -mtime +14 -exec ls -lh {} \;`
if [[ returnkey!= ′′ ]];thenecho"