Zabbix監控系列之-Rsync+Inotify實現文件同步更新(無防火牆版)

Rsync+inotify實現文件同步更新(無防火牆版)

目錄

說明:... 2

準備工作:關閉服務器的防火牆... 2

關閉防火牆firewall2

禁止開機啓動防火牆... 2

編寫腳本”shut-firewall.sh”. 2

發佈服務器配置rsync192.168.171.128. 2

安裝配置Rsync. 2

配置rsyncd.conf文件... 2

創建認證文件:/etc/rsyncd.secrets. 3

設置文件權限... 3

啓動rsync服務... 4

關閉rsync服務... 4

創建目錄... 4

目標服務器配置rsync192.168.171.129. 4

安裝配置Rsync. 4

配置rsyncd.conf文件... 4

創建安全認證文件rsyncd.secrets. 5

設置文件權限... 5

啓動rsync服務... 5

關閉rsync服務... 5

創建目錄... 6

在發佈服務器上,手動測試數據同步效果!... 6

目錄效果... 6

發佈服務器配置inotify192.168.171.128. 6

安裝inotify-tools工具,實現目錄實時監控... 6

配置inotify監控腳本rsync.sh——監控PRO目錄... 6

設置開機啓動inotify等腳本... 7

附錄腳本內容... 7

新增機器,需要做的幾件事兒... 7


說明:

操作系統:Linux version 3.10.0-327.el7.x86_64 centos7.1151

發佈服務器:192.168.171.128

目標服務器:192.168.171.129192.168.171.130……

目的:將發佈服務器上的文件/pro目錄實時同步到目標服務器的/pro

 

準備工作:關閉服務器的防火牆

關閉防火牆firewall

[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.

編寫腳本”shut-firewall.sh”

#!/bin/bash

systemctl stop firewalld.service

 

發佈服務器配置rsync192.168.171.128

安裝配置Rsync

[root@localhost ~]# yum install rsync –y

配置rsyncd.conf文件

[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

#同步項目該項爲擴展,單點同步可忽略

[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

創建認證文件:/etc/rsyncd.secrets

[root@localhost ~]#vim /etc/rsyncd.secrets

123456

設置文件權限

[root@localhost ~]# chmod 600 /etc/rsyncd.secrets

啓動rsync服務

守護進程模式啓動方法:

[root@localhost ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

關閉rsync服務

直接關閉進程:

[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

目標服務器配置rsync192.168.171.129

安裝配置Rsync

[root@localhost ~]# yum install rsync –y

配置rsyncd.conf文件

[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

 

#同步項目該項爲擴展,單點同步可忽略

[web1]

path = /web

list = false

ignore errors = yes

read only = no

write only =no

hosts allow = *

auth users = rsyncuser

secrets file =/etc/rsyncd.secrets

創建安全認證文件rsyncd.secrets

[root@localhost ~]# vim /etc/rsyncd.secrets

rsyncuser:123456

設置文件權限

[root@localhost ~]# chmod 600 /etc/rsyncd.secrets

啓動rsync服務

守護進程模式啓動方法:

[root@localhost ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd.conf

關閉rsync服務

直接關閉進程:

[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 [email protected]::pro1

目錄效果

[root@localhost pro]# ll

total 8

drwxr-xr-x. 2 root root 4096Aug 15 17:10 pro1

 

如果有問題,請參考報錯處理!直至成功後,可繼續下面的操作!

 

發佈服務器配置inotify192.168.171.128

安裝inotify-tools工具,實現目錄實時監控

[root@localhost ~]#yum install inotify-tools –y

配置inotify監控腳本rsync.sh——監控PRO目錄

[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

(如果有多個目錄,可設置多個監控腳本,若有多臺主機,可設置多行代碼)

設置開機啓動inotify等腳本

[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

 

新增機器,需要做的幾件事兒

  1. 在發佈服務器上修改inotify的監控主機信息,參見rsync-pro1.sh

  2. 重複準備工作

  3. 重複操作目標主機(192.168.171.129)的部署過程。

  4. 測試數據同步效果


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 顯示幫助信息

 


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