Blog Posts

Automatically switching between different Time Machine disks

After having finally switched to Leopard during my paternity leave (thanks again Liip for that :)) I started using Time Machine for my backup needs. Since I had an almost-always running Popcorn Hour with some spare space, the wish to use that device was kinda obvious. While not officially supported by Apple (for whatever reasons) setting it up on the samba share was pretty easy and it works pretty flawless since then.

Back at work I of course didn't want to miss the hourly backups anymore and needed a solution there too. So I did the same with a disk attached to our airport. Worked equally flawless and since Time Machine seems to handle different and changing backup disks correctly all I had to do was to switch the Time Machine disk every morning and evening. I now have two full backups of my disk, one at home and one at the office, geographically separated by approx. 2 kilometers, which should be enough for most possible disasters. And if that's not enough, then I'm not sure if I care about my backups anymore :)

But the switching every day between the two disks was cumbersome and easily forgotten, so I wanted to automate that. With some googling I found out, that the destination disk was written in the " /Library/Preferences/com.apple.TimeMachine BackupAlias" property and could be read with

defaults read /Library/Preferences/com.apple.TimeMachine BackupAlias

This gives you a rather long string starting with something like <00000000 014e0002 .... That's what you need for later. And for both disks.

Next I wrote two little shell scripts, looking like this:

#!/bin/bash
defaults write /Library/Preferences/com.apple.TimeMachine BackupAlias '<00000000 014e0002 ....>'

and started them, when needed. No more half a dozen mouse-clicks and typing in that password every time, just calling that script was enough, but still not perfect. Jiayong Ou to the rescue, which made me aware of the context-and-location-aware and free application MarcoPolo. A little bit of configuring and I have now a fully automated backup solution at home and at the office. I additionally added the line

/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper -auto &

to my script and a delay of 30 seconds in MarcoPolo, so that my backup started immediately when I'm online again and after the disk preference actually switched. Doesn't work always, since it's aborting a maybe running old backup, but seems not to do harm either.

That was it. A cool little application and a 2 line script is enough for making me forget about doing my backups :)

And as always: This works for me, but may completely destroy something on your side. And it's not officially supported by Apple and may therefore break with the next update. Use at your own risk

Related Entries:
- php-osx available for OS X 10.10 Yosemite
- PHP 5.6.0-alpha1 for php-osx released
- PHP 5.4.0RC1 for OS X on php-osx.liip.ch
- Easily test PHP 5.4 beta on OS X with php-osx
- Leopard native apache with custom (64bit) PHP

About the author

Comments [16]

Michel, 20.01.2009 19:20 CEST

cool solution. btw: do you use any harddrive encryption (filevault or something like that)?

Chregu, 20.01.2009 19:29 CEST

Michel: No, I don't use any encryption for now, maybe I should try :)

Chregu, 21.01.2009 11:33 CEST

Looks like Time Machine doesn't mount encrypted images, so that doesn't work :(

Adi, 21.01.2009 17:18 CEST

Time Machine cannot make backups to encrypted volumes (yet). If you use FileVault for your home directory, Time Machine backs up the encrypted sparse bundle, so you have sort of an encrypted backup. The drawback is, that you can't use Time Machine the usual way. It backs up your data only after you logged out (and the encrypted sparse bundle has been unmounted). So no hourly backups.

Steven, 31.01.2009 01:58 CEST

I was just thinging it would be nice to write a Time Machine preferences switcher.

It would be nice to have time machine back up just my photos and documents to my Amazon S3 on weekends, and switch back to full, local backups on weekdays.

An interface like iPhoto Buddy and I think the back-end part would be pretty simple (just swapping files).

Kirk Kerekes, 01.02.2009 16:26 CEST

Note:
1. Direct file access by the defaults tool is deprecated and will go away (see man 1 defaults).
2. Defaults will not access TimeMachine's defaults via the normal defaults mechanisms.
3. Thus, this fix is doomed.

Steven, 01.02.2009 20:24 CEST

@Kirk: took me a while to find what you were referring to. I think this is it:

http://developer.apple.com/DOCUMENTATION/Darwin/Reference/ManPages/man1/defaults.1.html

"WARNING: The defaults command will be changed in an upcoming major release to only operate on preferences domains. General plist manipulation utilities will be folded into a different command-line program."

Not sure what you mean by (2.) but it seems like this hint isn't doomed, it will just get shorter:

"defaults write /Library/Preferences/com.apple.TimeMachine ..."

will become:

"defaults write com.apple.TimeMachine ..."

Or, if all else fails, we can use the mentioned 'different command line program' for manual plist manipulation.

I think half the posts on macosxhints.com will need to be updated though. :D

Tim Hubbard, 30.03.2009 07:56 CEST

I'm curious about how time machine manages the effect of switching disks. As I understand it, the fsevents file only keeps a record of files that have changed since the last backup, however if that was at home, then if TM uses that file when you get to work, the work copy would be missing all the file changes backedup at home (and visa versa). Or does switching disk trigger a full disk scan so the first backup at each location takes ages (particularly over wireless)? I assume the latter, but this seems a lot disk scanning, twice a day. Tim

Chregu, 30.03.2009 08:02 CEST

I don't know the inner details. It's usually fast enough, so I doubt it does a full disk scan.

Outis, 09.04.2009 21:45 CEST

Hello, I'm a beginner in all this, but my boss wants me to set up his TM to backup to disks at work and at home. I would like to try your solution, but don't understand exactly how to do it. Could you please put it in a step by step for dummies?

Much appreciated!

Fintan, 17.04.2009 14:28 CEST

Would also appreciate a dummies guide as I would like to automate switching TM backup disks

Olav, 23.09.2009 09:56 CEST

Hello. I was wondering what tool TimeMachine use to write that property. If you open the lines with a HexEdit tool you can see the paths, but I'd like to know for sure so I don't mess up anything.
I'd like to be able to choose backup location without ever having to set it in System preferences first...
Like: user type (or choose) the path, and the tool wrights it to the plist.

Fearandil, 05.05.2010 09:56 CEST

Also the property DestinationVolumeUUID should be set.

Jonathan, 05.01.2012 22:36 CEST

Got your solution through Google Search... And finally ended up using tmutil (man tmutil in terminal), which was introduced by OS X Lion.

Thanks a lot for the help, anyway !

Chregu, 05.01.2012 22:54 CEST

Cool, didn't know about tmutil. Thanks for the hint.

Woggly, 18.12.2013 18:55 CEST

Do you know of any way to decode the alias into something human readable? I'm writing a script for work, and am curious as to where people are backing up to.

Add a comment

Your email adress will never be published. Comment spam will be deleted!