jasonwryan.com

Miscellaneous ephemera…

rsync + SSH Backups

image

I posted recently about setting up a microserver and included a rsync script for backing up my files to the server. At the time, I was running it manually, but since have sorted out the excludes list and have now automated it.

My original scheme was to set up a cron job and mount an NFS share before rsync’ing the directories across. In the end, I went with SSH and public key authentication as debugging all of the various permissions issues with the NFS share did not seem worth the effort.

First, on my local desktop, I created the keypair called, inventively, rsync:

1
2
3
ssh-keygen -t rsa -f rsync
mv rsync ~/.ssh && chmod 600 ~/.ssh/rsync
ssh-copy-id -i rsync.pub jason@microserver

This keypair was created without a passphrase so that the script could be successfully called from cron. To slightly increase the security around a key without a passphrase, I used the from option in authorized_keys1, to restrict access to the IP address of the client machine(s), like so:

1
from="192.168.1.10*" ssh-rsa AAAAB3Nza...

It was then just a matter of adding a line to my crontab:

1
2
crontab -e
00 22 * * * /home/jason/Scripts/runbackup

To further simplify matters, I created an alias in $HOME/.ssh/config for the SSH command:

.ssh/config
1
2
3
4
5
Host backups
   Hostname 192.168.1.200
   Port 0000
   User jason
   IdentityFile ~/.ssh/rsync

And I was done. Everynight at 10pm, all of my data will be backed up to the server. The rsync command in the script:

runbackup.sh
1
2
3
4
5
6
#!/bin/sh
# Backup to remote server over SSH

rsync -azP --delete --exclude-from=/home/jason/Scripts/excludes.txt \
    --log-file=/home/jason/Documents/rsync-$(date +%d%m%y).log \
    -e ssh /home/jason /etc backups:Backups/Centurion
Notes
  1. All of the options are detailed in man sshd(8).

Creative Commons image from skreuzer.

Comments