32

How do I set Time Machine to eject my external Time Machine backup drive automatically after each backup?

Pro Backup
  • 3,916
Adam A
  • 453
  • 4
    An interesting observation: when doing a TimeMachine backup to a network drive TimeMachine will automatically mount, backup and then unmount the remote TimeMachine share on the network drive. I wonder if you could exploit that to achieve what you want? – Ian C. May 25 '11 at 14:39

4 Answers4

28

~/bin/timemachine:

#!/bin/bash

d="Time Machine"  # (change this to match the name of your backup drive)
diskutil mount "$d" && tmutil startbackup -b && diskutil eject "$d"

~/Library/LaunchAgents/timemachine_eject.plist:

<?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>timemachine_eject</string>
    <key>Program</key>
    <string>/Users/username/bin/timemachine</string> <!-- Replace "username" with your username. "~/bin/timemachine" doesn't work -->
    <key>StartInterval</key>
    <integer>120</integer> <!-- run every two minutes for testing. -->
        <!-- Change this to a higher number like 43200 (run every 12 hours) once you've confirmed it works. -->
</dict>
</plist>

Make the script executable, unload the default plist, and load the new one:

chmod +x ~/bin/timemachine
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.backupd-auto.plist
launchctl load ~/Library/LaunchAgents/timemachine_eject.plist

Any time you want to make changes to the plist file, you have to unload and load it:

launchctl unload ~/Library/LaunchAgents/timemachine_eject.plist
launchctl load ~/Library/LaunchAgents/timemachine_eject.plist
abd3721
  • 105
Lri
  • 105,117
  • 1
    I love this solution, I further automated this process to mount and unmount work when I plug my drive and on a schedule. Visit my post here: http://somethinginteractive.com/blog/2013/07/24/time-machine-auto-mountunmount-drive-os-x/ – Mike Kormendy Jul 31 '13 at 06:07
  • On Maveriks this did not work for me, strangely the script looped and the backup was done continuously. The following change works though: ProgramArguments /Users/martin/bin/timemachine – Martin Jan 05 '15 at 14:21
  • somethinginteractive.com is down, archived version: https://web.archive.org/web/20160409130936/http://somethinginteractive.com/blog/2013/07/24/time-machine-auto-mountunmount-drive-os-x/ – Pro Backup Nov 27 '17 at 18:03
  • Could you explain why this works? – Pedro Paulo Amorim Nov 30 '21 at 13:59
  • com.apple.backupd-auto.plist doesn't seem to exist anymore on OS Ventura, and I ideally don't want to deactivate the default plist. I've made another version which might be helpful to others here: https://github.com/micahcarroll/auto-eject-time-machine – mdc Aug 27 '23 at 21:38
  • @mdc I liked your approach but wanted to ask about any gotcha's, sharp corners before testing it on my daily system. Also did you leave the Time Machine automatic backups enabled (meaning it is called every hour) or is your script the main driver (and it backups once a day)? – Halil Sen Oct 23 '23 at 14:06
7

Haven't tried this, but it looks like it might work:

Jettison (Mac App Store link)

Ejects external drives when going to sleep and remounts them waking from sleep.

Stu Wilson
  • 6,621
Jim Greer
  • 171
6

There might be a better way, but one solution might be to Applescript it. I haven't yet found any way to run an applescript after a backup, but you could:

  1. Turn off automatic TM backup
  2. Set up an applescript to run TM
    • Some googling turned up this line to force an immediate TM build: do shell script "/System/Library/CoreServices/backupd.bundle/Contents/Resources/backupd-helper >/dev/null 2>&1 &"
  3. Add a line to eject the disk afterwards.
    • eject disk somedrivename

If you want this to happen on a regular basis, you could attach it to a cron job.

Fishtoaster
  • 1,052
2

Jettison is a tool that automatically ejects all external disks when you put your Mac to sleep. It can be downloaded from the developer's software, or via brew:

brew install jettison

It costs 5$ after a 15 day trial period.

LKM
  • 113