Inotify + unison implement bidirectional Real-time Data Synchronization
A new project was created a few days ago. The website architecture is that the front-end uses nginx to reverse proxy the two backend tomcat servers. At this time, the data between the two tomcat servers is synchronized in real time, which becomes a problem? I have searched for many solutions on the Internet. There are several methods for file synchronization under liux: 1. nfs for web data sharing 2. rsync + inotify for web Data Synchronization 3. rsync + sersync for faster and more resource-saving web Data Synchronization 4. unison + inotify for two-way web Data Synchronization the fourth solution is described in detail here, the first few have their own shortcomings. Only the fourth solution supports bidirectional real-time synchronization. When one of the servers goes down, web access is not affected. (Ps: I have always liked nfs and the configuration is very simple, but it has a fatal drawback that when one of the web Services fails, web pages will be inaccessible ). Unison is a file synchronization tool across windows, linux, and mac OS platforms. It not only supports local synchronization, but also supports synchronization through network protocols such as SSH, RSH, and Socket. Even better, Unison supports two-way synchronization. You can synchronize data from A to B, or from B to A without additional settings. Download and compile and install the three software packages. (Inotify-tools-3.14.tar.gz?ocaml-3.10.2.tar.gz=unison-2.32.52.tar.gz) [root @ vmware1 tools] # wget http://caml.inria.fr/pub/distrib/ocaml-3.10/ocaml-3.10.2.tar.gz [Root @ vmware1 ~] # Mkdir-p/taokey/tools [root @ vmware1 ~] # Cd/taokey/tools/[root @ vmware1 tools] # wget http://caml.inria.fr/pub/distrib/ocaml-3.10/ocaml-3.10.2.tar.gz [Root @ vmware1 tools] # wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/unison-2.32.52/unison-2.32.52.tar.gz [Root @ vmware1 tools] # wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [Root @ vmware1 tools] # tar xf inotify-tools-3.14.tar.gz [root @ vmware1 tools] # tar xf ocaml-3.10.2.tar.gz [root @ vmware1 tools] # ll total usage 3772 drwxrwxrwx 5 1000 1000 March 14 4096 inotify-tools-3.14-rw-r -- r -- 1 root 358772 March 14 2010 inotify-tools-3.14.tar.gzdrwxrwxr-x 26 root wheel 4096 February 29 2008 ocaml-3.10.2-rw-r -- r -- 1 root 2785669 February 29 2008 ocaml-3.10.2.tar.gzdrwxr-xr-x 7 501 wheel 4096 June 30 2009 unison-2.32.52-rw-r -- r -- 1 root 697866 June 30 2009 unison-2.32.52.tar.gz compilation install inotify, need to install gcc compiler, otherwise inotify compilation reports an error. [Root @ vmware1 tools] # cd inotify-tools-3.14 [root @ vmware1 inotify-tools-3.14] #. /configure -- prefix =/usr/local/inotify & make install modify PATH environment variable [root @ vmware1 inotify-tools-3.14] # cd/usr/local/inotify/[root @ vmware1 inotify] # echo "PATH =/usr/local/inotify/bin: $ PATH ">/etc/profile. d/inotify. sh [root @ vmware1 inotify] # source/etc/profile. d/inotify. sh: add the library file to the system identification path [root @ vmware1 inotify] # echo "/Usr/local/inotify/lib">/etc/ld. so. conf. d/inotify. conf [root @ vmware1 inotify] # ldconfig-v | grep inotify/usr/local/inotify/lib: libinotifytools. so.0-> libinotifytools. so.0.4.1 link the library file to the system identification path [root @ vmware1 inotify] # ln-sv/usr/local/inotify/include // usr/include/inotify "/usr/include/ inotify "->"/usr/local/inotify/include/"compile and install ocaml, unison depends on ocaml [root @ vmware1 ~] # Cd/taokey/tools/ocaml-3.10.2 [root @ vmware1 ocaml-3.10.2] #. /configure [root @ vmware1 ocaml-3.10.2] # make world opt [root @ vmware1 ocaml-3.10.2] # make install compile and install unison, install dependency package [root @ vmware1 ocaml-3.10.2] # cd .. /unison-2.32.52 [root @ vmware1 unison-2.32.52] # yum-y install ctags-etags [root @ vmware1 unison-2.32.52] # make UISTYLE = text THREADS = true STATIC = true # UISTYLE = text THREADS = true STATIC = true: use the command Add thread support, compile in static mode # make install will prompt an error, this error is to ask you cp unison/usr/local/bin, copy it to [root @ vmware1 unison-2.32.52] # make installmv/root/bin // unison/tmp/unison-10576mv: unable to get the file status of "/root/bin // unison" (stat): no file or directory make: [doinstall] Error 1 (ignore) cp unison/root/bin/cp: cannot create common file "/root/bin/": is a directory make: * ** [doinstall] Error 1 [root @ vmware1 unison-2.32.52] # cp unison/usr/local/bin 2. install the preceding three software packages on the vmware2 server 192.168.1.12. Repeat the preceding steps for installation. 3. the ssh key pair is generated on the server vmware1 (192.168.1.11) and the server vmware2 (192.168.1.12) respectively to implement key-free login. 192.168.1.11: [root @ vmware1 ~] # Cd. ssh/[root @ vmware1. ssh] # touch authorized_keys [root @ vmware1. ssh] # cat id_rsa.pub> authorized_keys [root @ vmware1. ssh] # scp authorized_keys 192.168.1.12:/root /. ssh 192.168.12: [root @ vmware2 ~] # Cd. ssh/[root @ vmware2. ssh] # cat id_rsa.pub ssh-rsa protocol + protocol/w1mJMMTrpgK + dZd7nOTmIx7QjIwGb5G31G + y71_lgvptr/protocol + protocol/protocol 7f6v/5vui5u8GPlO5k + aAKQ = root @ vmware2 copy id_rsa.pub on the vmware2 server to the authorized_keys file on the vmware1 server. [Root @ vmware1. ssh] # cat authorized_keys ssh-rsa kernel + kernel/w1mJMMTrpgK + dZd7nOTmIx7QjIwGb5G31G + y71_lgvptr/kernel + kernel/ 5vui5u8GPlO5k + aAKQ = root @ vmware2 after completion, test whether the key-free login is allowed. [Root @ vmware1 ~] # Ssh 192.168.1.12reverse mapping checking getaddrinfo for bogon [192.168.1.12] failed-possible break-in attempt! Last login: Tue Sep 9 22:03:33 2014 from 192.168.1.104 [root @ vmware2 ~] #4. Create a file directory and write scripts for real-time data synchronization. [Root @ vmware1 ~] # Mkdir-p/var/vm1 [root @ vmware2 ~] # Mkdir-p/var/vm2 compile the unison synchronization script for testing. [Root @ vmware1 ~] # Cat unison-A.sh #/bin/bashipB = "192.168.1.11" srcA = "/var/vm1" dstB = "/var/vm2"/usr/local/inotify/bin/inotifywait-mrq -e create, delete, modify, move $ srcA | while read line; do/usr/local/bin/unison-batch $ srcA ssh: // $ ipB/$ dstBecho-n "$ line">/var/log/inotify. logecho 'date | cut-d ""-f1-4 '>/var/log/inotify. logdone [root @ vmware1 ~] # Chmod o + x unison-A.sh [root @ vmware1 ~] # Sh unison-A.sh & vmware2 on the server: [root @ vmware2 ~] # Cat unison-B.sh #/bin/bashipA = "192.168.1.11" srcB = "/var/vm2" dstA = "/var/vm1"/usr/local/inotify/bin/inotifywait-mrq -e create, delete, modify, move $ srcB | while read line; do/usr/local/bin/unison-batch $ srcB ssh: // $ ipA/$ dstAecho-n "$ line">/var/log/inotify. logecho 'date | cut-d ""-f1-4 '>/var/log/inotify. logdone [root @ vmware2 ~] # Sh unison-B.sh & [1] 10555 [root @ vmware1 ~] # Cd/var/vm1/[root @ vmware1 vm1] # lsa B c test [root @ vmware2 ~] # Cd/var/vm2/[root @ vmware2 vm2] # lsa B c test [root @ vmware2 vm2] # rm-rf test [root @ vmware1 vm1] # lsa B c