Unison + inotify implement real-time bidirectional data synchronization

Source: Internet
Author: User
Tags inotify rsync ocaml

Unison + inotify implement real-time bidirectional data synchronization

unisonIt 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,unisonTwo-way synchronization is supported. You can synchronize data from A to B or from B to A without additional settings.

inotifyIs a Linux kernel feature that monitors the file system and sends event warnings to specialized applications in a timely manner, such as delete, read, write, and uninstall operations. You can also track the source and target details of an activity. Use inotifySimple: create a file descriptor, attach one or more monitors (one monitor is a path and a group of events), and then use the read method to get events from the descriptor. Read does not use up the entire cycle. It is blocked before the event occurs. Environment Introduction servers:
Server node1: 192.168.5.10, synchronization Directory:/opt/http/Server node2: 192.168.5.20, synchronization Directory:/opt/http/
Configure the epel Source
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
1. install the software on node1 and node2 (1) install ocaml
[root@node1 ~]# yum install  ocaml -y
(2) install unison
[root@node1 ~]# yum install  unison240.x86_64 -y
(3) install inotify
[root@node1 ~]# yum install  inotify-tools.x86_64 -y
2. For details about how to configure the two-host ssh Trust, see (1) execute the ssh-keygen command under the root user of node1. Press ENTER wherever necessary, generate a certificate that establishes a security trust relationship.
[root@node1 ~]# ssh-keygen -t rsa
Node1 establishes a trust relationship with node2.
[root@node1 ~]# chmod 600 /root/.ssh/authorized_keys[root@node1 ~]# scp -r /root/.ssh/id_rsa.pub 192.168.5.20:/root/.ssh/authorized_keys
(2) perform the same operation on node2 to Establish a trust relationship between node2 and node1. (3) run the command remotely to test whether the command is successful. For the first time, enter the PASSWORD Command Format: ssh remote username @ Remote Host IP address 'remote command or script'
[root@node2 ~]# ssh root@192.168.5.10 'hostname'

3. Add a script (1) on node1 of 192.168.5.10 Server:
[root@node1 ~]# mkdir /script
[root@node2 ~]# vim /script/inotify.sh
#/Bin/bashUNISON = 'ps-ef | grep-v grep | grep-c inotifywait 'if [$ {UNISON}-lt 1] then ip2 = "192.168.5.20" # target host IP src2 = "/opt/http/" dst2 = "/opt/http/"/usr/bin/inotifywait-mrq-e create, delete, modify, move $ src2 | while read linedo/usr/bin/unison-batch $ src2 ssh: // $ ip2/$ dst2 echo-n "$ line">/var/log/inotify $ (date + % u ). log echo 'date + % F % T ""-f1-4 '>/var/log/inotify $ (date + % u ). logdonefi
(2) Add the script on the 192.168.5.20 server node2:
[root@node2 ~]# mkdir /script
[root@node2 ~]# vim /script/inotify.sh
#/bin/bashUNISON=`ps -ef |grep -v grep|grep -c inotifywait`if [ ${UNISON} -lt 1 ]then    ip2="192.168.5.10"    src2="/opt/http/"    dst2="/opt/http/"    /usr/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read linedo    /usr/bin/unison -batch $src2 ssh://$ip2/$dst2    echo -n "$line " >> /var/log/inotify/inotify$(date +%u).log    echo ` date +%F %T " " -f1-4` >> /var/log/inotify/inotify$(date +%u).logdonefi
4. Modify the script permission on two servers:
[root@node1 ~]# chmod a+x /script/inotify.sh
5. Create a log directory. If the/var/log/inotify directory does not exist, manually create the directory to store inotify logs;
[root@node2 ~]# mkdir /var/log/inotify
6. Add a scheduled task
[root@node2 ~]# crontab -e* * * * * /bin/bash /script/inotify.sh > /dev/null 2>&1 &

