DEV Community

mous-design
mous-design

Posted on

Plug in, back up, auto-eject: Time Machine for a disk you don't leave connected

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:

  1. Checks whether the disk that appeared is the correct disk.
  2. Checks with tmutil latestbackup when the last backup was.
  3. If it's older than MIN_INTERVAL (I use 12 hours), runs tmutil startbackup --block, which waits until the backup is done.
  4. 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):

  1. Set the disk name and interval at the top of the script.
  2. Set Time Machine to back up manually (otherwise it'll keep doing its own hourly thing).
  3. Give FolderActionsDispatcher Full Disk Access in System Settings / Privacy & Security. Without this, tmutil can't do its job from a Folder Action.
  4. Create a Folder Action in Automator on /Volumes that 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)