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

```
sudo apt-get install rsync

```

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:

```
sudo apt-get install openssh-server
sudo ufw allow 22

```
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:

```
mkdir /home/thisusername/cron
ssh-keygen -t rsa -b 2048 -f /home/thisusername/cron/thishostname-rsync-key

```

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]----+

+-----------------+

```

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:

```
ssh-copy-id -i /home/thisusername/cron/thishostname-rsync-key.pub thatusername@thathostname

```

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

```
ssh -i /home/thisusername/cron/thishostname-rsync-key thatusername@thathostname

```

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

```
rsync -rtvu --delete-delay -e "ssh -i /home/thisusername/cron/thishostname-rsync-key" /this/directory/ thatusername@thathostname:/that/backupdirectory/

```

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:

```
sudo nano mydrivesync.sh

```

Edit the file to contain:

```
#!/bin/sh
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

```

Now make the file executable:

```
sudo chmod +x mydrivesync.sh

```

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

```
sudo nano /etc/init/mysyncsvc.conf

```

Edit the file to contain:

```
# mysyncsvc - mysyncsvc job file

description "run backup sync script"
author "Me <myself@i.com>"

# Execute script
exec /home/thisusername/mydrivesync.sh start

```

You can start the service by executing

```
sudo service mysyncservice start

```

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