viernes, 22 de octubre de 2010

Synchronize folders between two ubuntu terminals automatically

Sometimes you want to synchronize the documents of your desktop with the documents in your laptop (you could want to sync all your photos, music or documents), this can be a time consuming task and you can easilly lose changes, duplicate files or simply forget to sync. To avoid this we will explain here how to install a sync folder server in ubuntu, configure it and finally sync automatically every time we start our computer.

Before anything we need to install some software in both terminals (in our case the latop and the desktop):

sudo apt-get install unison-gtk,unison,expect,openssh-server

Unison is the synchronization server that mantains the differences between local and remote folders, unison-gtk is a graphical interface for unison, expect is a program that allow us write powerfull shell scripts and finally open-ssh is the server that implements the secure shell connections.

After installing, we should test the ssh server writing in terminal this command:

ssh host-address -l user

Replace host-address with the address of the remote server. This could be an ip.
Replace user with your ubuntu user.

You should be asked to accept a certificate and after taht you need to input your password.

Next, we need to create a profile in unison. To make easier this process, we are going to use unison-gtk. Click the "add profile button" and give it a name like "MyUnisonProfile", next, for the local folder set something like /home/mylocaluser/thelocalfolderToSync, in the next page you must provide the remote folder to sync. This should be something like /home/myremoteuser/myremotefolder, check the SSH option and give the remote server.

If you provided correct information you should be prompted to input password, and finally you should see a table with the differences between the two folders. If you click Go button the two folders will be merged and from this point every change made in any folder (remote or local) can be sync using this tool.

This is terrific but like we explained in the beggining of the post, we would like to automate this process to ensure that our folders are up to date with no efford. To achive this we are going to write an script that syncs the unison profile "MyUnisonProfile" when we start our gnome session.

First of all create a file in your home directory "" and paste this code inside replacing the password and the profile with the correct values:


echo "Waiting for networking"
sleep 60;
echo "Sync folders"
expect -c "set timeout -1;\
spawn unison $PROFILE_NAME -batch
match_max 100000;\
expect *password:*;\
send -- $SSH_PASSWORD\r;\
echo "Done"

You can test this script double-clicking in nautilus and selecting execute on terminal or running in any terminal:


Note that the script starts but waits 60 seconds before sync, we need to sleep because the network is not ready until some seconds after gnome login. We could not find a better way to wait until networking is ready :(

The last step is call this script adding the sh line to the end of .bashrc file that can be found in your home directory.

WARNING: This script is in batch mode! This means that you won't be asked for any permission. If you delete a file in any terminal, after running this script you will miss this archive in both terminals.

PS: If you install unison-gtk using the ubuntu software center, the "unison" command will be linked autommatically to unsion-gtk. This won't allow you to create a completely silent script because this will open the gtk window. In this case you need to look for the command line tool and call this command directly Eg. unison-2.32.52

No hay comentarios:

Publicar un comentario