File bidirectional synchronization in Linux

Source: Internet
Author: User
Tags ocaml
1. Unison introduction: Unison is a file synchronization tool that can be used on Windows, Linux, and other Unix platforms. it can ensure consistency between the two folders (local or online. Unison has the same features as some other synchronization tools or file systems, but it also has its own features...

1. Introduction to Unison
Unison is a file synchronization tool that can be used in Windows, Linux, and other Unix platforms. it can ensure consistency between the two folders (local or online. Unison has the same features as some other synchronization tools or file systems, but also has its own features:
1. cross-platform use;
2. there are no special requirements on kernel and user permissions;
3. the Unison function is bidirectional. it can automatically process the parts without conflict in the two-minute copy. the conflicting parts will be displayed so that the user can choose to update the policy;
4. as long as the two hosts can be connected, you can run unison. you can directly use socket connection or secure ssh connection, which does not require high bandwidth, use a compression transfer protocol similar to rsync.

The environment is as follows:
Vm1: 10.13.114.19
Vm2: 10.13.114.32

II. Compile and install Unison
In Linux, Objective Caml compiler is required when Unison is compiled and installed through the source code package.
Install
[Root @ vm1 ~] # Wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
[Root @ vm1 ~] # Tar-xzvf ocaml-3.12.1.tar.gz
[Root @ vm1 ~] # Cd ocaml-3.12.1
[Root @ vm1 ocaml-3.12.1] #./configure
[Root @ VMware ocaml-3.12.1] # make world opt
[Root @ VMware ocaml-3.12.1] # make install

Compile and install Unison
[Root @ vm1 ~] # Wget http://www.seas.upenn.edu /~ Bcpierce/unison // download/releases/stable/unison-2.40.63.tar.gz
[Root @ vm1 ~] # Tar-xzvf unison-2.40.63.tar.gz
[Root @ vm1 ~] # Cd unison 2.40.63
[Root @ vm1 unison-2.40.63] # make UISTYLE = text
[Root @ vm1 unison-2.40.63] # make install

When executing make install, the following error message may appear:
Mv: cannot stat '/root/bin // unison': No such file or directory
Make: [doinstall] Error 1 (ignored)
Cp unison/root/bin/
Cp: cannot create regular file '/root/bin/': Is a directory
Make: *** [doinstall] Error 1

The cause of the error is that, by default, Unison copies the file to the/root/bin directory, but Linux does not, therefore, we need to copy the generated executable file unison to the PATH directory of the system.
[Root @ vm1 unison-2.40.63] # cp unison/usr/local/bin

 

Upload the executable file unison to the remote host 10.13.114.32
[Root @ vm1 unison-2.40.63] # scp unison root@10.13.114.32:/root/
Log on to the remote host through SSH, and then copy unison to the PATH directory of vm2
[Root @ vm2 ~] # Cp unison/usr/local/bin

3. configure ssh key trust
We recommend that you use a common user to perform operations. The reason is that the root operation is dangerous and password-free login is even more dangerous.

Create an admin user on two servers
[Root @ vm1 ~] # Useradd-m admin
[Root @ vm1 ~] # Passwd 12345
[Root @ vm2 ~] # Useradd-m admin
[Root @ vm2 ~] # Passwd 123456

Create a key on vm1 and configure vm2 Trust
[Root @ vm1 ~] # Su-unison
[Admin @ vm1 ~] $ Ssh-keygen-t rsa
Use the default value when you are prompted to save the location of the private key and public key;
When you are prompted whether a passphrase is required, press enter directly, that is, the private key password is not used.
Then, a pair of keys, id_rsa (private key file) and id_rsa.pub (public key file) will be generated and stored in the/home/unison/. ssh/directory.

Add the public key to the authorized_keys file of vm2
Upload a file to vm2
[Admin @ vm1 ~] $ Scp ~ /. Ssh/id_rsa.pub unison@10.13.114.32:/home/unison/

Use rsync SSH to log on to the remote host and add the public key to the authorized_keys file.
[Admin @ vm2 ~] $ Mkdir. ssh
[Admin @ vm2 ~] $ Chmod 700. ssh
[Admin @ vm2 ~] $ Mv ~ /Id_rsa.pub ~ /. Ssh/authorized_keys
[Admin @ vm2 ~] $ Chmod 600 ~ /. Ssh/authorized_keys

Similarly, perform the following steps to create a key on vm2 and configure the vm1 Trust.
[Root @ vm2 ~] # Su-admin
[Admin @ vm2 ~] $ Ssh-keygen-t rsa

Upload the file to vm1
[Admin @ vm2 ~] $ Scp ~ /. Ssh/id_rsa.pub unison@10.13.114.19:/home/unison/

Use rsync SSH to log on to vm1 and add the public key to the authorized_keys file.
[Admin @ vm1 ~] $ Mv ~ /Id_rsa.pub ~ /. Ssh/authorized_keys

Restart the SSH service
[Root @ vm1 ~] #/Etc/init. d/sshd restart
[Root @ vm2 ~] #/Etc/init. d/sshd restart

IV. configure and use Unison
Create the test directory on the two servers for testing.
[Root @ vm1 ~] # Su-admin
[Unison @ vm1 ~] $ Mkdir test
[Root @ vm2 ~] # Su-unison
[Unison @ vm2 ~] $ Mkdir test

Run the unison command on both servers. if you are prompted to confirm, press enter to select the default value.
[Unison @ vm1 ~] $ Unison/home/admin/test/ssh: // admin@10.13.114.32 // home/admin/test/
[Unison @ vm2 ~] $ Unison/home/admin/test/ssh: // admin@10.13.114.19 // home/admin/test/

Modify the unison configuration file of the two servers and enter the following content:
[Unison @ vm1 ~] $ Vim/home/unison/. unison/default. prf
 
# Unison preferences file
Root =/home/admin/test
Root = ssh: // admin@10.13.114.32 // home/admin/test/
# Force =
# Ignore =
Batch = true
# Repeat = 1
# Retry = 3
Owner = true
Group = true
Perms =-1
Fastcheck = false
Rsync = false
Sshargs =-C
Xferbycopying = true
Log = true
Logfile =/home/unison/. unison/unison. log


[Unison @ vm2 ~] $ Vim/home/unison/. unison/default. prf
# Unison preferences file
Root =/home/admin/test
Root = ssh: // admin@10.13.114.19 // home/admin/test/
# Force =
# Ignore =
Batch = true
# Repeat = 1
# Retry = 3
Owner = true
Group = true
Perms =-1
Fastcheck = false
Rsync = false
Sshargs =-C
Xferbycopying = true
Log = true
Logfile =/home/unison/. unison/unison. log


The related annotations are as follows:
Force indicates that the directory is synchronized to the remote end based on the folder specified locally. Note that if the force parameter is specified, Unison becomes a single synchronization item, that is, it will be synchronized based on the folder specified by force, similar to rsync.
The basic principle of Unison bidirectional synchronization is: if there are two folders A and B, folder A synchronizes its changes to B, and folder B also synchronizes its changes to, the last two folders A and B share the same content, which is A collection of AB folders.
One disadvantage of two-way Unison synchronization is that unison will not synchronize a file when it is modified in both the synchronization folders, because unison cannot determine which one prevails.
Ignore = Path indicates that the specified directory is ignored, that is, it is not synchronized during synchronization.
Batch = true, indicating full automatic mode. accept the default action and execute it.
-Fastcheck true indicates that only the file creation time is used for synchronization. if the option is false, Unison compares the content of files in two locations.
Log = true indicates that the running information is output on the terminal.
Logfile specifies the output log file.

In addition, Unison has many parameters. here we only introduce several common parameters. for details, see the Unison manual.
-Auto // accept the default action and wait for the user to confirm whether the action is executed.
-Batch // batch mode. it is fully automated. it accepts the default action and executes it.
-Ignore xxx // add xxx to the ignore list
-Ignorecase [true | false | default] // whether to ignore case sensitivity of file names
-Follow xxx // whether synchronization of the symbolic link pointing content is supported
Owner = true // The File owner that maintains synchronization
Group = true // keep the synchronized file group information
Perms =-1 // retain the read and write permissions of the synchronized files
Repeat = 1 // start a new synchronization check after 1 second interval
Retry = 3 // retry upon failure
Sshargs =-C // use the ssh compression transmission mode
Xferbycopying = true"
-Immutable xxx // unchanged directory, which can be ignored during scanning
-Silent // quiet mode
-Times // synchronous modification time
-Path xxx parameter // synchronize only the subdirectories and files specified by the-path parameter, instead of the entire directory.-path can appear multiple times.

PS: the unison configuration file in Windows is in the C: \ Documents Ents and Settings \ currentuser \. unison directory by default. the default configuration file name is default. prf.

V. test
First, create a file or directory in the/home/unison/test directory of server1 and server2 respectively, and then execute unison on server1, if you can see the files created on both server 1 and server 2, the synchronization is successful.

Create files on server 1 and server 2 respectively
[Unison @ server1 ~] $ Cd test
[Unison @ server1 test] $ touch 1.txt touch 3.txt
[Unison @ server2 ~] $ Cd test
[Unison @ server2 test] $ touch 2.txt touch 4.txt

Run unison on server 1
[Unison @ server1 ~] $ Unison

Check whether the file is synchronized on server 1 and server 2.
[Unison @ server1 ~] $ Cd test
[Unison @ server1 test] $ ls
1. txt 2.txt 3.txt 4.txt
[Unison @ server2 ~] $ Cd test
[Unison @ server2 test] $ ls
1. txt 2.txt 3.txt 4.txt

All 1.txt 2.txt 3.txt 4.txt files are displayed, indicating that the file has been synchronized successfully!

Note: You may need to enter the password for the first SSH connection.

6. regular or real-time synchronization
If you want to execute tasks on a regular basis, you can use crontab to schedule tasks. for example, you can set the task to be executed every 5 minutes by using the following method:
[Root @ server1 ~] # Su-unison
[Unison @ server1 ~] $ Crontab-e
1 */5 */usr/local/bin/unison

Author: ERDP technical architecture"

Related Article

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.