How to synchronize with SSH

by admin

I will share my experience in synchronizing files with SSH here. Following are demonstrated by synchronizing Dokuwiki. Of course, you can synchronize everything as you like.

The wiki I used in my homepage is Dokuwiki. The most great feature is that it does not require database connection. It’s very convenience for personal usage.

I have two Dokuwiki maintained. One is located in localhost, another in the webserver. I always edit the pages on localhost, and synchronize them to the webserver at times. Since Dokuwiki is totally PHP based, without database such as MySQL, I only need to synchronize some files.

All the changes on wiki are reflected on the folder “data” in your Dokuwiki path. So every time, I only need to synchronize this folder.

The best way to communicate with webserver is SSH. Fortunately, my webserver Godaddy support SSH access. Another thing need to concern is synchronizer. Here I use Unison. The best description of Unison comes from it’s the author of this software:

A file-synchronization tool for Unix and Windows
Unison is a file-synchronization tool for Unix and Windows, written
in OCaml. It allows two replicas of a collection of files and
directories to be stored on different hosts (or different disks
on the same host), modified separately, and then brought up to
date by propagating the changes in each replica to the other.

Unison offers several advantages over various synchronization methods
such as CVS, Coda, rsync, Intellisync, etc. Unison can run on and
synchronize between Windows and many UNIX platforms. Unison requires
no root privileges, system access or kernel changes to function. Unison
can synchronize changes to files and directories in both directions,
on the same machine, or across a network using ssh or a direct
socket connection.

Transfers are optimised using a version of the rsync protocol,
making it ideal for slower links. Unison has a clear and precise
specification, and is resilient to failure due to its careful
handling of the replicas and its private structures.

If you are using Ubuntu machine, you can install it from the repository just by one command:

sudo apt-get install unison

There is also a gtk front-end for this software: unison-gtk, but I think it’s useless at most time.

Now, another problem comes to front–in order to synchronize files with SSH, both installations of Unison on client and server are required. However, at most time, you don’t have the permission to install software such as unison on SSH server. Here, we attack this issue by SSHFS. It allows you to mount a remote folder on your machine via SSH.

Everything is OK. I wrote a small script to handle the synchronization:

sshfs -o workaround=rename username@sshserver:/path/html/wiki wiki-remote/
echo "Synchronising with sshserver"
unison /var/www/dokuwiki/data ./wiki-remote/data 
  -fastcheck yes 
  -ignore 'Path cache' 
  -ignore 'Path locks'

Modify it to fit your actual environment. Note that the argument of sshfs command “-o workaround=rename”, this prevents the “renaming problem“.

All things done. Excute this script. Or, you could add an cron job to run this script automatically once per day. Have fun in synchronizing files with SSH!