天天看點

Rsync + inotify 實作檔案實時同步

實驗目的:

實作兩台Centos 7 主控端之間檔案可以實時同步

測試環境

服務端ip :192.168.10.180

用戶端ip :192.168.10.181

同步目錄:/home/test/html/

同步使用者:test

部署服務端

Centos 7 預設已經安裝 xinetd  rsync

檢視 xinetd  rsync 是否安裝

rpm -aq |grep xinetd

xinetd-2.3.15-13.el7.x86_64

rpm -aq |grep rsync

rsync-3.0.9-17.el7.x86_64

建立配置檔案将xinetd服務下面的配置檔案,将rsync交給xinetd管理,預設情況下centos7 沒有此配置檔案

cat /etc/xinetd.d/rsync

# default: off

# description: The rsync server is a goodaddition to an ftp server, as it

#  allows crc checksumming etc.

service rsync

{

       disable              = no 

       flags                  = IPv6

       socket_type      = stream

       wait                   = no

       user                   = root

       server                =/usr/bin/rsync

       server_args       = --daemon

       log_on_failure  += USERID

重新開機rsync 服務

systemctl restart xinetd

檢視

netstat -tnlp | grep rsync

tcp       0      0 192.168.10.180:873        0.0.0.0:*               LISTEN      1016/rsync

建立同步使用者(用戶端也需要做同樣操作)

useradd test

echo test | passwd test --stdin

Changing password for user test.

passwd: all authentication tokens updatedsuccessfully.

切換普通使用者 test  建立ssh 免登陸

Su – test

ssh-keygen ## 一路回車

Generating public/private rsa key pair.

Enter file in which to save the key(/home/test/.ssh/id_rsa):

Created directory '/home/test/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in/home/test/.ssh/id_rsa.

Your public key has been saved in/home/test/.ssh/id_rsa.pub.

The key fingerprint is:

74:d0:93:24:91:73:8d:1c:94:6b:20:fa:8d:cf:a5:[email protected]

The key's randomart image is:

+--[ RSA 2048]----+

|       +B+*     |

|     . +oO .    |

|    . ..+.o     |

|   .  . .o      |

|    . oS.       |

|     o . .      |

将test 使用者産生的龔玥拷貝到用戶端伺服器上面

ssh-copy-id -i [email protected]

ssh [email protected] #測試不需要輸入密碼即可成功

安裝inotify

下載下傳軟體:

wget https://sourceforge.net/projects/inotify-tools/files/latest/download/inotify-tools-3.14.tar.gz

編譯安裝:

tar zxvf  inotify-tools-3.14.tar.gz && cd inotify-tools-3.14

./configure

make && make install

配置

配置inotify 

ll  /proc/sys/fs/inotify/# 檢視是否有以下資訊輸出,如果沒有表示系統核心不支援

total 0

-rw-r--r-- 1 root root 0 Oct 27 16:28max_queued_events

-rw-r--r-- 1 root root 0 Oct 27 16:28max_user_instances

-rw-r--r-- 1 root root 0 Oct 27 16:28max_user_watches 

參數說明

max_queued_events   #表示監控事件隊列

max_user_instances  #表示最多監控執行個體數

max_user_watches   #表示每個執行個體最多監控檔案數

注:當要監控的目錄、檔案數量較多或者變化較頻繁時,要加大這三個參數的值。

例如:可直接修改/etc/sysctl.conf配置檔案,将管理隊列設為32768,執行個體數設為1024,監控數設為9000000(建議大于監控目标的總檔案數)

cat /etc/sysctl.conf

net.ipv4.conf.all.accept_redirects=0

fs.inotify.max_queued_events = 32768

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 90000000

inotify常用參數:

-e  用來指定要監控哪些事件。

這些事件包括: create建立,move移動,delete删除,modify修改檔案内容,attrib屬性更改。

-m 表示持續監控

-r  表示遞歸整個目錄

-q 表示簡化輸出資訊。

測試

使用inotifywait指令監控 服務端 /home/test/htmll發生的變化。然後在另一個終端向/home/test/html目錄下添加檔案、移動檔案,檢視螢幕輸出結果。

1 号終端輸入:inotifywait-mrq -e modify,delete,create,attrib /home/test/html/

2 号終端輸入: cd /home/test/html/ && touch1.HTML

1 号終端顯示2号終端的監控事件

/home/test/html/ CREATE 1.HTML

/home/test/html/ ATTRIB 1.HTML

執行個體示範:

将服務端的/home/test/html/ 檔案目錄實時同步到用戶端,使用實時同步操作前需要,配置test 使用者的ssh 免登陸,上面已做配置,在伺服器端執此腳本,伺服器端的資料就好自動同步到用戶端。參考腳本如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<code>#!/bin/bash</code>

<code># ---------------------------------------------------------------------------------------------------------</code>

<code># Filename:    rsync_ inotifywait.sh</code>

<code># Date:      2017/10/27</code>

<code># Author:     http://sdsca.blog.51cto.com/10852974/1976974</code>

<code># Description: Based on Centos 7.2</code>

<code>#</code>

<code># Usage:      rsync_ inotifywait.sh</code>

<code># Version:     1.0</code>

<code># ------------------------------------------</code>

<code>Src=</code><code>/home/test/html</code>

<code>Dst=</code><code>test</code><code>@192.168.10.181:</code><code>/home/test/</code>

<code>T=`</code><code>date</code><code>+%Y-%m-%d-%H:%M:%S`</code>

<code>log=</code><code>/home/test/log/rsync</code><code>.log</code>

<code> </code> 

<code>/usr/local/bin/inotifywait-mrq</code> <code>-e modify,delete,create,attrib ${Src} | </code><code>while</code> <code>read</code> <code>files   </code>

<code>        </code><code>do</code>

<code>              </code><code>/usr/bin/rsync</code> <code>-ahqzt --delete$Src $Dst</code>

<code>        </code><code>echo</code> <code>"`date +'%F %T'` 出現事件 $files"</code> <code>&gt;&gt;${log}2&gt;&amp;1</code>

<code>        </code><code>done</code>

 本文轉自 水滴石川1 51CTO部落格,原文連結:http://blog.51cto.com/sdsca/1976974,如需轉載請自行聯系原作者

繼續閱讀