rsync+inotify實現數據觸發同步

實驗需求:在web服務器上搭建rsync服務,客戶端上傳網頁文件,在客戶端寫腳本同步web服務器上的網頁數據


一.WEB服務器(192.168.100.1)上搭建rsync服務

1.開啓服務

#vim /etc/xinetd.d/rsync

……

   disable = no                   //把disable = yes改成no    

……

或者

# chkconfig rsync on


#service xinetd start


2.創建rsync主配置文件

# vim /etc/rsyncd.conf              //默認不存在

uid = root

gid = root

use chroot = yes

pid file = /var/run/rsyncd.pid

log file = /var/log/rsyncd.log


[webfile]

path = /var/www/html              //共享網站根目錄

comment = Rsync share test

read only = no                  //需要寫入權限一定設置爲no


3.重啓xinetd服務

# service xinetd restart



二.客戶端安裝inotify工具inotify-tools,並通過腳本實現rsync觸發同步

inotify-tools 是爲linux下inotify文件監控工具提供的一套c的開發接口庫函數,同時還提供了一系列的命令行工具,這些工具可以用來監控文件系統的事件。inotify-tools是用c編寫的,除了要求內核支持inotify外,不依賴於其他。inotify-tools提供兩種工具,一是inotifywait,它是用來監控文件或目錄的變化,二是inotifywatch,它是用來統計文件系統訪問的次數。如果列出的內核版本不低於2.6.13,系統就支持 inotify。還可以檢查機器的 /usr/include/sys/inotify.h 文件。如果它存在,表明內核支持 inotify。


1.安裝軟件

# tar -zxvf inotify-tools-3.14.tar.gz

# cd inotify-tools-3.14

# ./configure 

# make

# make install

# ls /usr/local/bin/inotify*

/usr/local/bin/inotifywait  /usr/local/bin/inotifywatch      //有2個工具,可以通過命令加--help查看使用說明


2.創建網頁文件上傳目錄和認證口令文件

# mkdir /data

# echo 123456 > /root/rsync_pass    //創建認證口令文件並將同步密碼寫入

# chmod 600 /root/rsync_pass


3.使用rsync+inotifywait工具編寫腳本實現觸發同步

# vim rsync_webfile.sh 

/bin/bash

host1=192.168.100.1

src=/data/                  //加/只上傳data下的文件,不加/連data目錄一起上傳

dst1=webfile

user1=root

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src\

| while read files

        do

        /usr/bin/rsync -az --delete --password-file\=/root/rsync_pass  $src $user1@$host1::$dst1

                echo "${files} was rsynced" >>/tmp/rsync.log 2>&1

         done           



4.上傳網頁文件測試

# echo test > /data/test.html

# chmod +x rsync_webfile.sh

# ./rsync_webfile.sh &



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章