Home > Database > Mysql Tutorial > mysql全量和增量备份脚本

mysql全量和增量备份脚本

WBOY
Release: 2016-06-07 14:51:54
Original
1111 people have browsed it

全量: [root@masterleo]#catDBfullBak.sh#!/bin/bash#usemysqldumptofullybackupmysqldataBakDir=/root/leo/fullLogFile=/root/leo/full/bak.logDate=`date+%Y%m%d`Begin=`date+"%Y年%m月%d日%H:%M:%S"`cd$BakDirDumpFile=$Date.sqlGZDumpFile=$Date.sql.tg

全量:

[root@master leo]# cat DBfullBak.sh 
#!/bin/bash
#use mysqldump to fully backup mysql data

BakDir=/root/leo/full
LogFile=/root/leo/full/bak.log

Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
mysqldump -uroot -p123456 --all-databases --lock-all-tables --routines --triggers --events --master-data=2 --flush-logs > $DumpFile
tar zcvf $GZDumpFile $DumpFile
rm $DumpFile
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
Copy after login

参数注释:

--all-databases   #备份所有库
--lock-all-tables #为所有表加读锁
--routinge        #存储过程与函数
--triggers        #触发器
--events          #记录事件
--master-data=2   #在备份文件中记录当前二进制日志的位置,并且为注释的,1是不注释掉在主从复制中才有意义
--flush-logs      #日志滚动一次
Copy after login

结果如下:

[root@master full]# ls
20140728.sql.tgz  bak.log
[root@master full]# cat bak.log 
开始:2014年07月28日 19:02:59 结束:2014年07月28日 19:02:59 20140728.sql.tgz succ
开始:2014年07月28日 19:12:01 结束:2014年07月28日 19:12:01 20140728.sql.tgz succ
[root@master full]#
Copy after login

增量备份:

[root@master leo]# cat DBDailyBak.sh 
#!/bin/bash

BakDir=/root/leo/binlog/
BinDir=/var/lib/mysql

LogFile=/root/leo/binlog/bak.log
BinFile=/var/lib/mysql/mysql-bin.index

mysqladmin -uroot -p123456 flush-logs

Counter=`wc -l $BinFile|awk '{print $1}'`
NextNum=0

for file in `cat $BinFile`
do 
    base=`basename $file`
    NextNum=`expr $NextNum + 1`
    if [ $NextNum -eq $Counter ]
    then
        echo $base skip! >> $LogFile
    else
        dest=$BakDir/$base
        if(test -e $dest)
        then
            echo $base exist! >> $LogFile
        else
            cp $BinDir/$base $BakDir/
            echo $base copying >> $LogFile
        fi
    fi
done

echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next Bakup succ~ >> $LogFile
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template