歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Centos 6.3下rsync+inotify的安裝配置

Centos 6.3下rsync+inotify的安裝配置

日期:2017/3/3 16:16:22   编辑:關於Linux

什麼是inotify?

inotify是一種強大的,細粒度的,異步文件系統時間監控機制,它可以替代crond實現與rsync的觸發式文件同步,從而監控文件系統中添加,刪除,修改,移動等細粒事件,從LINUX 2.6.13起,就已加入了對inotify的支持,所以我們只需要安裝一個第三方軟件inotify-tools即可管理此服務。

之前利用的rsync+crond來觸發實現同步的瓶頸在於,rsync在同步數據時,需要先掃描所有文件後進行比對,而後進行差異傳輸,如果文件數量級別很大而且變化會很快,掃描所有文件會非常耗時,而且會存在漏同步的問題,造成效率低下。

而rsync+inotify則會彌補前者先掃描後同步的效率問題,采用系統級別監控各種變化,當文件發生任何變化,就會觸發rsync同步,解決效率與實時性問題。

LINUX操作系統: centOS6.3 64bit

rsync: 系統自帶

inotify-tools: inotify-tools-master

www1(rsync server):192.168.7.73

www2(rsync client):192.168.7.74

拓撲圖:

(server)表示僅服務端配置

(client)表示僅客戶端配置

(server,client)表示客戶端與服務端都需配置

環境搭建:(server,client)

1.關閉iptables和SELINUX

# service iptables stop

# setenforce 0

# vi /etc/sysconfig/selinux

---------------

SELINUX=disabled

---------------

判斷LINUX系統內核是否達到2.6.13以上:

# uname -a

-------------

Linux www1.example.com 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

-------------

查看inotify目錄是否存在:

# ls -lsart /proc/sys/fs/inotify/

------------------

總用量 0

0 dr-xr-xr-x 0 root root 0 6月 4 14:04 ..

0 dr-xr-xr-x 0 root root 0 6月 4 17:35 .

0 -rw-r--r-- 1 root root 0 6月 4 17:35 max_user_watches

0 -rw-r--r-- 1 root root 0 6月 4 17:35 max_user_instances

0 -rw-r--r-- 1 root root 0 6月 4 17:35 max_queued_events

------------------

若返回以上內容,則系統支持inotify.

一.安裝rsync:(server,client)

傳送門:http://showerlee.blog.51cto.com/2047005/1132724

配置:(server)

# vi /etc/rsyncd.conf

--------------------

uid = root

gid = root

use chroot = no

max connections = 10

strict modes = yes

port = 873

address = 192.168.7.73

[test]

path = /test

comment = mirror for test

ignore errors

read only = no

list = no

auth users = user

secrets file = /etc/rsync.pas

hosts allow = *

# hosts deny = 0.0.0.0/0

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

--------------------

啟動rsync

# rsync --daemon --config=/etc/rsyncd.conf

重啟xinetd使其配置生效:

# /etc/init.d/xinetd restart

二.安裝inotify-tools:(server,client)

可以到https://github.com/rvoicilas/inotify-tools/下載zip包,然後傳到系統進行編譯安裝:

# unzip inotify-tools-master.zip

# cd inotify-tools-master

# ./autogen.sh

# ./configure --prefix=/usr/local/inotify

# make && make install

配置client端的inotify:(client)

# vi /etc/rc.d/inotify.sh

該腳本在做客戶端目錄下文件若發生變化,則向服務端做同步上傳操作,也就是保持客戶端目錄文件發生變化,服務端也相應改變。

------------------

#!/bin/bash

src=/test

des=test

ip=192.168.7.73

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%T%w%f' -e modify,delete,create,attrib $src | while read file

do

rsync -vzrtopg --delete --progress $src user@$ip::$des --password-file=/etc/rsync.pas &&

echo "$src has been resynced"

done

------------------

 

賦予執行權限

# chmod +x /etc/rc.d/inotify.sh

執行腳本並做開機啟動:

# /etc/rc.d/inotify.sh

# echo "/etc/rc.d/inotify.sh" >> /etc/rc.local

注:這個腳本的作用是通過inotify監控文件目錄的變化,進而觸發rsync進行同步操作,由於這是通過內核完成的主動式觸發操作,所以比rsync遍歷整個目錄的掃描方式效率要高很多。

驗證:

在客戶端創建5個文件,到服務端查看文件是否實時同步?

(client)

# cd /test

# touch 1 2 3 4 5

(server)

# cd /test

# ls

-------------

1 2 3 4 5

-------------

驗證成功,client端的目錄發生變化會實時同步到server端,類似一個網絡raid-1

總結:

rsync+inotify比較適用於輕量級文件即時同步,如果量大建議還是使用共享存儲方法解決。

參考:酒哥的構建高可用LINUX服務器(第2版)

Copyright © Linux教程網 All Rights Reserved