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:192.168.1.237
vm2:192.168.1.238
Second, installation unison
Install on both servers in the following ways
[Email protected] ~]# yum-y install OCaml
[Email protected] ~]# yum-y install unison
[Email protected] ~]# yum-y install OCaml
[Email protected] ~]# yum-y install unison
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–admin
[[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/admin/.ssh/directory.
[Email protected] ~]$ cd/home/admin/.ssh/
[email protected]. ssh]$ LL
Total 8
-RW-------1 Admin admin 1675 Jan 7 21:51 Id_rsa
-rw-r--r--1 Admin Admin 392 Jan 7 21:51 id_rsa.pub
Add the public key to the vm2 Authorized_keys file
Uploading Files to vm2
[email protected]. ssh]$ SCP id_rsa.pub [email protected]:/home/admin/
Use the Admin user ssh to log in to the remote host and add the public key to the Authorized_keys file
[Email protected] ~]# cd/home/admin/
[email protected] admin]# LL
Total 4
-rw-r--r--1 Admin Admin 392 Jan 7 21:54 id_rsa.pub
[[email protected] admin]$ mkdir. SSH
[Email protected] admin]$ chmod. SSH
[Email protected] admin]$ mv Id_rsa.pub. Ssh/authorized_keys
[Email protected] admin]$ chmod Ssh/authorized_keys
Similarly, perform the following steps to create a key on VM2 and configure VM1 Trust
[[email protected] ~]$ ssh-keygen-t RSA
[Email protected] ~]$ cd/home/admin/.ssh/
[email protected]. ssh]$ LL
Total 12
-RW-------1 Admin admin 392 Jan 7 22:01 Authorized_keys
-RW-------1 Admin admin 1671 Jan 7 22:04 Id_rsa
-rw-r--r--1 Admin Admin 392 Jan 7 22:04 id_rsa.pub
Uploading Files to VM1
[email protected]. ssh]$ SCP id_rsa.pub [email protected]:/home/admin/
Use the Admin user ssh to login to VM1 and add the public key to the Authorized_keys file
[Email protected] ~]# cd/home/admin/
[Email protected] admin]$ mv Id_rsa.pub. Ssh/authorized_keys
Restart the SSH service
Two servers with SU switch to root user execution
[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] ~]$ cd/home/admin/
[Email protected] ~]$ mkdir test
[email protected] ~]$ LL
Total 4
Drwxrwxr-x 2 Admin admin 4096 Jan 7 22:12 test
[Email protected] ~]# su-admin
[Email protected] ~]$ cd/home/admin/
[Email protected] ~]$ mkdir test
[email protected] ~]$ LL
Total 4
Drwxrwxr-x 2 Admin admin 4096 Jan 7 22:13 test
Modify the Unison configuration file for both servers and enter the following:
[Email protected] ~]$ VIM/HOME/ADMIN/.UNISON/DEFAULT.PRF
#Unison Preferences File
root =/home/admin/test
root = Ssh://[email protected]//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/admin/.unison/unison.log
[Email protected] ~]$ VIM/HOME/ADMIN/.UNISON/DEFAULT.PRF
#Unison Preferences File
root =/home/admin/test
root = Ssh://[email protected]//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 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/admin/test directory of VM1 and vm2, then execute unison on VM1, and then if you can see the files you created on VM1 and VM2, the synchronization is successful.
Switch Admin user on VM1 and vm2, create file
[[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 VM1
[email protected] ~]$ Unison
See if files are synchronized on VM1 and vm2
[[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!
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-admin
[Email protected] ~]$ CRONTAB-E
1 */5 * * * */usr/bin/unison
This article is from the "Linux art (q Group: 1991706)" blog, please be sure to keep this source http://304076020.blog.51cto.com/7503470/1600195
Using unison for bidirectional synchronization of files under Linux