工作原理就是把每次完整备份的binlog 和postion取出,做增量备份的时候取当时的binlog和postion,使用mysqlbinlog 把这之间的数据读出到另一个文件。
注:本文中可能有个别地方会有些纰漏,请大家自行调试,因为下面的脚本是我用在真实环境中copy过来的,只是临时改了里面个别的参数,例如文件夹位置,密码等。。。。如有问题敬请谅解
一、完整备份
vi full_mysqldump.sh
#!/bin/sh
###########
#########
#########DIR_INFO############
date=`date +%Y%m%d`
BACK_DIR="/home/profiles/liuhaifeng/fullbackup"
FILE_DIR="/home/profiles/liuhaifeng"
#############################3MYSQL_BACK#########################
if [ ! -d $BACK_DIR$date ];then
mkdir -p $BACK_DIR$date
fi
/usr/local/mysql/bin/mysql -uroot -p'123456' -h 127.0.0.1 -P 3306 -e "FLUSH TABLES WITH READ LOCK"
/usr/local/mysql/bin/mysqldump -uroot -p'123456' -h 127.0.0.1 -P 3306 --default-character-set=gbk --opt --triggers -R --hex-blob --flush-logs --master-data=2 --all-databases > $BACK_DIR$date/fullbackup.sql
/usr/local/mysql/bin/mysql -u root -p'123456' -h 127.0.0.1 -P 3306 -e "UNLOCK TABLES"
##########################
################get postion###############
sed -n "22p" a.sql | awk -F'=|,|;' '{print $2,$4}' > $FILE_DIR/postion
################# FTP TO 16.1 ###############
################# FTP INFO ###############
HOST="192.168.16.1"
USER="user"
PASSWD="password"
FTP_DIR="database"
ftp_db()
{
ftp -n << ! >/dev/null 2>&1 &
open $HOST
user $USER $PASSWD
binary
prompt
cd $FTP_DIR
put $1
bye
!
echo "sql backup suecss!!"
}
##############################################
#####################TRANSFER INFL#############
cd $BACK_DIR$date
if [ ! -f fullbackup.sql ]; then
echo "mysqldump back file faild !!! please check"
exit 1
fi
/bin/tar zcf fullbackup_$date.tar.gz fullbackup.sql
sleep 5
ftp_db fullbackup_$date.tar.gz
二、做增量备份,根据上面完整备份所获得的postion以及binlog 与服务器当前的postion和binlog做增量备份,备份后把当前的postion以及binlog 存到postion文件中,留坐下一次增量备份的其实点。
vi zl_backup.sh
#!/bin/sh
#####for zengliang backup########
DATE=`date +%Y%m%d`
OLDDATE=`date --date='10 days ago' +%Y%m%d`
BACK_DIR="/data/mysql2/"
FILE_DIR="/home/profiles/liuhaifeng"
startbinlog=`awk '{print $1}' $FILE_DIR/postion`
startpostion=`awk '{print $2}' $FILE_DIR/postion`
cd $FILE_DIR
mysql -uroot -p123456 -S /tmp/mysql.sock3306 -h 192.168.16.3 -e "show master status\G;"|awk '{print $2}' > nowpostion
stopbinlog=`sed -n '3p' $FILE_DIR/nowpostion`
stoppostion=`sed -n '4p' $FILE_DIR/nowpostion`
if [ "$startbinlog" == "$stopbinlog" ]; then
mysqlbinlog --start-position=$startpostion --stop-position=$stoppostion $BACK_DIR$startbinlog >> ch_zlback_$DATE.sql
else
startline=`awk "/$startbinlog/{print NR}" $BACK_DIR/mysql-bin-bj1.index`
stopline=`wc -l $BACK_DIR/mysql-bin-bj1.index |awk '{print $1}'`
for i in `seq $startline $stopline`
do
binlog=`sed -n "$i"p $BACK_DIR/mysql-bin-bj1.index |sed 's/.\///g'`
case "$binlog" in
"$startbinlog")
mysqlbinlog --start-position=$startpostion $BACK_DIR$binlog >> ch_zlback_$DATE.sql
;;
"$stopbinlog")
mysqlbinlog --stop-position=$stoppostion $BACK_DIR$binlog >> ch_zlback_$DATE.sql
;;
*)
mysqlbinlog $BACK_DIR$binlog >> ch_zlback_$DATE.sql
esac
done
fi
####for ftp######
HOST="192.168.16.1"
USER="user"
PASSWD="password"
FTP_DIR="database"
ftp_db()
{
ftp -n << ! >/dev/null 2>&1 &
open $HOST
user $USER $PASSWD
binary
prompt
cd $FTP_DIR
put $1
bye
!
echo "sql backup suecss!!"
}
if [ -f ch_zlback_$DATE.sql ];then
tar czf ch_zlback_$DATE.tar.gz ch_zlback_$DATE.sql
sleep 3
ftp_db ch_zlback_$DATE.tar.gz
echo "$stopbinlog $stoppostion" > $FILE_DIR/postion
rm -rf ch_zlback_$OLDDATE.sql ch_zlback_$OLDDATE.tar.gz
else
echo "$DATE backup fail!!!!"
fi
三、做好计划任务
把增量备份脚本 和 完全备份脚本按照想要的时间写在计划任务中,做定时的增量备份。
当然还原的时候需要按照顺序依次还原。