Mysql脚本实现增量备份
因为线上数据库有点大,磁盘又没有做个RAID,而且服务器紧缺。本来想用xtrabackup去做增量备份的,但是各种错误,我已经对它失去信心了,遂,自己写一个脚本去实现。
脚本分两部分,一部分是Full backup,这没什么好介绍的,就是全备嘛。另外一部分是Incremental backup,增量备份方面是以天为最少单位去执行的,每一天增量的内容都独立一个sql文件,当然也可以修改一下,追加到同一个文件去,但是我觉得还是分开比较好一点。
脚本刚写出来,简单测试过没问题,有兴趣的同学可以帮忙测试一下,欢迎报告错误,报BUG邮箱:[email protected]
任务计划:
0 3 * * 5 /bin/bash /var/scripts/full-backup.sh &> /dev/null 0 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=root DBpw=123456 MysqlBin=/usr/bin/mysql MysqlDumpBin=/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 postion sed -n ‘22p‘ $BACK_DIR-$Date/fullbackup.sql | awk -F"‘|,|=|;" ‘{print $3,$6}‘ > $FILE_DIR/FullBackPostion ## Check backup file if [ ! -s $BACK_DIR-$Date/fullbackup.sql ]; then echo "mysqldump backup file faild , please check" | tee -a /var/scripts/full-backup.log exit 1 fi
增量备份:
#!/bin/bash ################################################################# # Mysql Backup Shell Script Part 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/mysql MysqlBinLog=/usr/bin/mysqlbinlog User=root DBpw=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 postion LastStartBinLog=`sed -n ‘2p‘ $BackupDir/Postion-$OldDate` LastStartPos=`sed -n ‘3p‘ $BackupDir/Postion-$OldDate` # Get after the first backup binlog and postion AfterStartBinLog=`sed -n ‘2p‘ $BackupDir/Postion-$Date` AfterStartPos=`sed -n ‘3p‘ $BackupDir/Postion-$Date` # Check whether the binlog switch if [ "$LastStartBinLog" = "$AfterStartBinLog" ];then $MysqlBinLog --start-position=$LastStartPos --stop-position=$AfterStartPos $MysqlBaseDir/$LastStartBinLog >> $BackupDir/inc_backup_$Date.sql else StartLine=`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 done fi else ## Get New Postion $MysqlBin -u$User -p"$DBpw" -e "show master status\G"|awk ‘{print $2}‘ > $BackupDir/Postion-$Date ## Get Stop Postion And binlog StopBinLog=`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.sql else StartLine=`awk "/$StartBinLog/{print NR}" "$MysqlBaseDir"mysql-bin.index` StopLine=`wc -l $MysqlBaseDir/mysql-bin.index |awk ‘{print $1}‘` for i in `seq $StartLine $StopLine` do 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 ;; *) echo -e "$Date\n$StartBinLog to $StopBinLog have other values" >> /var/scripts/MyBackup.log esac done fi fi ############################ packing ################################ cd $BackupDir if [ -s inc_backup_$Date.sql ];then gzip inc_backup_$Date.sql else echo "$Date backup fail" >> /var/scripts/inc-backup.log fi
本文出自 “HQY's 运维人生” 博客,请务必保留此出处http://ldhqy.blog.51cto.com/2788106/1557127
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。