rsync+inotify觸發式遠程同步

 爲了同步網站的網頁文件等(集羣),網上查找資料看到rsync+inotify觸發式遠程同步

然後嘗試實驗下,效果不錯,點滴記憶,方便以後查詢使用!
安裝軟件可以利用rpm包或者編碼包,都是可行的!建議使用編碼包!



服務器端操作
因爲CentOS系統安裝的時候,自帶安裝了rsync軟件,查詢如下: 
rpm -ql rsync
當然如果沒有查詢到,請按照如下命令操作:
yum install rsync* -y

然後我們編輯rsync的配置文件(這個文件不存在,可以自己修改文件名 )
爲了方便直接使用默認的配置文件名 rsyncd.conf
cat >/etc/rsyncd.conf <<EOF
uid = nobody
gid = nobody
user chroot = no                                                 權限方面的問題
max connections =200                                         最大連接數
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[yxfshellhome]                                                  對外公佈的目錄
path = /home/shell                                           同步目錄
ignore errors                                                    忽略I/O錯誤
read only = no                                                  可讀寫
list = no                                                            列表
hosts allow = 192.168.0.0/255.255.255.0             網段
auth users = yxf                                                 用戶
secrets file = /etc/rsyncd.password                     格式:yxf:123456 (用戶名:密碼 ) 本機需要擁有這個用戶
EOF

touch /etc/rsyncd.password
echo “yxf:123456” >/etc/rsyncd.password                
多個用戶請按照如下方式排列
yxf:123456
yxf1:123456
....

爲了管理rsync方便 我們可以利用xinetd來管理它
yum install xinetd* -y

[root@mail shell]# vim /etc/xinetd.d/rsync

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no                                        這裏原先是yes 請更改爲no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
重啓xinetd 
/etc/init.d/xinetd restart
chkconfig xinetd on
那麼rsync也隨之啓動起來了






客戶端(從服務器端)
同上 檢查下rsync是否安裝了 沒有的話 請安裝上面的命令安裝下
即可
同步的命令
rsync -avzP [email protected]::yxfshellhome   /home/shell        (yxfshellhome 爲主服務器對外公開的共享目錄          /home/shell 爲從服務器上接受主服務器共享文檔存放的目錄)
但是這樣 會提示輸入密碼         (主服務器上用來共享的用戶的密碼)
爲了方便加入計劃任務 ,添加一條命令 
rsync -avzP  --delete 
--password-file=/etc/rsyncd.password [email protected]::yxfshellhome   /home/shell  
/etc/rsyncd.password 只要存放共享用戶的密碼即可 不存在的文件 請單獨建立
echo "123456" >/etc/rsyncd.password
--delete:意思是讓主從服務器上共享的目錄內容完全一致 從服務器上有多餘的文件 在執行命令時 將全部刪除
好的如此 這個rsync文件共享服務器完成
 
 
 
 
但是在實際的生產環境中,不可能讓計劃任務開啓rsync同步功能 那樣不能夠保證文件的統一性
這在我們需要利用inotify來檢測雙方文件的差異性而同步文件
 
主服務器上
安裝inotify很是簡單
tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify
make && make install 
注意 需要編譯的環境 yum install gcc gcc-c++ -y
如此 inotify安裝完成
cat > rsync_tongbu.sh <<EOF
#!/bin/sh
srcdir="/home/shell/"                                                                 表示主服務器上共享目錄路徑
ip="192.168.0.67 192.168.0.68"                                                表示需要同步的從服務器IP 多個IP 請中間留一個空格
dstdir="/home/shell/"                                                                 表示需要同步的從服務器上接受共享文件的目錄路徑
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y-%H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${srcdir} \
| while read file
do
        for i in $ip
                do
                rsync -aqztH --delete --progress ${srcdir} root@${i}:${dstdir}
                done
done
EOF 

建立ssh密鑰 需要傳送到個個從服務器上
ssh-kegen -t rsa
scp /root/.sss/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
scp /root/.sss/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
最後 執行rsync_tongbu.sh
 
檢查同步效果 
主服務器上
cd /home/shell
touch {1,2,3,4,5,6,7,8,9,0}{a,b,c,d,e,o,f,g,h,t}.txt
 
然後到從服務器上的/home/shell 下查看 
會看到你想要的內容的
好的 至此 rsync+inotify配置觸發式遠程同步配置完成
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章