Pete Freitag Pete Freitag

Howto Backup your Mac incrementally over SSH

Published on March 10, 2006
By Pete Freitag
linuxapple

Do you have access to a shell account on a unix server with some spare space? If so it's pretty easy to incrementally backup your files securely with SSH.

I titled this entry Howto Backup your Mac incrementally over SSH but this technique can also be used to backup any computer that can run rsync and ssh. They are already installed on Mac OS X, and most linux / unix servers.

Step 1 - Create a folder to store your backups on the remote server

mkdir backup

Make sure that your SSH user has permission to write to this directory.

Step 2 - Setup automatic authentication Optional

This step allows the backups to run without prompting you for a password when it runs. You can omit this step but you will have to type in your ssh password each you run backup.

I wrote an article called Setting up public key authentication over SSH that will guide you through this step.

If you own the server you might also want to create a user specifically for this process.

Step 3 - Use rsync to backup files incrementally

rsync -e "ssh" -rca --delete-after ~/test/ pete@myserver.example.com:backup

Now lets break it down a bit:

  • rsync - this syncs the local directory to with the server directory.
  • -e "ssh" - this tells rsync to use ssh if your want to pass in other ssh options such as port you can do that in the quotes: -e "ssh -p 12345"
  • -rca recursive, checksum, and archive
  • --delete-after - this will delete files on the server if you delete them locally.
  • ~/test/ - I am backing up / syncing the test directory inside my home directory on my mac.
  • pete@myserver.example.com:backup - my ssh username is pete, my remote ssh server hostname is myserver.example.com, and I am backing up into the directory ~pete/backup.

Excluding directories

Sometimes you might want to exclude a directory from being backed up, perhaps your Music directory since that is already backed up on your ipod.

rsync -e "ssh" -rca --delete-after --exclude=Music --delete-excluded ~/test/ pete@myserver.example.com:backup

Step 4 - Schedule it with cron Optional

Now lets create a cron job (scheduled task) to run this script every day. First make a new file called backup.sh in your home directory.

#!/bin/sh
rsync -e "ssh" -rca --delete-after ~/test/ pete@myserver.example.com:backup

Now sure make the file is executable: chmod ug+x backup.sh

Next type crontab -e this will open up your scheduled tasks list, in most cases it will open an empty file. Add the following to the file:

15 11 * * * yourusername ~yourusername/backup.sh

That's will schedule the script to run at 11:15am. If you don't want to do this step you can simply run the command whenever you want to run a backup.

DISCLAIMER You should test everything before performing the backups on your live data. If you would like to use the scripts above you must acknowledge that I'm not responsible for any loss of data.

I'd love to hear your feedback on this, please comment below. I'm use this technique to backup my two web servers on to each other.



backup ssh rsync mac osx linux unix tips howto

Howto Backup your Mac incrementally over SSH was first published on March 10, 2006.

If you like reading about backup, ssh, rsync, mac, osx, linux, unix, tips, or howto then you might also like:

Weekly Security Advisories Email

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).

Comments

Thanks Pete, I have it working like a champ. A dedicated server of mine is backing itself up to a shared server account I have. They both have fast connections so it's going really quickly.
by Jon Gales on 03/10/2006 at 9:52:41 AM UTC
I tried this approach, but...the resource forks are not preserved. Of course, if your data doesn't require this, it's a great solution.
by K Jim on 03/21/2006 at 7:15:07 AM UTC
I didnt quite understand the last comment if someone can put in little more layman terms i would appreciate.
by Nav on 08/09/2006 at 9:34:07 AM UTC
Beware "--delete-after". I run two scheduled cron jobs, one S,M,Tu,W,Th,F without the --delete-after option, then a separate Saturday job with the --delete-after option. If you run this automatically always using the --delete-after switch, you will find that the file you accidentally deleted yesterday was faithfully removed from your backup copy as well.
by speedeep on 01/09/2007 at 12:38:31 PM UTC
try using backup software that supports it like www.disksave.com
by Ronnie on 03/06/2007 at 11:54:07 PM UTC
Nice posting. Backing up the data is best option to avoid data loss. But sometime after precaution we face the problem of data loss which leads a frustrated situation. In this scenario we can use stellar phoenix data recovery mac software which easily recover data from mac partition. Get it from here http://www.macintosh-data-recovery.com

Thanks
by shrekk on 07/28/2008 at 3:38:23 AM UTC
A concise and very useful post. I could backup a damaged-Windows disk using a Debian live CD via rsync. Thanks.
by igiron on 10/27/2008 at 9:52:29 PM UTC
Thanks for the howto. I'm now backing up my MacBook and iMac to my Ubuntu file server. You saved me $500 on a Time Capsule.
by Travis on 11/09/2008 at 10:30:25 PM UTC
There are a lot of spam comments in here, can someone clean those up?
by David Zatz on 04/10/2010 at 1:43:55 PM UTC