MacOS – Run a launchd job after Time Machine backup

launchdmacostime-machine

Quite simply, I have a process that's triggered by launchd but currently runs once an hour, however really what I would like it to do is run once a Time Machine backup has completed.

Initially I thought I could just set my Time Machine backup (/Volumes/Foo/Backups.backupdb/Haravikk's Mac) as a watch path for launchd, however this seems to trigger much too early. It does seem to trigger again near the end, so I'm currently using a delay and then checking for the existence of a .inprogress backup, but it's not really an ideal solution, and sometimes Time Machine's cleanup stage will trigger the process multiple times.

Is there a more specific path that I could watch that will trigger my process only when Time Machine has finished a backup?

I realise I should perform my checks anyway to be sure, but currently my process is triggering several times per backup, which isn't what I want.

Best Answer

A simple solution is to use the tmutil command to manually trigger the backup and follow it up with your process (as part of a shell script that does both these steps).

  • First, turn off automatic backups from System Preferences > Time Machine or using the command tmutil disable
  • Use tmutil startbackup --auto --block to trigger a backup once every hour (the --block option is required to make the execution wait until the backup is finished)
  • Follow this with launching your process

See man tmutil for more information on using Time Machine from the command line.