mac backup script

Most backup systems assume you are lazy and inattentive, and if they don’t do pretty much everything for you, it won’t get done. So let’s say you dutifully set up your backup software as directed, “set and forget.” It starts humming along, doing backups every so often on its own schedule.

Fast forward to months or years later. Your primary copy of your data has been ripped away from you, due to misadventure, fire, theft, or equipment failure, and you want to get it back. Surprise! Your backups stopped working shortly after you enabled them, for whatever reason: the backup disk failed or is no longer connected to your computer, the disk filled up, or the software is misconfigured. The most recent “backup” you have is many months out of date.

Even if I could 100 percent trust that “automatic” backups would never fail, I still wouldn’t want to do it that way. If you’re a programmer, you would never set up git so that it automatically commits everything you’ve changed every hour or so, would you? Me neither. I feel the same way about my backup software. I want backups to occur only when my files are in a known-good, consistent state.

That suggests manually-initiated backups. And that’s exactly what I did, for many years. I configured my backup software to stop backing up automatically. At the end of the day, or whenever I was going to be away from my Mac for more than an hour or two, I manually started a Time Machine backup and a BackBlaze backup.

That worked well enough, I guess. But there were problems.

When I’m about to step away from my computer, it’s often because I’m mentally exhausted. I don’t have a lot of brain-power left over for doing fiddly things like picking the exact right menu item, twice. This meant that one or both of my two regular backups would often not get done. Later, when I returned to my Mac, there was no good way for me to see if those backups had completed successfully.

At some point in there, I managed to screw up my Mac so badly that I needed to completely wipe it and start over. That’s when I discovered that, in this particular circumstance, a Time Machine backup is not ideal. It’s a lot better to have an exact clone of your old hard disk you can start from. So I added a third daily backup to my routine, via Carbon Copy Cloner.

And with that, my daily backup routine was officially too unwieldy to perform manually anymore. Automating activities such as these is why we have shell scripting.

Backup AppleScript Menu

After a lot of trial and error, I managed to produce a bash shell script that performs all three of my backups automatically. It also generates and manages logs that show what happened during every backup. I start my script with a small AppleScript in my Mac’s script menu, so that I don’t have to fiddle with a terminal window every time.

My backup script performs the following steps:

  • Turn off the Mac’s monitor.

  • Start three simultaneous backups: Time Machine, BackBlaze, and Carbon Copy Cloner. Redirect all of these programs’ terminal output to three separate session log files.

  • Wait for the backups to finish. This could take as little as ten minutes or as long as several days, but usually finishes within two hours.

  • Combine all the session logs into one master backup log file, and save it in a particular directory.

  • Delete old backup logs that are older than two weeks or so.

  • The backup is now complete. If the monitor is still turned off, we can assume a human is not using the computer, so put it to sleep. If the monitor is now on, then a human has started using it before the backup completed, so do not put the computer to sleep.

My backup script also performs a secondary purpose: It can show you the contents of the most recent backup logs, so you can judge for yourself if it completed correctly or not. This works even if a backup is currently in progress, in which case it will show you what it has got so far. If you’re really curious, you can navigate to the backup log directory and examine all the logs for the past two weeks.

Sounds easy enough, right? I am going to guess I put in at least three solid weeks’ worth of work on my backup script, over a period of many months, before I had nailed all the corner cases. I am stubborn like that.

For starters, when I first attempted to write this script, back in 2016, BackBlaze did not support command-line-initiated backups. I sent an email to a guy who works at the company, and I am happy to report that he implemented that feature, just because I asked nicely. That counts as dazzling customer support in my book.

(I’m not going to name The BackBlaze employee here, because I don’t want the poor guy to get drowned in emails from people bugging him with their pet grievances. If you think your issue is as important as I thought mine was, I suspect you can get ahold of somebody in the company some kind of way, the same as I did.)

If you’d like to try my backup script for yourself, download this zip file, and follow the instructions you find inside.

Note that you don’t have to automate all three types of backups, like I do. You can choose just one or two backup methods instead. I’m sure the script could be modified to support other types of backups, if you are so inclined.

If you have feedback or an idea for some kind of improvement, drop me a line.