Bidirectional synchronization of files in Linux under Unison

Source: Internet
Author: User
Tags rsync ocaml

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://hx100.blog.51cto.com/44326/612301

I. Introduction of Unison
Unison is a file Sync tool that can be used under Windows, Linux, and other Unix platforms to keep content consistent between two folders (local or network). Unison has the same features as some other synchronization tools or file systems, but it also has its own characteristics:
1. Cross-platform use;
2. No special requirements for kernel and user rights;
3.Unison is bidirectional, it can automatically handle the two copy of the update without conflicting parts, the conflicting parts will be displayed to allow users to choose the update strategy;
4. As long as the two host can be connected, you can run unison, you can directly use the socket connection or secure SSH connection, the bandwidth requirements are not high, using similar to rsync compression Transfer protocol.

The environment is as follows:
vm1:10.13.114.19
vm2:10.13.114.32

Second, compile and install unison
Objective Caml compiler is required when installing Unison under Linux through Source package compilation.
Install in the following ways
[Email protected] ~]# wget http://caml.inria.fr/pub/distrib/ocaml-3.12/ocaml-3.12.1.tar.gz
[Email protected] ~]# TAR-XZVF ocaml-3.12.1.tar.gz
[Email protected] ~]# CD ocaml-3.12.1
[Email protected] ocaml-3.12.1]#./configure
[email protected] ocaml-3.12.1]# make world opt
[[email protected] ocaml-3.12.1]# make install

Compiling and installing unison
[Email protected] ~]# wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.40.63.tar.gz
[Email protected] ~]# TAR-XZVF unison-2.40.63.tar.gz
[Email protected] ~]# CD unison-2.40.63
[email protected] unison-2.40.63]# make Uistyle=text
[[email protected] unison-2.40.63]# make install

During the make install process, you may receive the following error message:
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 reason for the error is that the file is copied to the/root/bin directory by default with Unison, but Linux does not have that directory by default, so we need to copy the resulting executable unison to the system's path directory.
[email protected] unison-2.40.63]# CP Unison/usr/local/bin



Upload the executable file unison to the remote host 10.13.114.32
[Email protected] unison-2.40.63]# SCP unison [email protected]:/root/
Log in to the remote host via SSH and copy the unison to the VM2 path directory
[[Email protected] ~] #cp unison/usr/local/bin

Third, configure SSH key Trust
It is recommended to operate through a normal user, the reason is that the root operation itself is dangerous, password-free login root is more dangerous.

Create the Admin user on both servers
[[email protected] ~]# useradd-m admin
[Email protected] ~]# passwd 12345
[[email protected] ~]# useradd-m admin
[Email protected] ~]# passwd 123456

Create a key on VM1 and configure VM2 Trust
[Email protected] ~]# Su–unison
[[email protected] ~]$ ssh-keygen-t RSA
When prompted to save the location of the private key (key) and public key, use the default value;
When prompted for the need for a private key password (passphrase), hit enter directly, that is, the private key password is not used.
After that, a pair of keys will be generated, Id_rsa (private key file) and Id_rsa.pub (public key file), saved in the/home/unison/.ssh/directory.

Add the public key to the vm2 Authorized_keys file
Uploading Files to vm2
[Email protected] ~]$ SCP ~/.ssh/id_rsa.pub [email protected]:/home/unison/

Use rsync user ssh to log in to the remote host and add the public key to the Authorized_keys file
[[email protected] ~]$ mkdir. SSH
[Email protected] ~]$ chmod. SSH
[Email protected] ~]$ MV ~/id_rsa.pub ~/.ssh/authorized_keys
[Email protected] ~]$ chmod ~/.ssh/authorized_keys

Similarly, perform the following steps to create a key on VM2 and configure VM1 Trust
[Email protected] ~]# su–admin
[[email protected] ~]$ ssh-keygen-t RSA

Uploading Files to VM1
[Email protected] ~]$ SCP ~/.ssh/id_rsa.pub [email protected]:/home/unison/

Use rsync user ssh to login to VM1 and add the public key to the Authorized_keys file
[Email protected] ~]$ MV ~/id_rsa.pub ~/.ssh/authorized_keys

Restart the SSH service
[Email protected] ~]#/etc/init.d/sshd restart
[Email protected] ~]#/etc/init.d/sshd restart

Iv. configuration and use of unison
Create the test directory on both servers for testing
[Email protected] ~]# su-admin
[Email protected] ~]$ mkdir test
[Email protected] ~]# Su-unison
[Email protected] ~]$ mkdir test

Perform a unison on both servers, and if prompted for confirmation, go directly to select default values
[Email protected] ~]$ Unison/home/admin/test/ssh://[email protected]//home/admin/test/
[Email protected] ~]$ Unison/home/admin/test/ssh://[email protected]//home/admin/test/

