使用rsync進行MySQL增量備份

來源:互聯網
上載者:User

一、環境描述(使用rsync進行mysql的增量備份)

192.168.0.2為備份伺服器

192.168.0.3為需要經常備份的mysql資料庫

二、主要配置

1.備份伺服器配置
useradd mysql_db -d /data/bak
passwd rsync_server
#密碼我配置的為“123”

2.mysql伺服器配置
vi mysql_back.sh
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
binlog_dir=/data/mysql/3306/logs
#delete old binlog FileList
if [ -f $binlog_dir/mysql-bin.index ];then
 rm -f $binlog_dir/mysql-bin.index
fi
passwd="123"
ls -l $binlog_dir |grep  mysql-bin| awk '{print $8}' >/data/mysql/3306/logs/binlog/mysql-bin.index
Rsync_exec(){
        expect -c "
        set timeout 600;
        spawn rsync -rpogtv --progress --files-from=$binlog_dir/binlog/mysql-bin.index $binlog_dir mysql_db@192.168.0.2:/data/bak
        expect {
                \"*yes/no*\" {send \"yes\r\";exp_continue}
                \"*password*\" {send \"$passwd\r\";}
        }
        expect eof;"
}
Rsync_exec
 
3.賦予mysql_back.sh可執行檔權利,添加計劃任務即可使用。
 
//mysql_back.sh指令碼中的相關參數解釋。expect -c中的-c全拼為command命令的含義,也就是說-c 後面跟的是相關的命令,但這些命令要用" "引起來。set timeout設定了指令碼的逾時為600秒,spawn為運行系統命令的開始模式。rsync -rpogtv中r表示遞迴進入目錄,p表示保留檔案原來的許可權,o表示保留檔案原來的擁有者,g表示保留檔案原來的所屬組,t表示保留檔案原來建立或修改後的時間,v表示增加冗長資訊,--progress表示顯示rsync的過程,--files-from表示從哪裡擷取需要進行rsync的檔案。expect 在這裡使用的目的是為了避免互動式,其中\"*yes/no*\"兩端的\為其後面跟著的"的轉義符,*為任意匹配,eof表示程式的結束。

相關文章

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.