A Simple Dropbox Replacement

Here’s how to make your very own personal Dropbox replacement

1) Install the same version of ‘unison’ both on the server and client.  Install ‘inotify-tools’ on the client.  Create a folder on the client that you would like to use for syncing.

2) Install ‘openssh-server’ on the server.  Create a user on the server with the same name as the client that will be syncing to it.  Also create a sync folder in the user’s home folder.

3) To avoid being asked for the password all the time on the client:

  1. Generate an RSA key pair used to authenticate with the server (instead of using a password): ssh-keygen -t rsa
  2. Send your RSA key to your server: ssh-copy-id USERNAME@SERVER
  3. Now SSH to your server and it shouldn’t ask for a password. If you get an “Agent admitted failure to sign using the key” error, try ‘ssh-add’ and repeat this step.

4) On the client, create the following file as ‘/usr/local/bin/unisonsyncd’.  Be sure to replace [User], [Client/Server Sync Folder], and [Sync Server] with your info:

#!/bin/bash

# Variables Section
#==================================================================
# list process to monitor in the variable below.

PROGRAM=”unison”

# varible checks to see if $PROGRAM is running.

APPCHK=$(ps aux | grep -c $PROGRAM)

#==================================================================

# The ‘if’ statement below checks to see if the process is already
# running with the ‘ps’ command.  If the value is returned as a ’0′
# then the process will be started, otherwise the script will exit.

if [ $APPCHK = ‘0’ ]
then
/usr/bin/unison /home/[User]/[Client Sync Folder] ssh://[User]@[Sync Server]//home/[User]/[Server Sync Folder] -ui text -perms 0 -dontchmod -follow ‘Regex .*’ -contactquietly -batch
fi

exit

5) On the client, also create the following file as ‘/usr/local/bin/inotifyd’.  Be sure to replace [User] and [Client Sync Folder] with your info:

#!/bin/sh
while inotifywait -e modify -e attrib -e close_write -e close -e moved_to -e moved_from -e move -e create -e delete -e delete_self -r /home/[User]/[Client Sync Folder]; do
/usr/local/bin/unisonsyncd
done

6) Edit your crontab file (crontab -e) and add the following line:

*/3 * * * * /usr/local/bin/unisonsyncd >/dev/null 2>&1
@reboot /usr/local/bin/inotifyd

That’s all there is to it!  Now you should have a folder on your client that operates just like Dropbox does.  The only issue that still needs to be addressed is preventing multiple instances of Unison from running…  Also locking files while the sync is occuring might be a good idea.