Modify the Unison configuration file for both servers and enter the following:
[Email protected] ~]$ VIM/HOME/UNISON/.UNISON/DEFAULT.PRF

  1. #Unison Preferences File
  2. root =/home/admin/test
  3. root = Ssh://[email protected]//home/admin/test/
  4. # force=
  5. #Ignore =
  6. Batch = True
  7. #repeat = 1
  8. #retry = 3
  9. Owner = true
  10. Group = true
  11. perms =-1
  12. Fastcheck = false
  13. Rsync = false
  14. Sshargs =-C
  15. xferbycopying = true
  16. Log = true
  17. LogFile =/home/unison/.unison/unison.log



[Email protected] ~]$ VIM/HOME/UNISON/.UNISON/DEFAULT.PRF

  1. #Unison Preferences File
  2. root =/home/admin/test
  3. root = Ssh://[email protected]//home/admin/test/
  4. # force=
  5. #Ignore =
  6. Batch = True
  7. #repeat = 1
  8. #retry = 3
  9. Owner = true
  10. Group = true
  11. perms =-1
  12. Fastcheck = false
  13. Rsync = false
  14. Sshargs =-C
  15. xferbycopying = true
  16. Log = true
  17. LogFile =/home/unison/.unison/unison.log



The relevant notes are as follows:
Force indicates that the directory is synchronized to the far end, using the locally specified folder as the standard. It is important to note that if the force parameter is specified, then the unison becomes a single synchronization, which means that the folder specified by force is synchronized, similar to rsync.
Unison two-way synchronization basic principle is: if there is a B two folders, a folder to sync their own changes to the B,b folder also to sync their own changes to a, and finally a B two folders of the same content, is the AB folder collection.
One drawback of unison bidirectional synchronization is that, for a file to be modified in two synchronized folders, Unison is not synchronized because Unison cannot judge that.
Ignore = path means that the specified directory is ignored, which is not synchronized when synchronizing.
Batch = True, which indicates the automatic mode, accepts the default action, and executes.
-fastcheck true means that the synchronization is only compared by the creation time of the file, and if the option is False,unison, the contents of both files are compared.
Log = True indicates that the run information is output at the terminal.
LOGFILE Specifies the output log file.

In addition, Unison has a lot of parameters, here are only a few of the commonly used, please refer to the Unison manual for details.
-auto//Accept the default action and wait for the user to confirm that it is executed.
-batch//batch mode, fully automatic, accepts the default action and executes.
-ignore XXX//Add XXX to the Ignore list
-ignorecase [True|false|default]//Ignore file name case
-follow XXX//Whether to support the synchronization of the symbolic connection to the content
Owner = TRUE//Keep synchronized file owner
Group = TRUE//Keep file group information synchronized
perms = 1//Keep the file read/write permissions synchronized
repeat = 1//interval 1 seconds after the start of a new synchronization check
Retry = 3//Failed retry
Sshargs =-c//compressed transfer using SSH
Xferbycopying = True "
-immutable XXX//invariant directory, can be ignored when scanning
-silent//Quiet mode
-times//Synchronization modification time
-path XXX Parameters//Only the subdirectories specified by the-path parameter are synchronized and the files, not the entire directory,-path can appear multiple times.

The Unison configuration file under Ps:windows is located by default in the C:\Documents and Settings\currentuser\.unison directory, and the default profile name is DEFAULT.PRF.

V. Testing
First create the file or directory under the/home/unison/test directory of Server1 and Server2, then execute unison on Server1, and then if you can see the files you created on Server1 and Server2, the synchronization is successful.

Create files on Server1 and Server2, respectively
[[Email protected] ~]$ CD test
[[email protected] test]$ Touch 1.txt Touch 3.txt
[[Email protected] ~]$ CD test
[[email protected] test]$ Touch 2.txt Touch 4.txt

Execute unison on the Server1
[email protected] ~]$ Unison

See if files are synchronized on Server1 and Server2
[[Email protected] ~]$ CD test
[[email protected] test]$ ls
1.txt 2.txt 3.txt 4.txt
[[Email protected] ~]$ CD test
[[email protected] test]$ ls
1.txt 2.txt 3.txt 4.txt

All see the "1.txt 2.txt 3.txt 4.txt" All the files, the file synchronization has been successful!

Note: You may need to enter the password once for the first SSH connection, and then you will not need to enter it.

Six, regular or real-time synchronization
If you want to do this regularly, you can do so by crontab scheduled tasks, such as setting up every 5 minutes
[Email protected] ~]# Su-unison
[Email protected] ~]$ CRONTAB-E
1 */5 * * * */usr/local/bin/unison


Reference: http://heylinux.com/archives/845.html

This article is from the "No Cloud security Technology Station" blog, please be sure to keep this source http://hx100.blog.51cto.com/44326/612301

Bidirectional synchronization of files in Linux under Unison

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.