Restart the computer and test whether/opt/http/content in the two servers can be synchronized without restarting the computer. You can also manually execute the script to test/bin/bash/script/inotify. sh or/script/inotify. sh can be added, modified, or deleted in one of the/opt/http/directories. The Script status is displayed, and the other server should follow the operation. 7. If necessary, you can specify that files with certain suffixes cannot be opened simultaneously/root/. unison/default. prf, and add:
ignore = Name *.txtignore = Name *.mdb
This means that the txt and mdb files are not synchronized. After modification, run/bin/sh/script/inotify. sh again to make the changes take effect. I directly specify the name:
ignore = Name .bash_profileignore = Name .viminfoignore = Name .bash_history
After completing the above steps, there will basically be No problem. If there is a problem, proceed to the next step. 8. If there is a problem, manually modify the Script: In some companies, disabling root remote login is prohibited, only common users can use the synchronization scheme to wait for the synchronization directory (two computers cannot use the same name as the directory to be synchronized; otherwise, an error is reported)
192.168.5.10: /opt/http/192.168.5.20: /opt/http/
The installation process is as follows:
Useradd-g apache unisonpasswd unison (enter the new password) chown-R unison. /opt/http/mkdir/home/unison /. sshchmod 700/home/unison /. sshsu-unisonssh-keygen-t rsa (and press enter three times in a row) to add the key to the authorization key file.
Operate on node1 of 192.168.5.10 server (22 is the port number)
Cd/home/unison /. sshssh "-p 22" 192.168.5.10 cat/home/unison /. ssh/id_rsa.pub> authorized_keys # lower case pssh "-p 22" 192.168.5.20 cat/home/unison /. ssh/id_rsa.pub> authorized_keyemd-P 22 authorized_keys 192.168.5.20:/home/unison /. ssh/# uppercase Pchmod 600/home/unison /. ssh/authorized_keys
Operate on 192.168.5.20 server B
chmod 600 /home/unison/.ssh/authorized_keys
Perform the following tests on the two machines respectively (the password is required for the first execution, and the trust is successful for subsequent execution)
ssh -p 22 unison@192.168.5.10 datessh -p 22 unison@192.168.5.20 datesu - root
Node1 script
 #/bin/bashUNISON=`ps -ef |grep -v grep|grep -c inotifywait`if [ ${UNISON} -lt 1 ]thenip2="unison@192.168.5.20:22"src2="/opt/http/"dst2="/opt/http/"/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read linedo/usr/local/bin/unison -batch -sshargs "-i /home/unison/.ssh/id_rsa" $src2 ssh://$ip2/$dst2echo -n "$line " >> /var/umelook-log/inotify/inotify$(date +%u).logecho ` date +%F %T` >> /var/umelook-log/inotify/inotify$(date +%u).logdonefi
Node2 script:
 #/bin/bashUNISON=`ps -ef |grep -v grep|grep -c inotifywait`if [ ${UNISON} -lt 1 ]thenip2="unison@192.168.5.10:22"src2="/opt/http/"dst2="/opt/http/"/usr/local/bin/inotifywait -mrq -e create,delete,modify,move $src2 | while read linedo/usr/local/bin/unison -batch -sshargs "-i /home/unison/.ssh/id_rsa" $src2 ssh://$ip2/$dst2echo -n "$line " >> /var/umelook-log/inotify/inotify$(date +%u).logecho ` date +%F %T` >> /var/umelook-log/inotify/inotify$(date +%u).logdonefi

CentOS 6.5 rsync + inotify for Real-time Data Synchronization and backup

Rsync + inotify for Real-time Data Synchronization

Rsync + inotify for real-time file synchronization between servers

Rsync combined with Inotify Real-Time Synchronization Configuration

RSync for Data Backup

Inotify + rsync for Real-time Data Synchronization

Rsync + inotify Real-time Data Backup

Rsync + inotify for Automatic Data Synchronization

Use rsync for Real-time Data Synchronization and backup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.