Setup Directory Mirroring Over Network

Here we'll discuss how to setup a network directory mirroring program for backing up your files. We'll use a program called rsync to synchronize files between two linux machines on a network. If it's not already installed, just run

With rsync, you can transfer files over a network using SSH. However, when rsync commands are passed on the command line to access a remote computer, you'll be prompted for a password to that machine. If we want to script these commands, we don't want to wait for password prompts. For this, we'll need to share SSH keys. For the Raspberry Pi, you'll need to enable SSH access, which was described here. For other linux machines, this can be done with the following commands:

In this example, we'll be backing up a desktop Linux machine on the Raspberry Pi, and we'll setup a "push" configuration. In other words, the desktop will be "pushing" it's files and changes to a backup directory on the Raspberry Pi for safekeeping.

Generating SSH Keys
In order for the desktop machine to send files to the Raspberry Pi, it will need to be approved for access to the Pi's file system. We do this by sharing a SSH key. The key will need to be generated on the Raspberry Pi using the following command:

where you should replace "thisusername" with your desired login name and "thishostname" with your current machine's host name. This will generate something like the following: Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): [Hit Enter Here] Enter same passphrase again: [Hit Enter Again Here] Your identification has been saved in /home/thisusername/cron/thishostname-rsync-key. Your public key has been saved in /home/thisusername/cron/thishostname-rsync-key.pub. The key fingerprint is: 4f:5e:8c:6c:fa:10:ce:26:d4:11:be:1f:ce:46:d6:09 thisusername@thishostname The key's randomart image is: +--[ RSA 2048]+ |        .        | |       . .       |  |        o E      | |       . + = .   | |      . S O =    | |     . o & o     | |      . * B      | |       o +       | |          .      | +-+ If you want two way sharing, you'll need to repeat this on your second machine as well.

Sharing SSH Keys
Next we'll share the Pi's SSH key with our desktop Linux machine. To do this, execute the following command on the Raspberry Pi:

where "thatusername" and "thathostname" correspond to the login name and host name of the second (desktop) computer. This will put the Pi's public SSH key into /home/thisusername/.ssh/authorized_keys.

Syncing The Directory
Now on the desktop Linux machine, you should have permission to access the Pi using the SSH key without entering a password. You can test this by issuing

This should allow you to use the command line of your remote machine (the Pi).

In order to push a directory to a backup directory on the remote machine, the rsync command you'll want to use is

This command will recursively everything in the directory over to the remote machine for backup. In addition, the "delete-delay" modifier will first look to see if any of the files on the local machine no longer exist; in which case, they will also be deleted from the remote drive.

Creating the Startup Script
Now we need away to issue the rsync command automatically at a fixed interval whenever the computer is on. Start by creating a new shell script file name, for example, mydrivesync.sh:

Edit the file to contain: while true do rsync -rtvu --delete-delay -e "ssh -i /home/thisusername/cron/thishostname-rsync-key" /this/directory/ thatusername@thathostname:/that/backupdirectory sleep 5 done
 * 1) !/bin/sh

Now make the file executable:

Now make an Upstart service named, for example, mysyncsvc.conf:

Edit the file to contain: description "run backup sync script" author "Me " start on runlevel [2345] stop on runlevel [016] respawn expect fork exec /home/thisusername/mydrivesync.sh start You can start the service by executing
 * 1) mysyncsvc - mysyncsvc job file
 * 1) When to start the service
 * 1) When to stop the service
 * 1) Automatically restart process if crashed
 * 1) Essentially lets upstart know the process will detach itself to the background
 * 1) Execute script

or you can simply reboot. At this point, the service should run automatically.