Nathan Grigg

Automounting Time Machine

I use Time Machine to back up my home iMac to a USB external hard drive. But I don’t want the Time Machine volume mounted all of the time. It adds clutter and slows down Finder.

I’ve been using a shell script and a Launch Agent to automatically mount my Time Machine volume, back it up, and unmount it again.

Since this takes care of running Time Machine, I have Time Machine turned off in System Preferences.

Shell script

The shell script used to be more complicated, but Apple has been been improving their tools. You could actually do this in three commands:

  1. Mount the volume (line 6).
  2. Start the backup (line 14). The --block flag prevents the command from exiting before the backup is complete.
  3. Eject the volume (line 16).

Everything else is either logging or to make sure that I only eject the volume if it wasn’t mounted to begin with. In particular, line 4 checks if the Time Machine volume is mounted at the beginning.

 1 #!/bin/bash
 2 date=$(date +"%Y-%m-%d %H:%M:%S")
 4 if [[ -d "/Volumes/Time Machine Backups" ]]; then
 5     eject=false
 6 elif diskutil quiet mount "Time Machine Backups"; then
 7     eject=true
 8 else
 9     echo>&2 "$date Cannot mount backup volume"
10     exit 1
11 fi
13 echo $date Starting backup
14 if tmutil startbackup --block; then
15     echo $date Backup finished
16     if [[ $eject = true ]]; then
17         diskutil quiet eject "Time Machine Backups"
18     fi
19 else
20     echo>&2 "$date Backup failed"
21     exit 1
22 fi

Launch Agent

Nothing complicated here. This uses launchd to run the shell script every two hours and capture the output to a log file.

I save this as “net.nathangrigg.time-machine.plist” in “/Library/LaunchDaemons”, so that it is run no matter who is logged in. If you do this, you need to use chown to set the owner to root, or it will not be run.

If you are the only one that uses your computer, you can just save it in “~/Library/LaunchAgents”, and you don’t have to worry about changing the owner.

Either way, run launchctl load /path/to/plist to load your agent for the first time. (Otherwise, it will load next time you log in to your computer.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">


OS X will still mount your Time Machine volume every time you log in. You can fix this by adding one line to “/etc/fstab” (which you may need to create).

UUID=79CA38B7-BA13-4A15-A080-D3A8B568D860 none hfs rw,noauto

Replace the UUID with your drive’s UUID, which you can find using diskutil info "/Volumes/Time Machine Backups". For more detailed instructions, see this article by Topher Kessler.