22 February 2007

Automatic backup on Mac OS X using rsync

I want to backup my iTunes music library and my iPhoto library to our local backup server that I'm able to SSH login to ...

  1. First off, enable passwordless login to the remote server. We generate a public/private rsa key pair using ssh-keygen -t rsa. Do not enter a passphrase so that it won't ask for one later :) Now copy the contents of the generated .pub file (your public key) to your server in the file ~/.ssh/authorized_keys. You should be able to login to your server using ssh username@server now.

  2. Create a script that rsyncs your libraries that contains
    rsync -a -e ssh "/Users/local_username/Music/iTunes" username@server:remote_path --update
    rsync -a -e ssh "/Users/local_username/Pictures/iPhoto Library" username@server:remote_path --update
    and name it, for instance, backupLibraries.command in your ~ folder. You can test it from the command line, now, if you so desire: ~/backupLibraries.command.

  3. The easiest way to make sure that your script is executed daily, automatically, is by using an iCal event (pffff...). First, we'll create the AppleScript that will be executed by iCal. Open Script Editor (Applications:AppleScript:Script Editor) and create a script that contains
    do shell script "/bin/bash /Users/local_username/backupLibraries.command > ~/rSyncErr.txt || echo -n"
    You can try this by pressing 'Run' from Script Editor. Store the script as backup.scpt, for instance.

  4. Now create an iCal event that executes the script daily, by setting Repeat: every day, Alarm: Run script, backup (select Other... and select the file that you just created from Script Editor), and 1 minutes after.

  5. There, you should be all set now! If something goes wrong, you will be able to find out by taking a closer look at ~/rSyncErr.txt. If you're the optimistic type, just remove "> ~/rSyncErr.txt" from the AppleScript.

1 comment:

technimad said...

To copy the .pub file to the server use:

scp [local name of .pub file] [username]@[remote server name]:[filename on remote server].pub