https://blog.51cto.com/wupengfei/1958257php
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/ 1、編譯安裝unison 一、#編譯安裝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 二、#編譯安裝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/ 2、配置雙機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 . | | | | | | | | | +-----------------+ 二、#添加密鑰到受權密鑰文件中 (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/ root@192.168.9.106'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互相信任配置成功 3、編譯安裝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 & 5、測試