Inotify+unison implementation of data bidirectional synchronous backup in Linux

Source: Internet
Author: User
Tags chmod inotify mkdir ssh ocaml

The servers are:
Server a:192.168.1.6, synchronizing directory:/var/www
Server b:192.168.1.7, synchronizing directory:/var/www

Install Unison
First install OCaml, version is at least 3.07 or higher
Download Address: http://caml.inria.fr

The code is as follows Copy Code
Tar xvf ocaml-3.10.2.tar.gz
CD ocaml-3.10.2
./configure
Make World opt
Make install
Cd..

Install Unison
Download Address: www.seas.upenn.edu/~bcpierce/unison/

  code is as follows copy code
tar xvf unison-2.32.52.tar.gz
CD unison-2.32.52
make Uistyle=text threads=true static=true
CP Unison/usr/local/bin
CD..
Note:
Uistyle=text threads=true static=true represents the use of the command-line method, adding thread support to compile
Install inotify
in static mode download address: http:// Inotify-tools.sourceforge.net
Tar xvf inotify-tools-3.14.tar.gz
CD inotify-tools-3.14
./configure
Make
Make install
CD ...

Configure dual-machine SSH Trust (in addition to the following methods, you can also generate a key in a, the. ssh directory on A to the full SCP to B server/root/.ssh, so convenient)
Log in as root user
Create the. SSH directory on Server A
mkdir ~/.ssh
chmod ~/.ssh
Generate RSA Key
SSH-KEYGEN-T RSA
(then return three consecutive times)
Add encryption key to authorization key file
operation on 192.168.1.6 Server A (2222 is the port number)

The code is as follows Copy Code
CD ~/.ssh
SSH "-P 2222" 192.168.1.6 cat/root/.ssh/id_rsa.pub >> Authorized_keys #小写p
SSH "-P 2222" 192.168.1.7 cat/root/.ssh/id_rsa.pub >> Authorized_keys
Scp-p 2222 Authorized_keys 192.168.1.7:/root/.ssh/#大写P
chmod 600/root/.ssh/authorized_keys

Operating on 192.168.1.7 Server B
chmod 600/root/.ssh/authorized_keys
Perform the following tests on both machines (when you first execute, you will be asked to enter a password and you won't need to explain the trust success later)

The code is as follows Copy Code
Ssh-p 2222 192.168.1.6 Date
Ssh-p 2222 192.168.1.7 Date

Add script
To add a script on 192.168.1.6 Server A:

The code is as follows Copy Code
Mkdir/script
vim/script/inotify.sh
#/bin/bash
Unison= ' ps-ef |grep-v grep|grep-c inotifywait '
If [${unison}-LT 1]
Then
Ip2= "192.168.1.7"
Src2= "/var/www/"
Dst2= "/var/www/"
/usr/local/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 2 | While Read line
Todo
/usr/local/bin/unison-batch $src 2 ssh://$ip 2/$DST 2
Echo-n "$line" >>/var/log/inotify/inotify$ (date +%u). log
Echo ' Date +%f%T '-f1-4 ' >>/var/log/inotify/inotify$ (date +%u). log
Done
Fi

To add a script on the 192.168.1.7 server:

  code is as follows copy code
mkdir/script
vim/script/inotify.sh
#/bin/bash
unison= ' ps-ef |grep-v grep|grep-c inotifywait '
If [${unison}-lt 1 ]
Then
ip2= "192.168.1.6"
src2= "/var/www/"
dst2= "/var/www/"
/usr/local/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 2 | While the read line
do
/usr/local/bin/unison-batch $src 2 ssh://$ip 2/$DST 2
Echo-n "$line" >>/var/log/inot ify/inotify$ (date +%u). Log
Echo ' date +%f%T '-f1-4 ' >>/var/log/inotify/inotify$ (date +%u). Log
Done
Fi

To modify script permissions on two servers:
chmod a+x/script/inotify.sh
Add a task to a scheduled task (originally added under/etc/rc.local, but the script does not execute)

The code is as follows Copy Code
Crontab–e
#unison +inotify
* * * * * */bin/sh/script/inotify.sh >/dev/null 2>&1 &

Reboot the computer to test whether the contents of/var/www in two servers can be synchronized

Do not reboot the computer, the manual execution of the script can also test
Sh/script/inotify
When you add, modify, or delete files in one of the/var/www directories, you can see the script state while the other server should follow the action, and if there is a problem, please manually modify the script

In some companies, it is forbidden to disable root remote logging and only use normal users for synchronization scenarios
Waiting for sync directory (two computers can not use the same directory name to be synchronized, otherwise the error)

The code is as follows Copy Code
192.168.1.6:/var/web1
192.168.1.7:/VAR/WEB2

installation process
Useradd–g Apache Unison
passwd Unison
(Enter new password)
Chown–r Unison. /var/www
Mkdir/home/unison/.ssh
chmod 700/home/unison/.ssh
Su–unison
SSH-KEYGEN-T RSA

(then return three consecutive times)

Add encryption key to authorization key file
operation on 192.168.1.6 Server A (2222 is the port number)

The code is as follows Copy Code
Cd/home/unison/.ssh
SSH "-P 2222" 192.168.1.6 cat/home/unison/.ssh/id_rsa.pub >> Authorized_keys #小写p
SSH "-P 2222" 192.168.1.7 Cat/home/unison/.ssh/id_rsa.pub >> Authorized_keys
Scp-p 2222 Authorized_keys 192.168.1.7:/home/unison/.ssh/#大写P
chmod 600/home/unison/.ssh/authorized_keys

Operating on 192.168.1.7 Server B
chmod 600/home/unison/.ssh/authorized_keys


Perform the following tests on both machines (when you first execute, you will be asked to enter a password and you won't need to explain the trust success later)

The code is as follows Copy Code
Ssh-p 2222 unison@192.168.1.6 Date
Ssh-p 2222 unison@192.168.1.7 Date
Su-root

A script:

  code is as follows copy code
#/bin/bash
unison= ' ps-ef |grep-v grep|grep-c inotifywait '
If [${unison}-lt 1]
then
ip2= "unison@192.168.1.7:222 2 "
src2="/var/web1/"
dst2="/var/web2/"
/usr/local/bin/inotifywait-mrq-e create,delete,modify,move $src 2 | While the read line
do
/usr/local/bin/unison-batch-sshargs "-i/home/unison/.ssh/id_rsa" $SRC 2 ssh://$ip 2
/$DST 2
Echo-n "$line" >>/var/umelook-log/inotify/inotify$ (date +%u). Log
Echo ' date +%f%T ' >>/var/umel ook-log/inotify/inotify$ (date +%u). Log
Done
Fi



B Script:

The code is as follows Copy Code
#/bin/bash
Unison= ' ps-ef |grep-v grep|grep-c inotifywait '
If [${unison}-LT 1]
Then
Ip2= "unison@192.168.1.6:2222"
Src2= "/var/web2/"
Dst2= "/var/web1/"
/usr/local/bin/inotifywait-mrq-e Create,delete,modify,move $SRC 2 | While Read line
Todo
/usr/local/bin/unison-batch-sshargs "-i/home/unison/.ssh/id_rsa" $SRC 2 ssh://$ip 2
/$DST 2
Echo-n "$line" >>/var/umelook-log/inotify/inotify$ (date +%u). log
Echo ' Date +%f%T ' >>/var/umelook-log/inotify/inotify$ (date +%u). log
Done
Fi

This is my own common use of synchronous backup is not this and a one-way backup tool, is the network more commonly used a backup tool, I will not say so that everyone attack me ah.

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.