mysql全量備份、增量備份實現方法_Mysql

來源:互聯網
上載者:User

mysql全量備份、增量備份。開啟mysql的logbin日誌功能。在/etc/my.cnf檔案中加入以下代碼:

[mysqld]log-bin = "/home/mysql/logbin.log"binlog-format = ROWlog-bin-index = "/home/mysql/logindex"binlog_cache_size=32mmax_binlog_cache_size=512mmax_binlog_size=512m

重啟mysql即可。其中路徑 /home/mysql的使用者和組要改成mysql。

2、增量備份
在/home/mysql/目錄下建立以下目錄:

mkdir -p /home/mysql/backup/daily

增量備份指令碼

cd /home/mysqlvi binlogbak.sh#!/bin/bashexport LANG=en_US.UTF-8BakDir=/home/mysql/backup/dailyBinDir=/home/mysqlLogFile=/home/mysql/backup/binlog.logBinFile=/home/mysql/logindex.indexmysqladmin -uroot -proot123 flush-logs#這個是用於產生新的mysql-bin.00000*檔案Counter=`wc -l $BinFile |awk '{print $1}'`NextNum=0#這個for迴圈用於比對$Counter,$NextNum這兩個值來確定檔案是不是存在或最新的。for file in `cat $BinFile`do    base=`basename $file`    #basename用於截取mysql-bin.00000*檔案名稱,去掉./mysql-bin.000005前面的./    NextNum=`expr $NextNum + 1`    if [ $NextNum -eq $Counter ]    then        echo $base skip! >> $LogFile    else        dest=$BakDir/$base        if(test -e $dest)        #test -e用於檢測目標檔案是否存在,存在就寫exist!到$LogFile去。        then            echo $base exist! >> $LogFile        else            cp $BinDir/$base $BakDir            echo $base copying >> $LogFile        fi    fidoneecho `date +"%Y年%m月%d日 %H:%M:%S"` Bakup succ! >> $LogFile

賦予binlogbak.sh執行許可權

chmod a+x /home/mysql/binlogbak.sh

3、全量備份

vi databak.sh#!/bin/bashexport LANG=en_US.UTF-8BakDir=/home/mysql/backupLogFile=/home/mysql/backup/bak.logDate=`date +%Y%m%d`Begin=`date +"%Y年%m月%d日 %H:%M:%S"`cd $BakDirDumpFile=$Date.sqlGZDumpFile=$Date.sql.tgzmysqldump -uroot -proot123 --all-databases --flush-logs --delete-master-logs --single-transaction > $DumpFiletar -czvf $GZDumpFile $DumpFilerm $DumpFilecount=$(ls -l *.tgz |wc -l)if [ $count -ge 5 ]thenfile=$(ls -l *.tgz |awk '{print $9}'|awk 'NR==1')rm -f $filefi#只保留過去四周的資料庫內容Last=`date +"%Y年%m月%d日 %H:%M:%S"`echo 開始:$Begin 結束:$Last $GZDumpFile succ >> $LogFilecd $BakDir/dailyrm -f *

賦予databak.sh 執行許可權

chmod a+x /home/mysql/databak.sh

4、開啟定時任務

vi /etc/crontab#每個星期日淩晨3:00執行完全備份指令碼0 3 * * 0 /home/mysql/databak.sh >/dev/null 2>&1#周一到周六淩晨3:00做增量備份0 3 * * 1-6 /home/mysql/binlogbak.sh >/dev/null 2>&1

使上述定時任務生效

crontab /etc/crontab

查看定時任務

crontab -l

完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.