In our last part, we used
rsync to connect to a remote server to perform incremental backups. The problem is that we really want this to be automatic. Scheduling when a backup occurs is actually fairly simple. What is more work is performing automatic authentication so our backup can occur without user intervention.
Authentication using an SSH key
We’ll be using an SSH key to authenticate ourselves with Strongspace (or any other
rsync server for that matter). Thanks have to go to Jens-Christian Fischer’s post on his own backup solution, which helped get me started. To start with, open a terminal and enter the following command:
$ ssh-keygen -t dsa -b 1024
Leave the key location unchanged, but enter a passphrase. You can choose not to enter a passphrase (and this will simplify things) however this is pretty insecure. If anyone gets hold of your key they’ll be able to access your Strongspace files without the need for a password.
Now we’ve generated our key, we can use it to authenticate ourselves when connecting to the remote server. The server we’re connecting to needs a copy of our public key:
$ cp ~/.ssh/id_dsa.pub authorized_keys
Log into Strongspace via the web interface, create a directory called
.ssh and copy the
authorized_keys file into this directory. To test this, run the
rsync command we created in part one:
$ rsync -azvL /Users/johnsmith/backup email@example.com:/home/johnsmith/backups/mac
This time, rather than the Strongpsace server asking you for your password, you’ll get prompted for your key’s passphrase.
So at this point you must be thinking “Well, what was the point of that – you’ve replaced the need to enter the password for the remote server, with the need to enter a passphrase for your key!” – and you’d be right. What we need is some kind of software that can be used to automatically handle unlocking our key.
SSHKeychain is an OSX specific SSH key management tool. When using tools like
ssh, SSHKeychain can automatically lookup your passphrase. Download and install the tool, then start it up. Open the
Preferences pane, select the
Environment tab and enable
Manage global environment variables – this will allow other applications to use the keys managed by SSHKeychain. Check the
keys tab and ensure your key (
~/.ssh/id_dsa) is visible. Finally select
Agent/Key Status from the
Agent menu, enter your passphrase for your key, and enable the option to add the passphrase to your keychain – this means that when you log on, SSHKeychain will automatically have access to your SSH key’s passphrase. So long as you’re logged on you’ll have no need to type it in again.
For SSHKeychain to start managing your key, you’ll need to log off and back on, but before you do it’s a good idea to add SSHKeychain to your startup items so you don’t have to remember to start it up every time.
Once you’ve logged off and back on, SSHKeychain will be silently managing your keys. The first time you use an application like
rsync, SSHKeychain will look in your OSX Keychain, locate the passphrase for your key and automatically authenticate you. Note that you will need to be logged in for this to work, as SSHKeychain needs to be running, and your OSX Keychain needs to be unlocked.
To test this, after logging back in run our
rsync command again – this time you shouldn’t be prompted for any password.
In part three we’ll complete our backup solution by creating an automated backup script.