文章转自: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 "-d 14 day ago" +%Y%m%d%H%M%S)
#The start time point to backup binlog, the usage of mysqlbinlog is --start-datetime, --stop-datetime, time format is %Y%m%d%H%M%S, eg:20170502171054, time zones is [start-datetime, stop-datetime)
#The date to start backup binlog is yesterday at this very moment!
START_BACKUPBINLOG_TIMEPOINT=$(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_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 was exist."
return 0
else
echo "$BACKUPDIR is not exist, now create it."
mkdir -pv $BACKUPDIR
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 "$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_$DATE.sql #for test !!
$DB_BIN/mysqldump --lock-all-tables --flush-logs --master-data=2 -u$DB_USER -p$DB_PASSWORD -P$DB_PORT -A |gzip > $BACKUPDIR/db_fullbak_$DATE.sql.gz
FULL_HEALTH=`echo $?`
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_BIN/mysqladmin -u$DB_USER -p$DB_PASSWORD -P$DB_PORT flush-logs
$DB_BIN/mysql -u$DB_USER -p$DB_PASSWORD -P$DB_PORT -e "purge master logs before ${DELETE_OLDLOG_TIME}"
for i in `cat $BINLOG_INDEX`
do
$DB_BIN/mysqlbinlog -u$DB_USER -p$DB_PASSWORD -P$DB_PORT --start-datetime="$START_BACKUPBINLOG_TIMEPOINT" $i |gzip >> $BACKUPDIR/db_daily_$DATE.sql.gz
done
# $DB_BIN/mysqlbinlog -u$DB_USER -p$DB_PASSWORD -P$DB_PORT --start-datetime="$START_BACKUPBINLOG_TIME" $LOG_DIR/mysql-bin.[0-9]* |gzip >> $BACKUPDIR/db_daily_$DATE.sql.gz
INCREASE_HEALTH=`echo $?`
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 was exist."
return 0
else
echo "$BACKUPDIR_OLDER is not exist, now create it."
mkdir -pv $BACKUPDIR_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 $BACKUPDIR -name "*.sql.gz" -mtime +7 -exec ls -lh {} \;`
returnkey_old=`find $BACKUPDIR_OLDER -name "*.sql.gz" -mtime +14 -exec ls -lh {} \;`
if [[ $returnkey != '' ]];then

echo "----------------------"
echo "Moving the older backuped file out of 7 days to $BACKUPDIR_OLDER."
echo "The moved file list is:"
find $BACKUPDIR -name "*.sql.gz" -mtime +7 -exec mv {} $BACKUPDIR_OLDER \;
echo "-----------------------"
elif [[ $returnkey_old != '' ]];then
#delete the backuped file that created time out of 14 days from BACKUPDIR_OLDER directory.
echo "Delete the older backuped file out of 14 days from $BACKUPDIR_OLDER."
echo "The deleted files list is:"
find $BACKUPDIR_OLDER -name "*.sql.gz" -mtime +14 -exec rm -fr {} \;
fi
}
####################################
#--------------main----------------#
####################################
function MAIN(){
DB_RUN #Judge the process is run or not, if not run, the script will not bakup db
Run_process=`echo $?`
echo $?
if [[ $Run_process == 0 ]];then
BINLOG_EXSIT
binlog_index=`echo $?`
if [[ $binlog_index == 0 ]];then
echo "**********START**********"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "~~~~~~~~~~~~~~~~~~~~~~~"
if [[ $TODAY == $FULL_BAKDAY ]];then
echo "Start completed bakup ..."
INCREASE_BAKUP
FULL_BAKUP #full backup to all DB
BAKUP_CLEANER
else
echo "Start increaing bakup ..."
INCREASE_BAKUP
fi
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "**********END**********"
else
echo "**********START**********"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo "Sorry, MySQL binlog was not configed, please config the my.cnf firstly!"
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "**********END**********"
fi
else
echo "**********START**********"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo "Sorry, MySQL was not running, the db could not be backuped!"
echo "~~~~~~~~~~~~~~~~~~~~~~~"
echo $(date +"%y-%m-%d %H:%M:%S %A")
echo "**********END**********"
fi
}
#starting runing

BACKDIR_EXSIT $BACKUP_LOG
OLDER_BACKDIR_EXSIT $BACKUP_LOG
MAIN >> $BACKUP_LOG

三、测试方法:

使用了一个测试脚本,修改日期,达到一个月的演示效果。

#!/bin/bash
for day in {1..30}
do
date -s "2017-06-$day 12:00:00"
/bin/bash /root/bakup/mysql_backup.sh
done

四、脚本使用方法:

crontab -e
0 3 * * * /bin/bash /root/bakup/mysql_bakup.sh > /dev/null 2>&1 空格

#加个空格,不然有些机器不能执行脚本

MySQL自动化(全量+增量)备份脚本的更多相关文章

  1. mysql全量+增量备份脚本

    cat xtrabackup_mysql.sh #!/bin/bash #title :xtrabackup_mysql.sh #description :backup mysql by using ...

  2. Mysql备份系列(4)--lvm-snapshot备份mysql数据(全量+增量)操作记录

    Mysql最常用的三种备份工具分别是mysqldump.Xtrabackup(innobackupex工具).lvm-snapshot快照.前面分别介绍了:Mysql备份系列(1)--备份方案总结性梳 ...

  3. 【MySQL】全量+增量的备份/恢复

    生产环境中,有时需要做MySQL的备份和恢复工作.因MySQL是在运行过程中的,做全量备份需要时间,全量备份完成后又有数据变动,此时需要增量备份辅助.如果想恢复数据到一个空库(例如数据迁移或者上云等更 ...

  4. Xtrabackup全量 增量备份详解

    xtrabackup是Percona公司CTO Vadim参与开发的一款基于InnoDB的在线热备工具,具有开源,免费,支持在线热备,备份恢复速度快,占用磁盘空间小等特点,并且支持不同情况下的多种备份 ...

  5. Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录

    在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 线上数据库备份场景:每周日执行一次全量备份,然后每天下午1点执行MySQLdump增量备份 ...

  6. Mysql备份系列(3)--innobackupex备份mysql大数据(全量+增量)操作记录

    在日常的linux运维工作中,大数据量备份与还原,始终是个难点.关于mysql的备份和恢复,比较传统的是用mysqldump工具,今天这里推荐另一个备份工具innobackupex.innobacku ...

  7. mysql的全量备份与增量备份

    mysql的全量备份与增量备份 全量备份:可以使用mysqldump直接备份整个库或者是备份其中某一个库或者一个库中的某个表. 备份所有数据库:[root@my ~]# mysqldump -uroo ...

  8. mysql完美增量备份脚本

    是否因为mysql太大,来回备份浪费资源带宽而发愁,如果想解决这个麻烦就需要增量备份. vi /etc/my.cnf开启日志及定期清理日志log-bin=mysql-binbinlog_format= ...

  9. 企业级mysql数据库完全备份、增量备份脚本

    企业完全备份脚本 [root@client ~]# vim /opt/mysql_bak_wanbei.sh #!/bin/bash #MySQL数据库完全备份脚本 #设置登录变量 MY_USER=& ...

  10. Xtrabackup每周增量备份脚本程序

    Xtrabackup每周增量备份脚本程序(含附件)   程序描述 本程序是一个对percona xtrabackup使用的脚本,它完成了MySQL每周的备份. 程序结构 此程序包含了4个目录(bin. ...

随机推荐

  1. CentOS7下配置网络yum源(附带下载地址)

    一.查看外网是否通畅 配置网络yum源(需要保证外网开通,我这里是使用网易163提供开源镜像站) 二.下载repo文件 cd /etc/yum.repos.dwget http://mirrors.1 ...

  2. cmd批处理延迟代码 结束进程

    choice /t 5 /d y /n >nul taskkill /im chrome.exe /f pause

  3. 《HelloGitHub》第 14 期

    公告 欢迎通过在 GitHub 上新建 issues 方式推荐项目,我真心希望读者可以在 HelloGItHub,找到真正的编程乐趣! <HelloGitHub>第 14 期 兴趣是最好的 ...

  4. css因Mime类型不匹配而被忽略,怎么解决

    问题:在火狐.谷歌都可以正常显示出来,在别人的IE浏览器上也可以正常显示出来,但是在自己的ie浏览器就完全不能加载的熬样式了 控制台报告 SEC7113: CSS 因 Mime 类型不匹配而被忽略 答 ...

  5. 源码分析 Large-Margin Softmax Loss for Convolutional Neural Networks

    作者在Caffe中引入了一个新层,一般情况在Caffe中引入一个新层需要修改caffe.proto,添加该层头文件*.hpp,CPU实现*.cpp,GPU实现*.cu,代码结果如下图所示: caffe ...

  6. Spring的<context:property-placeholder.../>在junit中不起作用,失效,解决方法

    大家都知道,我们使用spring框架的时候喜欢把可以配置的变量放入一个properties配置文件中,然后在spring的applicationContext.xml配置文件中加入配置: <co ...

  7. 关于PHP高并发抢购系统设计

    内容并发抢购系统注意事项高并发架构设计描述程序端核心代码实现订单流程mysql 端并发解决方案注意事项(1)高并发环境下,对于服务器cup.内存.网络宽带使用率会瞬间暴涨,需要注意对同服务器上其他应用 ...

  8. Edison与Arduino通过USB对接通信

    虽然Edison本身就装在一块Arduino扩展板上,但当要接的传感器太多了的时候一个扩展板也不够用了.因此需要再用一Arduino板扩展功能.而两者需要通信才能达到一个整体的效果,因此这里介绍如何用 ...

  9. jquery中的$(document).ready()使用小结

    本篇文章主要是对jquery中的$(document).ready()使用方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 window.onload = function(){ ...

  10. Ubuntu下用cue文件对ape和wav文件自动分轨

    很多音乐CD的镜像文件都带cue格式的分轨文件,在Ubuntu Linux下可以通过一些工具来实现自动分轨. 一.Ubuntu下需要安装的工具有flac,shntool,libav-tools $ s ...