Rsync+inotify實現文件同步更新(無防火牆版)web
目錄算法
說明:... 2shell
關閉防火牆firewall2centos
發佈服務器配置rsync:192.168.171.128. 2服務器
安裝配置Rsync. 2ssh
目標服務器配置rsync:192.168.171.129. 4
發佈服務器配置inotify:192.168.171.128. 6
安裝inotify-tools工具,實現目錄實時監控... 6
配置inotify監控腳本rsync.sh——監控PRO目錄... 6
操做系統:Linux version 3.10.0-327.el7.x86_64 (centos7.1151)
發佈服務器:192.168.171.128
目標服務器:192.168.171.129,192.168.171.130,……
目的:將發佈服務器上的文件/pro目錄實時同步到目標服務器的/pro下
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]#firewall-cmd –state
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
Removed symlink/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
#!/bin/bash
systemctl stop firewalld.service
[root@localhost ~]# yum install rsync –y
[root@localhost ~]# vim /etc/rsyncd.conf
# /etc/rsyncd: configurationfile for rsync daemon mode
# See rsyncd.conf man page formore options.
port=873
uid=root
gid=root
use chroot = no
max connections=7
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsyncd.lock
strict modes = true
transfer logging =yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
#同步PRO1
[pro1]
path = /pro
list = true
ignore errors = yes
read only = no
write only =no
hosts allow = 192.168.171.129,192.168.171.130,192.168.171.131
auth users = rsyncuser
secrets file =/etc/rsyncd.secrets
#同步項目2 該項爲擴展,單點同步可忽略
[web1]
path = /web
list = true
ignore errors = yes
read only = no
write only =no
hosts allow =192.168.171.129,192.168.171.130,192.168.171.131
auth users = rsyncuser
secrets file =/etc/rsyncd.secrets
[root@localhost ~]#vim /etc/rsyncd.secrets
123456
[root@localhost ~]# chmod 600 /etc/rsyncd.secrets
守護進程模式啓動方法:
[root@localhost ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
直接關閉進程:
[root@localhost ~]# netstat -nlutp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1284/rsync
tcp6 0 0 :::873 :::* LISTEN 1284/rsync
[root@localhost ~]# kill 1284
[root@localhost ~]#mkdir /pro
[root@localhost ~]# yum install rsync –y
[root@localhost ~]# vim /etc/rsyncd.conf
# /etc/rsyncd: configurationfile for rsync daemon mode
# See rsyncd.conf man page formore options.
port=873
uid=root
gid=root
use chroot = no
max connections=7
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file =/var/run/rsyncd.lock
strict modes = true
transfer logging =yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
#同步項目1
[pro1]
path = /pro
list = false
ignore errors = yes
read only = no
write only =no
hosts allow = *
auth users = rsyncuser
secrets file =/etc/rsyncd.secrets
#同步項目2 該項爲擴展,單點同步可忽略
[web1]
path = /web
list = false
ignore errors = yes
read only = no
write only =no
hosts allow = *
auth users = rsyncuser
secrets file =/etc/rsyncd.secrets
[root@localhost ~]# vim /etc/rsyncd.secrets
rsyncuser:123456
[root@localhost ~]# chmod 600 /etc/rsyncd.secrets
守護進程模式啓動方法:
[root@localhost ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
直接關閉進程:
[root@localhost ~]# netstat -nlutp | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1784/rsync
tcp6 0 0 :::873 :::* LISTEN 1784/rsync
[root@localhost ~]# kill 1784
[root@localhost ~]#mkdir /pro
[root@localhost ~]#rsync -avzH --delete --progress --password-file=/etc/rsyncd.secrets /pro/pro1 rsyncuser@192.168.171.129::pro1
[root@localhost pro]# ll
total 8
drwxr-xr-x. 2 root root 4096Aug 15 17:10 pro1
若是有問題,請參考報錯處理!直至成功後,可繼續下面的操做!
[root@localhost ~]#yum install inotify-tools –y
[root@localhost ~]#vim /etc/mesh/rsync.sh
#!/bin/bash
src=/pro/
des1=pro
host1=192.168.171.129
#host2=192.168.171.130 #備用機
#host3=192.168.171.131 #備用機
user1=rsyncuser
/usr/bin/inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib$src | while read file DATE TIME DIR;
do
rsync -avzH --delete--progress --password-file=/etc/rsyncd.secrets $src $user1@$host1::$des1
#rsync -avzH --delete--progress --password-file=/etc/rsyncd.secrets $src $user1@$host2::$des1
#rsync -avzH --delete--progress --password-file=/etc/rsyncd.secrets $src $user1@$host3::$des1
echo "${files} wasrsynced" >> /var/log/rsync.log 2>&1
done
(若是有多個目錄,可設置多個監控腳本,如有多臺主機,可設置多行代碼)
[root@localhost ~]# chmod 755 /etc/rc.d/rc.local
[root@localhost ~]# vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FORCOMPATIBILITY PURPOSES
# It is highly advisable tocreate own systemd services or udev rules
# to run scripts during bootinstead of using this file.
# In contrast to previousversions due to parallel execution during boot
# this script will NOT be runafter all other services.
# Please note that you mustrun 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will beexecuted during boot.
/bin/bash /etc/mesh/shut-firewall.sh&
/bin/bash /etc/mesh/rsyncd-pro.sh&
/bin/bash /etc/mesh/rsyncdstart.sh&
[root@localhost ~]# vim /etc/mesh/rsyncd-pro.sh
#!/bin/bash
src=/pro
des1=pro
host1=192.168.171.129
host2=192.168.171.130
user1=rsyncuser
/usr/bin/inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib$src | while read file DATE TIME DIR;
do
rsync -avzH --delete --progress --password-file=/etc/rsyncd.secrets $src $user1@$host1::$des1
rsync -avzH --delete--progress --password-file=/etc/rsyncd.secrets $src $user1@$host2::$des1
echo "${files} wasrsynced" >> /var/log/rsync.log 2>&1
done
[root@localhost ~]# vim /etc/mesh/rsyncdstart.sh
#!/bin/bash
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
在發佈服務器上修改inotify的監控主機信息,參見rsync-pro1.sh。
重複準備工做
重複操做目標主機(192.168.171.129)的部署過程。
測試數據同步效果
Inotify參數設置調整以下:
[root@localhost pro]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events =16384
[root@localhost pro]# sysctl -a | grep max_user_watches
fs.epoll.max_user_watches =202874
fs.inotify.max_user_watches =8192
[root@localhost pro]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances= 128
[root@localhost pro]# sysctl -w fs.inotify.max_queued_events="99999999"
fs.inotify.max_queued_events =99999999
[root@localhost pro]# sysctl -w fs.inotify.max_user_watches="99999999"
fs.inotify.max_user_watches =99999999
[root@localhost pro]# sysctl -w fs.inotify.max_user_instances="65535"
fs.inotify.max_user_instances= 65535
vim /etc/sysctl.conf
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
rsync參數說明
rsync -avzH--delete --progress --password-file=/etc/rsyncd.secrets $src$user1@$host1::$des1
-v,--verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對文件傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性,等於-rlptgoD
-r, --recursive 對子目錄以遞歸模式處理
-R, --relative 使用相對路徑信息
-b, --backup 建立備份,也就是對於目的已經存在有一樣的文件名時,將老的文件從新命名爲~filename。可使用--suffix選項來指定不一樣的備份文件前綴。
--backup-dir 將備份文件(如~filename)存放在在目錄下。
-suffix=SUFFIX 定義備份文件前綴
-u, --update 僅僅進行更新,也就是跳過全部已經存在於DST,而且文件時間晚於要備份的文件。(不覆蓋更新的文件)
-l, --links 保留軟鏈結
-L, --copy-links 想對待常規文件同樣處理軟鏈結
--copy-unsafe-links 僅僅拷貝指向SRC路徑目錄樹之外的鏈結
--safe-links 忽略指向SRC路徑目錄樹之外的鏈結
-H, --hard-links 保留硬鏈結
-p, --perms 保持文件權限
-o, --owner 保持文件屬主信息
-g, --group 保持文件屬組信息
-D, --devices 保持設備文件信息
-t, --times 保持文件時間信息
-S, --sparse 對稀疏文件進行特殊處理以節省DST的空間
-n, --dry-run現實哪些文件將被傳輸
-W, --whole-file 拷貝文件,不進行增量檢測
-x, --one-file-system 不要跨越文件系統邊界
-B, --block-size=SIZE 檢驗算法使用的塊尺寸,默認是700字節
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行數據同步
--rsync-path=PATH 指定遠程服務器上的rsync命令所在路徑信息
-C, --cvs-exclude 使用和CVS同樣的方法自動忽略文件,用來排除那些不但願傳輸的文件
--existing 僅僅更新那些已經存在於DST的文件,而不備份那些新建立的文件
--delete 刪除那些DST中SRC沒有的文件
--delete-excluded 一樣刪除接收端那些被該選項指定排除的文件
--delete-after 傳輸結束之後再刪除
--ignore-errors 及時出現IO錯誤也進行刪除
--max-delete=NUM 最多刪除NUM個文件
--partial 保留那些因故沒有徹底傳輸的文件,以是加快隨後的再次傳輸
--force 強制刪除目錄,即便不爲空
--numeric-ids 不將數字的用戶和組ID匹配爲用戶名和組名
--timeout=TIME IP超時時間,單位爲秒
-I, --ignore-times 不跳過那些有一樣的時間和長度的文件
--size-only 當決定是否要備份文件時,僅僅察看文件大小而不考慮文件時間
--modify-window=NUM 決定文件是否時間相同時使用的時間戳窗口,默認爲0
-T --temp-dir=DIR 在DIR中建立臨時文件
--compare-dest=DIR 一樣比較DIR中的文件來決定是否須要備份
-P 等同於 --partial
--progress 顯示備份過程
-z, --compress 對備份的文件在傳輸時進行壓縮處理--exclude=PATTERN 指定排除不須要傳輸的文件模式--include=PATTERN 指定不排除而須要傳輸的文件模式--exclude-from=FILE 排除FILE中指定模式的文件--include-from=FILE 不排除FILE指定模式匹配的文件--version 打印版本信息--address 綁定到特定的地址--config=FILE 指定其餘的配置文件,不使用默認的rsyncd.conf文件--port=PORT 指定其餘的rsync服務端口--blocking-io 對遠程shell使用阻塞IO-stats 給出某些文件的傳輸狀態--progress 在傳輸時現實傳輸過程--log-format=formAT 指定日誌文件格式--password-file=FILE 從FILE中獲得密碼--bwlimit=KBPS 限制I/O帶寬,KBytesper second-h, --help 顯示幫助信息