Skip to content

Instantly share code, notes, and snippets.

@dmann
Last active August 29, 2015 14:18
Show Gist options
  • Save dmann/cde3662a604bd7057ac2 to your computer and use it in GitHub Desktop.
Save dmann/cde3662a604bd7057ac2 to your computer and use it in GitHub Desktop.
Overriding Time Machine's default behavior to do a single backup when a backup drive is connected and eject the drive when the backup is complete.
<!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>com.apple.TimeMachine_OnMount</string>
<key>StartOnMount</key>
<true/>
<key>Program</key>
<string>/usr/local/bin/timemachine_backup.sh</string>
</dict>
</plist>
#!/bin/bash
#------------------------------------------------------------------------------------------
# Launch control commands run to override Apple defaults
# sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.backupd-auto.plist
# launchctl load ~/Library/LaunchAgents/com.apple.TimeMachine_OnMount.plist
#------------------------------------------------------------------------------------------
# Set the drive name that we mount for our backups
# Conditional argument: if mounted entry has the volume name in the variable string
backup_mount_point=`/usr/bin/tmutil destinationinfo | grep 'Mount Point' | head -1 | cut -d : -f 2 | sed 's/^ *//'`
if [ "$backup_mount_point" != '' ]; then
echo Drive is mounted, so: backup and then eject
/usr/bin/tmutil startbackup -b -r &&
/usr/sbin/diskutil eject "$backup_mount_point"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment