Centos中Rsync結合Inotify 即時同步配置(資料量不大的時候使用)

來源:互聯網
上載者:User

首先先配置Rsync,上節已經說過了

這裡直接說明Inotify

安裝配置inotify 服務

在8.8.8.110 上搭建inotify 服務

1、下載安裝

 代碼如下 複製代碼

wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz

tar xzvf inotify-tools-3.13.tar.gz

cd inotify-tools-3.13

./configure  --prefix=/usr/local/inotify

make

make install

2、建立inotify_rsync.sh指令碼

(最好在伺服器上直接建立檔案,windows上建立的.sh檔案可能執行不了,需要修改轉換格式
解決方案:
vi  filename
然後用命令
:set ff? #可以看到dos或unix的字樣. 如果的確是dos格式的
然後用
:set ff=unix #把它強製為unix格式的, 然後存檔退出。
再次運行指令碼。)

 代碼如下 複製代碼

vi inotify_rsync.sh
以下是指令碼內容---------------------------------------
#!/bin/sh
#date:2015-1-11
#function:rysnc 8.8.8.110  to  8.8.8.111

if [ ! -f /etc/passwd.txt];then
       echo "mypassword">/etc/passwd.txt
       /bin/chmod 600 /etc/passwd.txt
fi
log=/opt/logs/rsync.log
src="/home/DemoRoot/Uploads/"
host="8.8.8.111"
module="home_www"

/usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src |  while read DATE TIME DIR FILE; do

      FILECHANGE=${DIR}${FILE}

      /usr/bin/rsync -avH --delete  --progress --password-file=/etc/passwd.txt $src root@$host::$module &
      echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> $log
done

指令碼結束------------------------------------------------
相關註解如下:
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${src}
-m 是保持一直監聽
-r 是遞迴查看目錄
-q 是列印出事件

-e close_write,modify,delete,create,attrib 是指 “監聽 建立 移動 刪除 寫入 許可權” 事件
/usr/bin/rsync -avH --delete  --progress --password-file
-a 存檔模式
-H 儲存硬串連
-delete 刪除於多餘檔案
--password-file 密碼檔案
今天參數可以man rsync

要排除同步某個目錄時,為rsync添加--exculde=PATTERN參數,注意,路徑是相對路徑,具體查看man rsync。
要排除某個目錄的事件監聽的處理時,為inotifywait添加--exclude或--excludei參數,具體查看man inotifywait。
--exclude-from="/usr/local/inotify/logs/rules.txt" 可以匹配過濾檔案:
如排除包括 .svn的檔案:
#cat /usr/local/inotify/logs/rules.txt
- *.svn*

inotifywait 命令產生三個傳回值,分別是“日期,時間,檔案” 這3個傳回值會做為參數傳給read,因此指令碼中的“while read D E F” 寫法細化了傳回值。

賦予指令碼可執行許可權
chmod  -R 755  inotify_rsync.sh
運行
nohup  /opt/inotify_rsync.sh &
顯示進程列表
ps -ef|grep inotify_rsync
顯示日誌
tail -f /opt/logs/rsync.log

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.