User Tools

Site Tools


programming:bash:unison

Unison

Server Side

  1. Enable SSH access on server

Client Side

  1. Download Unison
  2. See ssh_connection to create the client certificate and how to put it on the server.
  3. copy in ~:
  4. Move all from ~/Documents/* in ~/Documents/Archives
  5. create ~/Documents/folder1
  6. copy in in ~/Library/Application Support/Unison:
  7. Synchronize once with Unison
  8. add in crontab:
    0 9-19 * * 1-5 ~/run_unison.sh -p server_folder1 -s server.myserver.com 2>&1 >> ~/unison.log
    30 9-19 * * 1-5 ~/run_unison.sh -p server_desktop -s server.myserver.com 2>&1 >> ~/unison.log

Scripts

run_unison.sh

#! /bin/bash
#
# To you use this script, you need to use ssh certificate
# 1) ssh-keygen -t rsa // this will produce a ~/.ssh/id_rsa.pub file
# 2) cat this ~/.ssh/id_rsa.pub into the ~/.ssh/authorized_keys of your targeted machine
# 3) use this script as connect.sh -p profile [-r anExpectFile]
#
# The expectFile contains the passphrase that you entered in the point 1) above.
 
profile=""
username=$USER
server=""
expectBin="/usr/bin/expect"
expectFile="$HOME/.add-rsa"
 
function usage(){
	echo "Usage:"
	echo "$0 -p profile -s server [-r expectFile]"
	exit;
}
 
 
while getopts ":p:r:s:" optname
do
	case "$optname" in
		"p")
			profile=$OPTARG
			;;
		"r")
			expectFile=$OPTARG
			;;
		"s")
			server=$OPTARG
			;;
		"?")
			echo "Unknown option $OPTARG"
			usage
			;;
		":")
			echo "No argument value for option $OPTARG"
			usage
			;;
		*)
		# Should not occur
			echo "Unknown error while processing options"
			;;
	esac
done
 
if [[ $profile == "" ]]; then
	usage
fi
 
echo "Starting Unison with profile $profile..."
 
/sbin/ping -o -t 2 $server
ret_ping=$?
if [[ $ret_ping -ne 0 ]]; then
	echo "No connection to server ($ret_ping). Exiting..."
	exit;
fi
 
# Run the ssh-agent
eval `ssh-agent` > /dev/null
#echo "$SSH_AGENT_PID"
 
if [[ ! -x $expectBin ]]; then
	echo "$expectBin either does not exist or is not executable!"
	exit
fi
 
if [[ -x $expectFile ]]; then
	# Use Expect to feed the passphrase to ssh-add
	$expectFile > /dev/null
	#connect to $target
	echo "/usr/bin/unison -log -logfile "$HOME/unison.log" -batch "$profile" -auto -ui text"
	/usr/bin/unison -log -logfile "$HOME/unison.log" -batch "$profile" -auto -ui text
else 
	echo "$expectFile either does not exist or is not executable!"
fi
 
# suppress the ssh-agent process
kill -9 $SSH_AGENT_PID
 
# Remaining ssh-agent
echo "List of the remaining ssh-agent:"
ps -u $username | grep "ssh-agent" | awk '{print $1}'
 
#To kill all ssh-agent use this:
#kill -9 `ps -u $USERNAME | grep "ssh-agent" | awk '{print $1}'`
 
echo "End of $target connection... good bye!"
exit 0

common.prf

# Unison preferences file
 
# (... other preferences ...)
 
# If any new preferences are added by Unison (e.g. 'ignore'
# preferences added via the graphical UI), then store them in the
# file 'common' rathen than in the top-level preference file
addprefsto = common
 
# Names and paths to ignore:
ignore = Name temp.*
ignore = Name *~
ignore = Name .*
ignore = Name .*~
ignore = Path */pilot/backup/Archive_*
ignore = Name *.o
ignore = Name *.tmp
ignore = Name .DS_Store
ignore = Name ._*
ignore = Name ._*.*
ignore = Name ._Icon

server.prf

This is the home folder on OSX and on the remote server

# Unison preferences file
# Roots of the synchronization
root = /Users/aUser/
root = ssh://me@server.myserver.com//home/me/

server_folder1.prf

This will synchronize /Users/aUser/folder1 (OSX client) with /home/me/folder1 (linux server)

# Unison preferences file
path = Documents/folder1
include common
include server

server_desktop.prf

This will synchronize /Users/aUser/Desktop (OSX client) with /home/me/Desktop (linux server)

# Unison preferences file
path = Desktop
include common
include server
programming/bash/unison.txt · Last modified: 2013/03/11 07:43 by sbolay