How to rsync to a server using ssh keys

mac-osxrsyncssh-keys

I'm running on a Mac, and I want to set up a crontab to rsync from my VPS on a weekly basis to grab some backup archives. I'm using ssh keys (no passwords allowed on the VPS), and normally I can ssh in without any prompts (my private key has a passphrase but it's stored in the Mac keychain). However, when cron runs a script, it fails and emails me saying that ssh failed:

Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: unexplained error (code 255) at /SourceCache/rsync/rsync-42/rsync/io.c(452) [receiver=2.6.9]

The script being executed:

#!/bin/sh
rsync -Cavz -e "ssh -i /Users/bob/.ssh/id_rsa" bob@myvps:/backups/ ~/Documents/Backups

What am I doing wrong?

Best Answer

I don't think that cron has access to your passphrase and that's why you cannot log into your remote server. The best way to achieve this is to set up a new key with no passphrase.

Using a key without a passphrase is less secure but you can however limit its usage. See AUTHORIZED_KEYS FILE FORMAT section in the sshd manual.