Skip to content

Instantly share code, notes, and snippets.

@pjv
Created October 8, 2013 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjv/6887598 to your computer and use it in GitHub Desktop.
Save pjv/6887598 to your computer and use it in GitHub Desktop.
Back up a mac to an external USB-connected hard disk using Time Machine but keep the hard disk unmounted in between backups.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>Label</key>
<string>mac.backup</string>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>3600</integer>
<key>Program</key>
<string>/path/to/tm_backup.sh</string>
</dict>
</plist>
#!/bin/bash
# This is the name of your backup disk
BACKUP_DISK="Backup"
# Mount the disk if it is unmounted
if ! mount | grep "$BACKUP_DISK" ; then
diskutil mount "$BACKUP_DISK"
fi
# If the drive is successfully mounted, do the backup (nothing will happen if disk is not connected)
if mount | grep "$BACKUP_DISK" ; then
# do backup, then unmount backup volume
tmutil startbackup --auto --block &&
diskutil eject "$BACKUP_DISK"
fi
@pjv
Copy link
Author

pjv commented Oct 8, 2013

These two files together let you back up to an external USB-connected hard disk using Time Machine on OS X but keep the hard disk unmounted in between backups.

You can put tm_backup.sh anywhere you want. You need to make it executable (chmod 755). I put mine inside the bin directory in my home directory.

You need to put mac.backup.plist inside the LaunchAgents folder inside Library in your home directory.

You need to configure two things inside the scripts:

tm_backup.sh: configure the name of your backup disk as the variable BACKUP_DISK

mac.backup.plist: configure the full path to where you put tm_backup.sh where it says <string>/path/to/tm_backup.sh</string>

Finally, once everything is configured and where it needs to be, issue this command in the terminal:

launchctl load ~/Library/LaunchAgents/mac.backup.plist

It should immediately mount your backup disk (if unmounted) and start a Time Machine backup and then unmount your backup disk when the backup is completed. Then every hour it will do the same thing again. If you want backups more or less often than once an hour, change the 3600 number to the number of seconds you want in between backups.

I keep the switch for Time Machine in the system preferences pane set on OFF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment