• No results found

Two networked UNIX machines

Unison synchronises fi les between computers – here we’re using the command- line version of the tool

This line indicates that Unison has nothing to sync at the moment, since no fi les have changed

This error message shows that there is a lock fi le that needs to be manually removed

The network connection is broken so Unison cannot synchronise fi les

01

Installing Unison

Most Linux distributions have Unison as a package ready for installation so that you do not have to compile it. At the time of writing, the current stable version of Unison is 2.40.102. Note that every machine that is part of the synchronisation process must have a copy of the command-line version of Unison installed. Additionally, this copy of Unison should be located inside one of the directories of the default PATH shell variable. Assuming you’re on a Debian-based system, simply type:

sudo apt-get install unison

To fi nd the version of Unison you are using, just type the following command:

unison -version

03

Synchronising fi les on the same computer

Although Unison was initially developed with synchronising fi les located on different machines in mind, you can synchronise fi les located on the same computer using one of the following two methods:

• Using localhost as the remote machine name. • Using only local directory paths for both root = lines.

Your advisor prefers the second way as it does not require the SSH server be up and running. The Unison profi le fi le is called localFiles.prf

04

Synchronising fi les with another computer

Given a profi le name called articles.prf that is located inside the ~/.unison directory, you can tell Unison to use it by executing the following command:

$ unison articles

The remote machine declaration starts with

root = ssh://.

There are rare occasions – usually when the user changed a fi le on both computers before synchronising – where Unison will not be able to determine whether a fi le or directory has changed on the local or the remote server. In such situations, Unison kindly asks for your help so that it will not mistakenly proceed using the wrong version.

02

Unison profi le fi les

Unison can run from the command line without using any confi guration fi les (profi les), but using a profi le greatly simplifi es its use –

having a profi le fi le does not prevent you from adding extra command-line options (although this article will not deal much with such options). In the rare event that you have troubles working with Unison, you may run it using the ‘-debug all’ command-line option so that you can better trace and resolve errors.

and is located inside the ~/.unison directory. The following lines to work on the same machine, as they do not include a remote machine:

root = /Users/mtsouk root = /tmp

05

Running Unison using a simple profi le fi le

Some basic things you need to know about Unison profi les are:

• The default backup level is 2, which controls how many previous versions of each fi le are kept not including the current version of the fi le. So the default backup level keeps two backup copies of a fi le. The parameter for setting the backup level is called maxbackups.

• You can tell Unison not to keep fi le backups by not adding any backup options inside a Unison profi le fi le.

• Profi le fi les support Unicode characters. • The logfi le option tells Unison to keep a log fi le called unison.log inside the .unison directory. • Sometimes, network problems prohibit fi le synchronisation, but Unison’s error messages are denoting the problem:

$ unison articles Contacting server...

Connection closed by 109.xxx.yyy.zzz Fatal error: Lost connection with the server

07

The .unison directory

Most of the Unison housekeeping happens inside the .unison directory which resides in the user’s home directory.

Unison keeps the following data there:

• Its profi le fi les including the default profi le called default.prf.

• Its backup fi les, if you decided to have central backups.

• The status fi le for each used profi le.

• The Unison log fi le, should you tell it to keep one. You can put it anywhere you want but it is very convenient for it to reside there.

• Unison creates lock fi les there, during synchronisations, which it deletes when fi nished. Note: It is not recommended to synchronise the whole .unison directory. Synchronising just the profi les is okay.

08

A more advanced Unison profi le fi le

The contents of the advanced.prf fi le are as follows: root = /Users/mtsouk root = ssh://linode//home/mtsouk times = true batch = true # Log file logfile = /Users/mtsouk/.unison/ unison.log # Paths to synchronize path = code/C # Backup files backup = Name * backuplocation = central maxbackups = 3

ignore = Name {Thumbs.db} ignore = Name {.DS_Store} ignore = Path {Samples.lnk}

06

Explaining the profi le fi le

Lines starting with a # denote comments and are not processed any further. Exactly two lines must start with root =. They declare the machines (one is always the local

