Navigate / search

Backups in Mac OS X using rsync

Unix is at the core of OS X
Unix is at the core of OS X

A friend of mine posted on social media that his faithful MacBook Pro had died on him and left small bits of his projects out of reach on his hard disk. ( He had IT rustle them up)
This came at a time when I was experimenting with scripting rsync and SSH to backup a server to a secondary machine in the event things go wrong. Things do go wrong every now and again. Sometimes very wrong, like when you decided to skip putting a baking sheet under that mostly defrosted and fairly gooey Delissio pizza.
It’s easy to make backups in Mac OS X using rsync even to a company server. There are a few things you should know beforehand and your company IT guy can usually help you out with the specifics of your enterprise server. The thing about being the administrator is you are your own gatekeeper. You can easily work the angles available to you.
If you’re on your own, in a smaller company, at home or just want to take a stab at automating your backups yourself then this guide is for you.

rsync is a powerful command line utility and should be respected! Please read the whole posting before punching these commands in! As a sync tool it has the ability to delete material in the destination folder!

Rsync is a powerful unilateral sync tool that can retain all of your data with all of its attributes such as ownership, permissions, dates and so forth and it does so with compression and an algorithm that allows for incremental backups that are super fast. I have to say, it is very fast and unobtrusive to even for a full backup of a fairly large directory. The compression function boosts data throughput for the transfer and makes remote backups way less of a pain. It can also resume transfers that have failed. Along with a whole bunch of other features. Andrew Tridgel the creator of rsync said it would outlive a host of his other creations.

I use the Asus RT-N16 as my home router and it kind of runs like my Linux server as well. With a 1TB disk plugged in and mounted you can use it for incremental backups, as a media server and for always on torrenting. In this case I am going to be scheduling a local directory from my Mac’s desktop, to be backed up daily and weekly.
Rsync unlike other copy commands will remove old files that have since been deleted “syncing” the two folder or drives. This can be turned off by adding the flag –delete and your disk will grow as you go until there is no more space.
This essentially means rsync mirrors one directory to another. With a little more doing, one can have rsync keep snapshots of the disk from a few days running so that if you deleted something yesterday and the backup script ran your files still exist. This process is well explained at the previous link and requires the use of symbolic links that create a skeleton of the directory with only the files that have been changed on hand for if you need them. The “skeletal” folders only contain links to the original backup. You can script this whole process with Automator or as a cron job and then reliable backups are opened up to you with no cost and just a little elbow grease.
Heck, I love Apple’s time machine but that only works reliably when you have a network attached disk taking regular backups. Not to mention it is really a bugger trying to sort out a time machine drive. Consequently I have 2 of them sitting idly, in case I should think of some file I had forgotten about that happens to be have survived in my time machine. Not likely…

The syntax of rsync is unix based and is the same on OS X and various flavours of Linux. Actually learning rsync and cron scheduling is a good jump off point between the two operating systems. Knowing some basic Unix commands can really help you become a power user on any Mac OS.

rsync -avzn ssh /home/path/to/sourcefile/ user@IPaddress:/root/path/todestination/

Breaking the syntax down is easy:

rsync is the command and opens the syntax

-avz are the flags which delineate between all the options available for the sync. The a = archive mode which is a combination of a bunch of options meant for backups (rlptgoD). I have also included the -n flag which will only do a dry run to test the file copy.

-r, –recursive recurse into directories

-l, –links copy symlinks as symlinks

-p, –perms preserve permissions

-P, –progress and partial combined

–progress, shows the progress in the terminal

-t, –times preserve modification times

-g, –group preserve group

-o, –owner preserve owner (super-user only)

-D same as –devices –specials

–devices preserve device files (super-user only)

–specials preserve special files

It is important to note that when using the archive flag there will be differences when recopying with simpler options. The files won’t match in attributes and will be copied again!

It is also important to note to SSH into a server requires the correct key files to be in place. SSH is a widely used remote shell for administration. With it you can copy files as if you were the super user on that linux machine provided you log in with that account. If you need to do more than backing up files and want to back up a system then it will be necessary unless you are on that machine locally.

The simplest method to rsync to a company server would be:

rsync -avzPn ‘~/Desktop/Files\ for\ Work/’ /Volumes/MountedServer/Mydirectory/Backups/

The ~/ or tilde before the slash is a unix shortcut to the logged in user’s home folder!

The tricky part here is the spaces within the syntax. Spaces generally are the breaks in syntax that the shell understands as a cue to move on to the next part of the expected syntax. So that “for Work” without the slashes and single quotes would look like the destination path and effectively breaks the command when that path isn’t found. A better explanation is here.

Running that command with the syntax all in place should give you a time for transfer and a speedup time with no to minimal errors. Next time I will tell you how to schedule it using cron tables built into OS X.




Josh Vamos

I am a post production audio professional by day and a filmmaker and tinkerer by night.

Leave a comment


email* (not published)