1. 程式人生 > >inotify+rsync實現實時同步並郵件通知

inotify+rsync實現實時同步並郵件通知

代碼 moni 創建目錄 存在 echo ets selinux 5.0 from

服務器之間文件實時同步,監控文件的變化,發送郵件通知,並實時同步文件。

由於人工同步多臺服務器的文件比較吃力,可以借助這樣一套軟件,自動化的實現這樣的工作。

並且可以事實監控變化發送郵件給系統管理人員。

服務器的架構圖:

技術分享圖片

文件源服務器:10.0.0.20

需要同步的目標服務器:10.0.0.50 10.0.0.60 10.0.0.70

一.目標服務器配置

1.關閉selinux

vi /etc/selinux/config #編輯防火墻配置文件

#SELINUX=enforcing #註釋掉

#SELINUXTYPE=targeted #註釋掉

SELINUX=disabled #增加

:wq
! #保存,退出 setenforce 0 #立即生效

2.如果生產環境防火墻不關閉就開放rsync的TCP端口

vi /etc/sysconfig/iptables #編輯防火墻配置文件

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT


/etc/init.d/iptables restart #最後重啟防火墻使配置生效

3、安裝Rsync服務端軟件

yum install rsync xinetd #安裝

vi /etc/xinetd.d/rsync #編輯配置文件,設置開機啟動rsync

disable = no #修改為no

4、創建rsyncd.conf配置文件

[root@master ~]# cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 100
timeout = 600
pid file=/var/run/rsyucd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[benjamindata]
path = /home/benjamindata
ignore errors
read only = no
list = no

hosts allow = 10.0.0.20/255.255.255.0
auth users = benjamin
secrets file = /etc/rsync.pass

5.創建用戶認證文件

[root@manager ~]# cat /etc/rsync.pass 
benjamin:123

6、設置文件權限

chmod 600 /etc/rsyncd.conf  #設置文件所有者讀取、寫入權限

chmod 600 /etc/rsync.pass  #設置文件所有者讀取、寫入權限

7、啟動rsync

/etc/init.d/xinetd start  #啟動

service xinetd stop   #停止

service xinetd restart #重新啟動

二.源服務器配置

1.關閉selinux

同上

2.防火墻開放873端口

同上

3.修改優化內核參數

vi /etc/sysctl.conf #內核參數修改

fs.inotify.max_queued_events = 16384

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 1048576

sysctl -p #初始化內核參數

參數說明:

max_queued_events:

inotify隊列最大長度,如果值太小,會出現"** Event Queue Overflow **"錯誤,導致監控文件不準確

max_user_watches:

要同步的文件包含多少目錄,可以用:find /home/benjamindata -type d | wc -l 統計,必須保證max_user_watches值大於統計結果(這裏/home/benjamindata為同步文件目錄)

max_user_instances:

每個用戶創建inotify實例最大值

4.編譯安裝 inotify-tools工具

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xzvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14./configure --prefix=/usr/local/inotify
make
make install

5、設置系統環境變量,添加軟連接

echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh

source /etc/profile.d/inotify.sh  #使設置立即生效

echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf

ln -s /usr/local/inotify/include  /usr/include/inotify

6、創建腳本,實時觸發rsync進行同步

vi /usr/local/inotify/rsync.sh   #編輯,添加以下代碼

======================================

#!/bin/sh

srcdir=/home/benjamindata/

dstdir=benjamindata

excludedir=/usr/local/inotify/exclude.list

rsyncuser=benjamin

rsyncpassdir=/etc/passwd.txt

dstip="10.0.0.50 10.0.0.60 10.0.0.70"

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir

done

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

do

for ip in $dstip

do

rsync -avH --port=873 --progress --delete  --exclude-from=$excludedir  $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir

echo "  ${file} was rsynced" >> /tmp/rsync.log 2>&1

done

done

======================================

chmod +x /usr/local/inotify/rsync.sh   #添加腳本執行權限

腳本參數說明:

srcdir=/home/benjamindata/  #源服務器同步目錄

dstdir=benjamindata    #目標服務器rsync同步目錄模塊名稱

excludedir=/usr/local/inotify/exclude.list   

#不需要同步的目錄,如果有多個,每一行寫一個目錄,使用相對於同步模塊的路徑;

#例如:不需要同步/home/benjamin/目錄下的a目錄和b目錄下面的b1目錄,exclude.list文件可以這樣寫

a/
b/b1/

rsyncuser=benjamin  #目標服務器rsync同步用戶名

rsyncpassdir=/etc/passwd.txt  #目標服務器rsync同步用戶的密碼在源服務器的存放路徑

dstip="10.0.0.50 10.0.0.60 10.0.0.70"  #目標服務器ip,多個ip用空格分開

/tmp/rsync.log  #腳本運行日誌記錄

6、設置腳本開機自動執行

vi /etc/rc.d/rc.local  #編輯,在最後添加一行

sh /usr/local/inotify/rsync.sh & #設置開機自動在後臺運行腳本

7、測試inotify實時觸發rsync同步腳本是否正常運行

在源服務器10.0.0.20上創建目錄inotify_test

mkdir /home/benjamindata/inotify_test

#重新啟動源服務器:10.0.0.20

等系統啟動之後,查看三臺目標服務器10.0.0.50,10.0.0.60,10.0.0.70的/home/benjamindata下是否有inotify_test文件夾

