標籤:mysql指令碼 資料庫 增量備份 備份指令碼
因為線上資料庫有點大,磁碟又沒有做個RAID,而且伺服器緊缺。本來想用xtrabackup去做增量備份的,但是各種錯誤,我已經對它失去信心了,遂,自己寫一個指令碼去實現。
指令碼分兩部分,一部分是Full backup,這沒什麼好介紹的,就是全備嘛。另外一部分是Incremental backup,增量備份方面是以天為最少單位去執行的,每一天增量的內容都獨立一個sql檔案,當然也可以修改一下,追加到同一個檔案去,但是我覺得還是分開比較好一點。
指令碼剛寫出來,簡單測試過沒問題,有興趣的同學可以幫忙測試一下,歡迎報告錯誤,報BUG郵箱:[email protected]
任務計劃:
0 3 * * 5 /bin/bash /var/scripts/full-backup.sh &> /dev/null0 3 * * 1-4,6,7 /bin/bash /var/scripts/inc-backup.sh &> /dev/null
增量指令碼:
#!/bin/bash################################################################## Mysql Backup Shell Script Part 1 (Full backup)##Completed in sep 2014 22##Author: Colin Wong##Email: [email protected]######################################################################### BASE_INFO #######Date=`date +%Y%m%d`BACK_DIR="/usr/backup/full-backup"FILE_DIR="/usr/backup"User=rootDBpw=123456MysqlBin=/usr/bin/mysqlMysqlDumpBin=/usr/bin/mysqldump## Check whether Dir exists[[ -d $BACK_DIR-$Date ]] || mkdir -p $BACK_DIR-$Date## Start backup$MysqlDumpBin -u$User -p"$DBpw" --opt --triggers --routines --events --ignore-table=mysql.events --flush-logs --hex-blob --master-data=1 --all-databases > $BACK_DIR-$Date/fullbackup.sql ## Get postionsed -n ‘22p‘ $BACK_DIR-$Date/fullbackup.sql | awk -F"‘|,|=|;" ‘{print $3,$6}‘ > $FILE_DIR/FullBackPostion ## Check backup fileif [ ! -s $BACK_DIR-$Date/fullbackup.sql ]; then echo "mysqldump backup file faild , please check" | tee -a /var/scripts/full-backup.log exit 1fi
增量備份:
#!/bin/bash################################################################## Mysql Backup Shell ScriptPart 2 (Incremental backup)##Completed in sep 2014 22##Author: Colin Wong##Email: [email protected]############################################################################# Increment Backup ##############Date=`date +%Y%m%d`OldDate=`date --date=‘1 days ago‘ +%Y%m%d`MysqlBaseDir="/var/lib/mysql/"BackupDir="/usr/backup"StartBinLog=`awk ‘{print $1}‘ $BackupDir/FullBackPostion`StartPostion=`awk ‘{print $2}‘ $BackupDir/FullBackPostion`MysqlBin=/usr/bin/mysqlMysqlBinLog=/usr/bin/mysqlbinlogUser=rootDBpw=tianqu## Starting...if [ -s $BackupDir/Postion-$OldDate ];then# Get Master Info$MysqlBin -u$User -p"$DBpw" -e "show master status\G"|awk ‘{print $2}‘ > $BackupDir/Postion-$Date# Get the first backup binlog and postionLastStartBinLog=`sed -n ‘2p‘ $BackupDir/Postion-$OldDate`LastStartPos=`sed -n ‘3p‘ $BackupDir/Postion-$OldDate`# Get after the first backup binlog and postionAfterStartBinLog=`sed -n ‘2p‘ $BackupDir/Postion-$Date`AfterStartPos=`sed -n ‘3p‘ $BackupDir/Postion-$Date`# Check whether the binlog switchif [ "$LastStartBinLog" = "$AfterStartBinLog" ];then$MysqlBinLog --start-position=$LastStartPos --stop-position=$AfterStartPos $MysqlBaseDir/$LastStartBinLog >> $BackupDir/inc_backup_$Date.sqlelseStartLine=`awk "/$LastStartBinLog/{print NR}" "$MysqlBaseDir"mysql-bin.index` StopLine=`wc -l $MysqlBaseDir/mysql-bin.index |awk ‘{print $1}‘` for i in `seq $StartLine $StopLine` do# Get binlog filename from line i BinLogFile=`sed -n "$i"p "$MysqlBaseDir"mysql-bin.index |sed ‘s/.\///g‘` case "$BinLogFile" in "$StartBinLog") $MysqlBinLog --start-position=$StartPostion $MysqlBaseDir$BinLogFile >> $BackupDir/inc_backup_$Date.sql ;; "$StopBinLog") $MysqlBinLog --stop-position=$StopPostion $MysqlBaseDir$BinLogFile >> $BackupDir/inc_backup_$Date.sql ;; *)# Normally, program will not perform here echo -e "$Date\n$StartBinLog to $StopBinLog have other values" >> /var/log/MyBackup.log esac donefielse## Get New Postion$MysqlBin -u$User -p"$DBpw" -e "show master status\G"|awk ‘{print $2}‘ > $BackupDir/Postion-$Date## Get Stop Postion And binlogStopBinLog=`sed -n ‘2p‘ $BackupDir/Postion-$Date`StopPostion=`sed -n ‘3p‘ $BackupDir/Postion-$Date`##if [ "$StartBinLog" = "$StopBinLog" ]; then$MysqlBinLog --start-position=$StartPostion --stop-position=$StopPostion $MysqlBaseDir/$StartBinLog >> $BackupDir/inc_backup_$Date.sqlelseStartLine=`awk "/$StartBinLog/{print NR}" "$MysqlBaseDir"mysql-bin.index`StopLine=`wc -l $MysqlBaseDir/mysql-bin.index |awk ‘{print $1}‘`for i in `seq $StartLine $StopLine`doBinLogFile=`sed -n "$i"p "$MysqlBaseDir"mysql-bin.index |sed ‘s/.\///g‘`case "$BinLogFile" in"$StartBinLog")$MysqlBinLog --start-position=$StartPostion $MysqlBaseDir$BinLogFile >> $BackupDir/inc_backup_$Date.sql;;"$StopBinLog")$MysqlBinLog --stop-position=$StopPostion $MysqlBaseDir$BinLogFile >> $BackupDir/inc_backup_$Date.sql;;*)echo -e "$Date\n$StartBinLog to $StopBinLog have other values" >> /var/scripts/MyBackup.logesacdonefifi############################ packing ################################cd $BackupDirif [ -s inc_backup_$Date.sql ];thengzip inc_backup_$Date.sqlelse echo "$Date backup fail" >> /var/scripts/inc-backup.logfi
本文出自 “HQY's 營運人生” 部落格,請務必保留此出處http://ldhqy.blog.51cto.com/2788106/1557127
Mysql指令碼實現增量備份