如何实现mysql 全量和增量备份

下面一起来了解下如何实现mysql 全量和增量备份,相信大家看完肯定会受益匪浅,文字在精不在多,希望如何实现MysqL 全量和增量备份这篇短内容是你想要的。

MysqL 全量备份:

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

看完如何实现MysqL 全量和增量备份这篇文章后,很多读者朋友肯定会想要了解更多的相关内容,如需获取更多的行业信息,可以关注我们的行业资讯栏目。

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...