标签:基于mysqldump编写自动全备增备的shell脚本
在线上MySQL数据库备份分为全备和增备,而xtrabackup备份已经支持了增量备份了,但是mysqldump就不支持增量备份,所以我们需要写一个shell脚本对于mysqldump来自动全备和增备。
一下脚本要求我们做一个全备的策略,然后如何做增量备份,自动完成每天执行增量备份,每个星期天执行全备。备份完删除二进制文件,减低磁盘压力。
[root@cml5 ~]# 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/mydata mysqlbak_log=/usr/local/mysql/mydata/mysqlbak.log DATE=`date +%Y%m%d` TIME=`date +"%y-%m %H:%M:%S"` DUMPBAK=$DATE.sql GZDUMPBAK=$DATE.sql.gz WEEK=`date +%w` cd $mysqlbak_dir USER=root PASSWD=redhat bin_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_dir fi 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
##给脚本权限:
[root@cml5 ~]# cd /usr/local/src/script/ [root@cml5 script]# ls mysqlback.sh [root@cml5 script]# chmod a+x mysqlback.sh
##把它加入到crontab几乎任务里面:
[root@cml5 script]# crontab -e 1 0 * * * /usr/local/src/script/mysqlback.sh
本文出自 “第一个legehappy51cto博客” 博客,请务必保留此出处http://legehappy.blog.51cto.com/13251607/1980183
标签:基于mysqldump编写自动全备增备的shell脚本
原文地址:http://legehappy.blog.51cto.com/13251607/1980183