centos Unison+Inotify雙向同步
阿新 • • 發佈:2019-05-16
https://blog.51cto.com/wupengfei/1958257
Unison簡介
Unison是windows和unix平臺下都可以使用的檔案同步工具,它能使兩個資料夾(本地或網路上的)保持內容的一致。unison擁有其它一些同步工具或檔案系統的相同特性,但也有自己的特點:
1.跨平臺使用;
2.對核心和使用者許可權沒有特別要求;
3. unison是雙向的,它能自動處理兩分拷貝中更新沒有衝突的部分,有衝突的部分將會顯示出來讓使用者選擇更新策略;
4.只要是能連通的兩臺主機,就可以執行unison,可以直接使用socket連線或安全的ssh連線方式,對頻寬的要求不高,使用類似rsync的壓縮傳輸協議
unison各種版本下載地址:
http://www.seas.upenn.edu/~bcpierce/unison//download.html
unison編譯器下載地址:
http://caml.inria.fr/download.en.html
inotify下載地址
http://inotify-tools.sourceforge.net/
一、編譯安裝unison
1、#編譯安裝ocaml
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://caml.inria.fr/pub/distrib/ocaml-4.03/ocaml-4.03.0.tar.gz
[root@104 src]# tar xf ocaml-4.03.0.tar.gz
[root@104 src]# cd ocaml-4.03.0
[root@104 ocaml-4.03.0]# ./configure
[root@104 ocaml-4.03.0]# make world opt
[root@104 ocaml-4.03.0]# make install
2、#編譯安裝Unison
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
[root@104 src]# tar xf unison-2.48.4.tar.gz
[root@104 src]# cd src/
[root@104 src]# yum install -y ctags-etags glibc-static
[root@104 src]# make UISTYLE=text THREADS=true STATIC=true
#使用 ”UISTYLE=text THREADS=true STATIC=true“ 表示使用命令列方式,加入執行緒支援,以靜態模式編譯
#在執行完上面的命令後,會在當前目錄下生成可執行檔案unison,將其複製到系統的PATH路徑即可
[root@104 src]# cp unison /usr/local/bin/
二、配置雙機ssh信任
unison同步遠端目錄時要登入到遠端伺服器,配置互相信任
1,在倆臺機器上建立RSA金鑰
(1)以root使用者登入
(2)在root使用者的主目錄內建立.ssh目錄並設定正確的許可權
[root@104 ~]# mkdir ~/.ssh
[root@104 ~]# chmod -R 700 ~/.ssh
(3)使用sshh-keygen命令生產RSA金鑰
[root@104 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
75:e4:e8:90:47:3b:c3:60:7a:6d:85:a0:45:db:c1:9e root@104
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
| .*oo.o |
| = OoB |
| o =.%.o |
| . *E+ |
| S . |
| |
| |
| |
| |
+-----------------+
2、#新增金鑰到授權金鑰檔案中
(1)以root使用者登入
(2)在本機執行:
[root@104 ~]# cd ~/.ssh
[root@104 .ssh]# ssh "-p 22" 192.168.9.104 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@104 .ssh]# ssh "-p 22" 192.168.9.106 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@104 .ssh]# scp authorized_keys 192.168.9.106:/root/.ssh/
[email protected]'s password:
authorized_keys 100% 780 0.8KB/s 00:00
[root@104 .ssh]# chmod 600 /root/.ssh/authorized_keys
(3)在106機器上執行
[root@106 ~]# chmod 600 /root/.ssh/authorized_keys
(4)驗證測試
[root@104 .ssh]# ssh 192.168.9.104 date
Wed Aug 31 12:10:27 CST 2016
[root@104 .ssh]# ssh 192.168.9.106 date
Wed Aug 31 12:10:46 CST 2016
[root@106 ~]# ssh 192.168.9.104 date
The authenticity of host '192.168.9.104 (192.168.9.104)' can't be established.
RSA key fingerprint is 57:83:da:b9:ee:de:2a:1f:f1:74:da:ec:43:fa:7b:56.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.9.104' (RSA) to the list of known hosts.
Wed Aug 31 12:11:10 CST 2016
[root@106 ~]# ssh 192.168.9.106 date
The authenticity of host '192.168.9.106 (192.168.9.106)' can't be established.
RSA key fingerprint is 67:ff:e7:b0:5f:3b:46:e9:d7:8d:e1:10:21:02:8b:ce.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.9.106' (RSA) to the list of known hosts.
Wed Aug 31 12:11:30 CST 2016
#不需要輸入密碼就能顯示系統日期,說明ssh互相信任配置成功
三、編譯安裝inotify
#inotify特性需要Linux核心的支援,安裝inotify-tools確認系統核心為2.6.13版本以上,
[root@104 ~]# uname -r
2.6.32-431.el6.x86_64
[root@104 ~]# ls -l /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_queued_events
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_user_instances
-rw-r--r-- 1 root root 0 Aug 30 12:28 max_user_watches
如果有上面三項輸出,表示系統已經預設支援inotify,接著就可以開始安裝inotify-tools了。
[root@104 ~]# cd /usr/local/src/
[root@104 src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@104 src]# tar xf inotify-tools-3.14.tar.gz
[root@104 src]# cd inotify-tools-3.14
[root@104 inotify-tools-3.14]# ./configure
[root@104 inotify-tools-3.14]# make
[root@104 inotify-tools-3.14]# make install
[root@104 inotify-tools-3.14]# ll /usr/local/bin/inotifywa*
-rwxr-xr-x 1 root root 44287 Aug 31 12:23 /usr/local/bin/inotifywait
-rwxr-xr-x 1 root root 41377 Aug 31 12:23 /usr/local/bin/inotifywatch
#inotify-tools安裝完成後,會生成inotifywait和inotifywatch兩個指令,其中,inotifywait用於等待檔案或檔案集上的一個特定事件,它可以監控任何檔案和目錄設定,並且可以遞迴地監控整個目錄樹。
inotifywatch用於收集被監控的檔案系統統計資料,包括每個inotify事件發生多少次等資訊。
四,配置unison,測試同步zabbix的php目錄
#104編寫指令碼
[root@104 ~]# vim /data0/inotify.sh
#/bin/bash
ip2="192.168.9.106"
src2="/data0/a/"
dst2="/data0/a/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read line; do
/usr/local/bin/unison -batch $src2 ssh://$ip2/$dst2
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
[root@104 ~]# chmod +x /data0/inotify.sh
[root@104 ~]# nohup /data0/inotify.sh &
#106編寫指令碼
[root@106 ~]# vim /data0/inotify.sh
#/bin/bash
ip1="192.168.9.104"
src1="/data0/a/"
dst1="/data0/a/"
/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src1 | while read line; do
/usr/local/bin/unison -batch $src1 ssh://$ip1/$dst1
echo -n "$line " >> /var/log/inotify.log
echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done
[root@106 ~]# chmod +x /data0/inotify.sh
[root@106 ~]# nohup /data0/inotify.sh &