Have you ever wanted to keep your Documents directory contents synchronised and consistent across more than one platform? While doing some school work I found myself using my laptop or my desktop and hunting for files.

As always, please be aware of possible word wrapping in scripts displayed here.

In order for this to work the same user name must exist on both the local and the remote systems. Now it is possible to get this to work using different user names, but not with the scripts I supply here. So for simplicity be sure that the same username exists on both systems, and that they have a Documents directory in the location of /home/username/Documents on both systems.

Also, unision must be installed on both systems. Using debian you can “apt-get install unison” easy enough.

Both of the following scripts are executed on the system doing the syncing. This is normally the client, laptop, or the system which is disconnected from the network (or powered off) the most.

Script 1
This script just generates keys and then copies the keys to the remote server. This way you don’t need to enter login credentials to log into the remote server via ssh. Without this, the cron job will just hang waiting for input.

As root execute the following commands. You may need sudo if running ubuntu.

nano /bin/sshkeys.sc

paste in the following

echo GETTING INFORMATION
echo enter the target systems username
read _USER
echo enter the target systems name or IP address
read _SERVER
echo GENERATING KEYS
echo do not enter a passphrase. Just hit enter twice. In fact
echo just accept all defaults and keep hitting enter.
ssh-keygen -t rsa
cd ~/.ssh
echo COPYING the generated keys to the remote server
echo you will be prompted for the password for the supplied user
echo but, this should be the last time!
scp ./id_rsa.pub $_USER@$_SERVER:/home/$_USER/.ssh/authorized_keys

end paste

chmod +x /bin/sshkeys.sc

Here is the tricky part… NOT as root execute the following command.

/bin/sshkeys.sc

If you are logged in as root you just set up root’s key’s on the remote server, which would be fine if root’s documents are the ones you want to synch. If your user account is a sudoer, DO NOT execute this command with a sudo.

So lets examine this script. At first we are prompted for user input with the echo and read commands. You must supply the user name, and information about the target remote server. (with either hostname, FQDN, or IP address.) It is kind of self explanatory there. Then we are generating the encryption keys and copying them to the remote server. For the secure copying, you will be prompted for the user’s password. This should be the last time you are asked. Because from here on out it will use the copied keys, and we told it (by hitting enter twice) to not request a pass phrase.

Another (unrelated) fun trick:
Once you have done the keygen you can do all kinds of nifty things, like execute commands remotely without logging in

ssh user@servernameorIP ‘ls -l /tmp’

Would list the contents of the /tmp directory on the remote machine

ssh -X ant2ne@192.168.1.5 VirtualBox

Would launch the Virtual Box console on the remote machine, but display it on the local machine.

Script 2
as root execute these commands. you may need to proceed them with sudo if using ubuntu.

nano /bin/unison.sc

Paste in the following.

Syncing()
{
_LOG=/tmp/my_unison.log
_DEST=ssh://remoteuser@remoteServer//home/remoteuser/Documents
_SOURCE=/home/localuser/Documents

date > $_LOG
unison -silent -auto -fastcheck true $_DEST $_SOURCE
date >> $_LOG
}
ping -c 1 RemtoeServer && Syncing

End paste

chmod +x /bin/unison.sc

So lets examine this script

_LOG is the location for your log file. I suggest /tmp/my_unison.log or some other directory where you have write access to.

_DEST is the remote destination to sync the files to. It must be in the format of “ssh://remoteuser@remoteServer//path/to/destination”. Lets break this down further. “ssh:” is the protocol to be used for the synch. ssh is encrypted and more secure. “remoteuser@remoteserver” this is the information that ssh needs to make a connection. remoteuser needs to be a valid user with write access on the path/to/destination part. and remoteserver is the FQDN or IP address of the target server. “//path/to/destination” is a UNC path to the target of the sync. If you were to navigate to the directory on the remote server and type pwd, that would be the information included here. BUt don’t forget to have the 2 wacks ( aka “//”) at the beginning of this path and not just one.

_SOURCE is the local location of the directory to be synced. if you were to navigate to that directory and type pwd this would be the exact information entered here.
date > $_LOG and date >> $_LOG are just a little way to find out exactly how long it took to sync. If this takes too long then perhaps unison isn’t the method for you.

ping -c 1 remoteserver && Syncing This is the last line, but it is executed first. This line tells the local computer to ping the remoteserver and if the remote server responds, then jump the the function Syncing, which then executes the stuff contained within the {} of the Syncing heading. If the ping fails, then the script just exits and there is not Syncing attempt. You don’t want to sync to a server that doesn’t exist do you? This is very handy for a laptop.

Once again, as root execute this command. You may need to proceed them with sudo if using ubuntu.

nano /bin/unison.sc

This time you need to edit _DEST= and _SOURCE= to fit your environment. I don’t know what that is. The words remoteuser remoteserver and localuser all need to be modified to fit your needs.

Cron
The first synchronisation may take awhile because not only does it have to copy the files, but I think it builds a database of the meta data for the files. So you may want to execute the script manually by typing “unison.sc”. After this one completes, you’ll want it to be automated.

The final step is to configure cron to perform the sync on a regular basis. With your limited user account (the user that wants to sync the documents) execute.

crontab -e

And paste in the following line

*/15 * * * * /bin/unison.sc

End paste
This line executes this unision.sc ever 15 minutes, basically causing the Documents directory to synchronise every 15 minutes.

Disclaimer
Synchronisation is not a form of backup. If you delete a file, or it becomes corrupted, that file will be deleted and corrupted on the remote system as well. This is just a method of keeping your Documents consistent across multiple platforms.

But, once the Documents are synced, the remote system (assuming the remote system is a file server) can use tar to backup the Documents for archiving purposes. Didn’t I talk about backups using tar on another blog post?