Use rsync to backup your Mac to a remote server via SSH

If you find online backup solutions expensive and heavy on your system, you might be pleased to hear there is a cheap, easy way to create a mirror backup your Mac (or other UNIX variant) to a secure online server. And as of Mac OS X Leopard, rsync will even transfer metadata associated with your files like Tags and Comments.

This method does not allow you to roll back to previous dated backups like certain backup solutions do (Time Machine, for example). What it does is create a mirrored backup of your Mac on a remote server, so it’s best used in conjunction with a local hard-drive based incremental backup solution.

Still, if the house burns down your files will be safe and once you’ve done the initial backup, rsync is very efficient at keeping your remote backup mirrored with your disk.

In this example we’re going to backup the currently logged-in user’s Documents folder.

What you’ll need:

-rsync running on you Mac (it’s there on 10.4 and 10.5)
-An Internet Service Provider (ISP) who allow you to connect via SSH. [examples 1 2 3]

Procedure:

Step 1 - Set up the remote server

  • You’re going to need shell access to your server. If you don’t already have it and you use cPanel to administer your server then there is often a “Request Shell Access/Remote Login” option. If there isn’t, contact your ISP directly.
  • With shell access set up, create a folder that is outside of the scope of your webserver. You could use FTP to do this, if you prefer. For example, set up a folder called private_BKP at the root level of your server and put a folder in there called Mac_One_BKP. The path is then /home/private_BKP/Mac_One_BKP and its in there that we’re going to put the contents of our Documents folder.

Step 2 - Set up your Mac

  • Launch the Terminal application (found in your Utilities folder).
  • Type rsync -avz -e ssh /Users/yourUserName/Documents yourLoginName@yourServer.com:'/home/private_BKP/Mac_One_BKP'
    An easy way to do the first part is to type rsync -avz -e ssh then drag and drop your Documents folder right into the Terminal window. Watch out that your system does not add a slash ‘/’ after the word ‘Documents’. If it does, just remove it. Why? The slash will mean rysnc will copy the contents of the Documents folder rather than copying the whole folder.
  • For the second part, you need to guarantee that ‘yourLoginName@yourServer.com:’/home/private_BKP/Mac_One_BKP’ is pointing to the folder you created on your remote server.
  • Before starting the transfer, you may want to exclude certain files and folders from being sent to the server (very private stuff, caches, etc.) To do this, see Step 3 - Deletes and Excludes below.
  • Press Return and you’ll be asked for your password. Provide it and you should see a long list of files scrolling in the Terminal window. These are the files moving from your machine to the remote server.

Note 1: If you want to checkout what the -avz -e stuff does, take a look at the rsync man page
Note 2: Your first backup can take hours, if not days (don’t worry - it’ll be MUCH faster the second time round thanks to rsync’s ability to transfer only the parts of files that have changed. Take a break from the computer and have a buy a real copy of this instead) this instead

Step 3 - Deletes and Excludes

One you’ve backed-up your Mac’s Documents folder using the method above there will come a time when you will want to run the script again - like when you add new content to your folder! However, you many also have deleted some content from your Documents.

  • To have rsync delete files on the remote server that are no longer on your machine, add --delete
    ex: rsync -avz --delete -e ssh /Users/yourUserName/Documents [etc.]
  • To have rsync exclude the transfer of certain files to the remote server , add --exclude='myFileName'
    ex: rsync -avz --delete --exclude='myFileName' -e ssh /Users/yourUserName/Documents [etc.]
    You can add multiple –exclude statements or add a list via an –exclude_from file (see the manual)

Disclaimer

Metaclarity does everything it can to ensure the information we post is accurate. However, Metaclarity can accept no responsibilty for any damage or loss of data use of the above information might cause.

Leave a Reply

You must be logged in to post a comment.