Unison File Syncronisation

http://www.cis.upenn.edu/~bcpierce/unison

Note: most of the following is for Ubuntu Linux

Config

~/.unison/example.prf

root = /home/klemens/
root = ssh://klemens@server.com//home/klemens/
fastcheck = true
perms = 0o0000
log = true


# When set to true, this flag causes the user interface to skip
# asking for confirmations on non-conflicting changes. (More
# precisely, when the user interface is done setting the
# propagation direction for one entry and is about to move to the
# next, it will skip over all non-conflicting entries and go
# directly to the next conflict.)
auto = true

#  When this is set to true, the user interface will ask no
#  questions at all. Non-conflicting changes will be propagated;
#  conflicts will be skipped.
batch = true

# Silent suppresses all but error messages. 
# Ideal for cron usage "unison -silent example"
#silent = true

# Debug output
#debug = all

# Repeat sync every 10minutes 
#repeat = 600

# Repeat when filesystem detects changes (problem with large amount of files?)
#repeat = watch

# Prevent writing into umounted mount directory
#mountpoint = /media/usbdisk

# Use rsync to copy files larger than 10MB
copythreshold = 10240
copyprog = rsync --inplace --no-whole-file
copyprogrest = rsync --inplace --no-whole-file --partial


# In case of conflicts always prefer my versions (Alternative: "prefer = newer")
prefer = /home/klemens/

# Create a copy with the old data on the non-preferred side
# "test (copy: conflict on 2016-06-09).sh"
copyonconflict = true

# Sync only certain subdirectories
path = Documents
path = Downloads
path = Music

# Handle symbolic links if they differ on local and destination system
# Example: Local: /home/klemens/Music = symlink to /srv/Music
follow = Path Downloads
follow = Path Music

# Ignore files or directories per name, also in subdirectories
ignore = Name {.svn}
# Ignore path
ignore = Path top-level-dir/second-level-dir

Ignore path documentation: http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html#ignore

Cron

  • crontab -e
    • 0 10,12,14,16,18 * * * run-one nice -n 19 ionice -c3 unison -auto -batch -silent example

Prevent leftover locks by run-one

  • sudo vi /etc/rc.local
    • # Cleanup leftover locks of run-one (for unison cronjob)
      rm /home/username/.cache/run-one/*

Cron + Zabbix

  • vi ~/bin/unison_wrapper.sh
#!/bin/bash
#

# Store start timestamp
start=`date +%s`

OUTPUT="$(run-one nice -n 19 ionice -c3 unison -silent ullVaio)"
#echo exit: $?
#echo out: ${OUTPUT}

# Unison exit codes
#    0: successful synchronization; everything is up-to-date now.
#    1: some files were skipped, but all file transfers were successful.
#    2: non-fatal failures occurred during file transfer.
#    3: a fatal error occurred, or the execution was interrupted.

if [ $? -eq 3 ] ; then
  echo "Unison: a fatal error occurred, or the execution was interrupted"
  exit 3
fi

# Calculate runtime in seconds
end=`date +%s`
runtime=$(($end-$start))
# echo runtime

# Notify Zabbix
zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -k unison.runtime -o $runtime > /dev/null

Ubuntu Version Problems

Use 2.40 on Ubuntu 16.04LTS Xenial to be compatible with Ubuntu 12.04-15.10

Use 2.48 (xenial) on 14.04

Run Unison on Startup / as Service

http://serverfault.com/questions/382042/run-unison-in-background-on-startup

Watch

http://askubuntu.com/questions/709319/unison-repeat-watch-file-limit

Windows

from http://strobelstefan.org/?p=336

Problem: slow performance of big files

https://github.com/bcpierce00/unison/issues/65
 

Unsion with separate ssh key

You can use this, e.g. when your primary private key is password protected.

  • ssh-keygen -t rsa -C "me@mypc_for_unison" -f ~/.ssh/id_rsa_unison
    • Do not enter a passphrase!
  • ssh-copy-id -i ~/.ssh/id_rsa_unison me@host.example.com
  • unison -sshargs '-i /home/USER/.ssh/crontabkey_dsa' ...