computer) that are going to participate in the syncing process as well as the directories that are considered the root directories for each machine. After those declarations, the actual directories that are going to be synced are listed. In this example we have just one directory (one path = line). Its full path is /Users/mtsouk/ docs/article/working for the local machine and /home/mtsouk/docs/article/working for the machine called linode (an IP alias inside /etc/ hosts). You can have as many path entries as you want. All fi les are synced except the ones that match the ignore option. You can also have as many ignore = lines as you want and can use regular expressions in them. The backup option tells Unison to keep backups of all fi les.

12

Unison hints and tips

The fi rst two or three times you use a new profi le, double-check if everything works as expected.

• You do not need to use every parameter that Unison supports, just the ones that will do your job!

• You can troubleshoot Unison using the -debug all command-line option. It will generate lots of output useful for debugging. • The more you use Unison, the more you will understand its practicality.

• You should be very careful with your backup options, especially maxbackups, as it can take up too much space on your computer.

• You can use Unison to securely exchange fi les between computers.

• If a Windows machine is involved in the synchronisation process, be careful with fi le and directory permissions.

• For non-critical data fi les you may run Unison once a day, but for critical data you should run it more often.

• Unison cannot replace regular backups! • When you are making a new profi le, either start simple or use an existing one as a starting template. Add the extra functionality and features while making sure that you always have a working profi le.

09

Explaining the advanced profi le fi le

• The times = true line tells Unison to synchronise modifi cation times.

• The maxbackups = 3 line tells Unison to keep the current fi le version plus three backups of it. • The backup = Name * line tells Unison to back up every fi le.

• The backuplocation = central, which is the default option, tells Unison to keep all backups in a central location. If neither the backupdir preference nor the environment variable UNISONBACKUPDIR are set, the .unison/backup directory is used as the backup location. If set to local, then all backups will be kept in the same directory as the original fi les.

• The batch = true option is a little tricky and you should be careful with it as Unison will ask no questions at all and non-confl icting changes will be propagated whereas confl icts will be skipped. Nevertheless, it is an essential option if you want to use Unison as a cron job.

• The ignore = Name {.DS_Store} line tells Unison to not synchronise fi les that end with

.DS_Store.

11

Two common Unison troubleshooting techniques

There are times when things do not work as expected. Unison offers you many options that can help you both fi nd and solve problems.

The fi rst option to try is the -testserver option that just connects to the remote server and then exits without synchronising any fi les.

The second thing to do is run the following command:

$ ssh remote.machine.domain 'echo $PATH'

The aforementioned command let you see whether the PATH is the same as when you log in using ssh remote.machine.domain. If the problem is with the PATH, check if the option PermitUserEnvironment in /etc/ssh/ sshd_confi g is set to ‘no’ and change it to ‘yes’.

10

Using SSH without giving a password

The single most time-saving thing you can do is to set up SSH so that you will not need to enter your password each time you want to synchronise your fi les and directories. The procedure is easy and involves the following three steps:

1. Run ssh-keygen -t rsa

You will have to enter a passphrase twice, so please do remember the passphrase! Two new fi les are going to be created: ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

2. You may need to create a directory called

.ssh on the remote server if it does not already exist.

3. Copy the contents of the ~/.ssh/id_rsa.pub

fi le from your local server into the fi le ~/.ssh/authorized_keys found on the remote server. One way of doing it is by executing the following command:

$ cat ~/.ssh/id_rsa.pub | ssh linode 'cat >> .ssh/authorized_keys'

The next time you try to log into the remote Linux server using SSH, you will be asked for the passphrase of step 1 for the last time.

From now on, you can log into the remote Linux server by just typing ssh linode:

$ ssh linode

Linux (none) 3.9.3-x86_64-linode33 #1 SMP Mon May 20 10:22:57 EDT 2013 x86_64

. . .

Last login: Wed Jul 31 18:46:23 2013 from ppp-94-64-21-97.home.otenet.gr mtsouk@li140-253:~$

The fi rst time you log into the remote server without typing your password, the following informative message will be on the screen:

Identity added: /Users/mtsouk/.ssh/ id_rsa (/Users/mtsouk/.ssh/id_rsa)