If your Time Machine disk lives on a dock, you know the danger: unplug your laptop and you get a “Disk Not Ejected Properly” warning. Furthermore, if you leave the disk connected with default settings, Time Machine backs up every hour, filling it with way too many snapshots.
What I actually wanted was simple: when I plug the disk in, back up if it's been a while, then eject it so I can just yank the cable. So I wrote a small Folder Action that does exactly that.
How it works
macOS can run a script whenever something changes in a folder. Disks mount under /Volumes, so a Folder Action on /Volumes fires whenever a disk appears. The script then:
- Checks whether the disk that appeared is the correct disk.
- Checks with
tmutil latestbackupwhen the last backup was. - If it's older than
MIN_INTERVAL(I use 12 hours), runstmutil startbackup --block, which waits until the backup is done. - Unmounts the disk with
diskutil unmount, falling back to a forced unmount if needed.
Plug in, have a coffee, unplug. No clicking.
Failing visibly
One design choice I care about: if anything goes wrong, the disk stays mounted. A disk that's still there when you expected it gone is a visible signal that something needs a look. The reason ends up in ~/Library/Logs/backup-action.log. Silent failure is the worst kind of failure (for any code actually), specially for backups.
Setup
Nothing to install. It's one zsh script plus Automator (see README for details):
- Set the disk name and interval at the top of the script.
- Set Time Machine to back up manually (otherwise it'll keep doing its own hourly thing).
- Give
FolderActionsDispatcherFull Disk Access in System Settings / Privacy & Security. Without this,tmutilcan't do its job from a Folder Action. - Create a Folder Action in Automator on
/Volumesthat runs the script.
You can test it from Terminal first by passing the disk path as an argument, which is much nicer than plugging and unplugging while debugging.
Tested on macOS 26.
Get it
github.com/mous-design/backup-action (MIT)
If you have a different setup (multiple backup disks, network targets) I'd be curious how you'd extend it. Issues and PRs welcome.
Top comments (0)