rsync + inotify 實時同步

1. 前言

  2 臺 nginx 須要作集羣, 靜態文件和php文件都在nginx服務器本地。php

  有三種方案:html

    (1)NFSlinux

    (2)Rsync + inotifynginx

    (3)共享存儲服務器web

 

  第一種:當 nfs server端宕機,所有完蛋,第三種:共享存儲服務器單點,若是宕機也完蛋。所以採用 第二種方式:rsync + inotifyapache

 

2. 環境配置

 

  2.1 環境介紹vim

 

  系統:Centos 7.2 x64bash

  server端:192.168.118.14服務器

  client端(同步端):192.168.118.15測試

 

  2.2 搭建過程

  默認防火牆和selinux 關閉,這裏採用 http web服務器測試。

 

  client 端(同步端)配置:

[root@192.168.118.15 ~]#yum install rsync httpd -y

[root@192.168.118.15 ~]#egrep ^[a-z] /etc/rsyncd.conf 
uid = root        # 設置rsync運行權限爲root
gid = root        # 設置rsync運行權限爲root
use chroot = no    # 默認爲true,修改成no,增長對目錄文件軟鏈接的備份
max connections = 200    # 最大鏈接數
timeout = 300    # 設置超時時間
pid file = /var/run/rsyncd.pid    # pid文件的存放位置
log file = /var/log/rsyncd.log    # 日誌文件位置,啓動rsync後自動產生這個文件,無需提早建立
path = /var/www/html    # rsync服務端數據目錄路徑
read only = false    # 設置rsync服務端文件爲讀寫權限
hosts allow = 192.168.118.15    # 容許進行數據同步的客戶端IP地址,能夠設置多個,用英文狀態下逗號隔開
auth users = rsync    # 執行數據同步的用戶名,能夠設置多個,用英文狀態下逗號隔開
secrets file = /etc/rsync.password    # 用戶認證配置文件,裏面保存用戶名稱和密碼,後面會建立這個文件


[root@192.168.118.15 ~]#echo "rsync:123456" > /etc/rsync.password    # 用戶認證配置文件,裏面保存用戶名稱和密碼,後面會建立這個文件
[root@192.168.118.15 ~]#chmod 0600 /etc/rsync.password    # 配置文件權限,若是這裏沒修改權限,後續會報錯
[root@192.168.118.15 ~]#systemctl start rsyncd ; systemctl enable rsyncd 
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

 

 

  到此,客戶端(同步端)配置完畢。

 

  server 端配置:

[root@192.168.118.14 ~]#yum install httpd rsync -y

下載 inotify 並編譯安裝:

下載地址:https://sourceforge.net/projects/inotify-tools/


[root@192.168.118.14 /usr/local/src]#yum install gcc* -y
[root@192.168.118.14 /usr/local/src]#tar xf inotify-tools-3.13.tar.gz 
[root@192.168.118.14 /usr/local/src]#cd inotify-tools-3.13
[root@192.168.118.14 /usr/local/src/inotify-tools-3.13]#./configure --prefix=/usr/local/inotify/
[root@192.168.118.14 /usr/local/src/inotify-tools-3.13]#make && make install
[root@192.168.118.14 /usr/local/src/inotify-tools-3.13]#echo "export PATH=$PATH:/usr/local/inotify/bin" > /etc/profile.d/inotify.sh
[root@192.168.118.14 /usr/local/src/inotify-tools-3.13]#. /etc/profile.d/inotify.sh
[root@192.168.118.14 ~]#echo 123456 > /etc/server.pass 
[root@192.168.118.14 ~]#chmod 0600 /etc/server.pass
# 編寫同步腳本
[root@192.168.118.14 ~]#vim inotify.sh

#!/bin/bash

# 同步端的ip
host_slave=192.168.118.15
# 須要同步的目錄
master_src=/var/www/html/

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib /var/www/html/ | while read file
do
 rsync -az --delete ${master_src} rsync@${host_slave}::apache --password-file=/etc/server.pass
done

 

 

3. 測試

 

 4. 配置過程常見錯誤

 

報錯1:

ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(175) [sender=3.0.9]

 

處理辦法:

  檢查服務端的  /etc/server.pass 權限是不是 600

[root@localhost html]# chmod 600 /etc/server.pass
[root@localhost html]# ll /etc/server.pass
-rw-------. 1 root root 7 Apr 18 11:00 /etc/server.pass

 

 

報錯2:

@ERROR: Unknown module 'apache'
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9]

 

 

處理辦法:

  檢查 client端 rsyncd.conf ,[apache] 模塊內的配置

 

相關文章
相關標籤/搜索