#手動輸命令指定一個主機測試
[root@manager ~]# rsync -avH --port=873 --progress --delete --exclude-from=/usr/local/inotify/exclude.list /home/benjamindata/ benjamin@10.0.0.50::benjamindata
--password-file=/etc/passwd.txt sending incremental file list ./ inotify_test/ sent 76 bytes received 15 bytes 182.00 bytes/sec total size is 0 speedup is 0.00

# 使用腳本批量執行。

[root@manager ~]# bash /usr/local/inotify/rsync.sh
sending incremental file list


sent 70 bytes received 9 bytes 52.67 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
./
inotify_test/


sent 76 bytes received 15 bytes 182.00 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
./
inotify_test/


sent 76 bytes received 15 bytes 182.00 bytes/sec
total size is 0 speedup is 0.00

 

在目標服務器查看

[root@master ~]# ifconfig |awk -F [ :]+ NR==2{print $4}
10.0.0.50
[root@master ~]# ll /home/benjamindata/                    
total 4
drwxr-xr-x 2 root root 4096 Mar 12 14:22 inotify_test

三.配置安裝mutt+msmtp 發送郵件

1.安裝

分別下載解壓msmtp  mutt
[root@manager ~]  wget http://downloads.openwrt.org.cn/sources/msmtp-1.4.17.tar.bz2
[root@manager ~]# tar xf msmtp-1.4.17.tar.bz2
[root@manager msmtp-1.4.17]# cd msmtp-1.4.17
[root@manager msmtp-1.4.17]# ./configure --prefix=/usr/local/nsmtp

[root@manager msmtp-1.4.17]# make && make install
# mutt安裝如上
[root@manager ~]# wget http://downloads.openwrt.org.cn/sources/mutt-1.5.20.tar.gz
執行 configure 腳本時報configure: error: no curses library found 錯誤
[root@manager mutt-1.5.20]# yum install -y  ncurses-devel

2.修改配置文件

[root@manager mutt-1.5.20]# cat /root/.msmtprc

  account default
  port 25
  host smtp.163.com
  tls off
  auth plain
  from [email protected]
  user 13311802282
  password xxxxx
  logfile /var/log/msmtp.log

 
[root@manager ~]# vi /usr/local/msmtp/etc/msmtprc 
account default host smtp.
163.com from 13311802282@163.com auth login port 25 tls off user 13311802282 password xxxx logfile /usr/local/msmtp/log/msmtp.log

[root@manager ~] chomod 400 
vi /root/.muttrc 
set sendmail="/usr/local/msmtp/bin/msmtp" set use_from=yes set from=13311802282@163.com set envelope_from=yes
[root@manager ~]# vi /etc/Muttrc
set sendmail="/usr/local/msmtp/bin/msmtp" set use_from=yes set from="[email protected]" set realname="[email protected]" set editor="vim"

測試發送郵件,自己發給自己

[root@manager ~]# /usr/local/mutt/bin/mutt -s "test" -c  13311802282@163.com < /tmp/mailtest.txt 

技術分享圖片

監控目錄變化腳本

[root@manager ~]# cat mon.sh 
#!/bin/bash

src=/home/benjamindata
dstip="10.0.0.50 10.0.0.60 10.0.0.70"
/usr/local/inotify/bin/inotifywait -m -r -d -o /tmp/monitor.log --timefmt %F%T --format %T%w%f%e -e modify,attrib,move,close_write,create,delete,delete_self $src| while read DIRECTORY EVENT FILE

do

for ip in $detip
do
rsync -vzrtopg --delete --progress  benjamin@$ip::static --password-file=/etc/passwd.txt

echo "${files} was rsynced" >> /tmp/rsync.log 2>&1

done

done

#參數說明

-m, 即--monitor,表示始終保持事件監聽狀態。


-r, 即--recursive,表示遞歸查詢目錄。


-q, 即--quiet,表示打印出監控事件。


-e, 即--event,通過此參數可以指定要監控的事件,常見的事件有modify、delete、create、attrib等


--timefmt:指定時間的輸出格式


--format:指定變化文件的詳細信息

 

# 一旦發現文件改變會生成日誌

發送郵件腳本

[root@manager ~]# cat sendmail.sh 
#!/bin/bash

clear

path_f=/tmp/monitor.log

email=13311802282@163.com



function mutt_send()

{

/usr/local/mutt/bin/mutt -s "WARN" -c ${email}</tmp/monitor.log

}

if [ -s $path_f ]; then

mutt_send

echo "mail send.......";sleep 1

/usr/local/mutt/bin/mutt -s "WARN" -c ${email}</tmp/monitor.log

fi

cat /dev/null > $path_f

測試

 cd /home/benjamindata/
touch 123
# 由於定時任務要5分鐘才輪詢執行一次腳本,我手動觸發腳本執行。這個腳本就是檢查 monitor.log是否存在內容來判斷是否發送郵件,當每次腳本執行以後會清空該日誌內容。
sh sendmail.sh

[root@manager ~]# rm -fr /home/benjamindata/*

[root@manager ~]# cat /tmp/monitor.log

  2018-03-1218:27:41/home/benjamindata/123DELETE
  2018-03-1218:27:41/home/benjamindata/inotify_testDELETE,ISDIR
  2018-03-1218:27:41/home/benjamindata/inotify_test/DELETE_SELF

技術分享圖片

這裏就結束了。

inotify+rsync實現實時同步並郵件通知