準備兩臺機子,一臺是192.168.10.150做數據源服務器,一臺是192.168.10.151做目標服務器,如圖所示:
現在要實現的需求是150的/data/www/目錄實時同步到151的/data/www/目錄下
一、在151上配置rsync 服務器
cd /usr/local/src
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
tar -zxvf rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/usr/local/rsync
make && make install
編輯配置文件 vim /etc/rsyncd.conf,把它的內容改成如下:
uid = nobody gid = nobody use chroot = no max connections =30 pid file =/var/run/rsyncd.pid lock file =/var/run/rsyncd.lock log file =/var/log/rsyncd.log transfer logging =yes log format = %t %a%m %f %b syslog facility =local3 timeout = 300 [www] path=/data/www/ comment=www read only = no auth users=zhang secretsfile=/etc/rsync.pas hostsallow=192.168.10.150
然後 mkdir -p /data/www/
chown -R nobody.nobody /data/www/
vim /etc/rsync.pas編輯密碼文件,加入以下內容:
zhang:123456
chmod 600 /etc/rsync.pas
/usr/local/rsync/bin/rsync–daemon 啓動rsyncd服務
二、在150上的操作
安裝rsync,和151上的一樣,兩邊的rsync版本要保持一致,不然可能會出現莫名其妙的錯誤
vim /etc/rsync.pas 編輯密碼文件,加入以下內容:
123456
chmod 600 /etc/rsync.pas
安裝inotify-tools
tar -zxvf inotify-tools-3.14.tar.gz
cd inotify-tools
./configure--prefix=/usr/local/inotify-tools/
make && make install
mkdir -p /data/www/
配置實時同步腳本,rsync-inotify.sh
#!/bin/bash dir=/data/www/ ip=192.168.10.151 /usr/local/inotify-tools/bin/inotifywait-mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -emodify,delete,create,attrib ${dir} | while read file do /usr/local/rsync/bin/rsync -av --progress--delete --password-file=/etc/rsync.pas /data/www/ zhang@$ip::www done
然後執行nohup sh rsync-inotify.sh & 即可