Automatic Once-a-Day Time Machine Backups

Update: So in macOS 12 (and likely v11 and maybe v10) you have to give cron Full Disk Access for it to work. This seems less than ideal from a security perspective, so I’ve move to using launchd via a LaunchAgent plist file. Documentation on how to use cron is in an addendum in case you prefer that.


My wife is using an older MacBook Air from late 20101 and it can’t handle a Google Meet meeting, Chrome with a bunch of tabs open, and a Time Machine backup (all over WiFi) without the video meeting having trouble. We still wanted to back it up to the two TimeCapsules we have though.

I tried just having her run a backup manually when she was done for the day, but she’s tired then and so backups weren’t happening regularly at all. Thus, it was clear I needed a way to automate this. Turns out it’s not that difficult. This post explains how.

Quick summary

  • Setup Time Machine backup destinations
  • Run a first backup to each backup destination
  • Option 1 (described in an addendum since launchd is preferred by Apple):
    • Use crontab to setup a daily call to tmutil startbackup --auto --rotation
    • Use System Preferences > Security & Privacy > Full Disk Access to give /usr/sbin/cron full disk access
  • Option 2:
    • Create a LaunchAgent plist to invoke tmutil startbackup --auto --rotation at the desired time.
    • use sudo launchctl bootstrap gui/501/ /Users/{your-username}/Library/LaunchAgents/{your-launch-agent-plist-filename} to get the system to load and enable your
  • Use Energy Saver / Battery settings, or pmset repeat wake with appropriate time and days to wake up the computer a couple of minutes before that crontab command trigger time.

Step by Step instructions

  1. First step – create the Time Machine backup destinations in the Time Machine settings inside System Preferences.
  1. Run the first backup to each destination – either manually (via right-click on the destination drive icon in System Preferences > Time Machine view), or automatically by enabling “Back Up Automatically” in Time Machine settings and then waiting until they’ve both completed their first backup (don’t forget to turn off automatic once the first backup to each has run!).
  1. Decide what time you want your daily backup to run. I chose 12:59AM.
  1. Now create a LaunchAgent file for your task. The easiest way is to use LingonX, but you can create the file in a text editor like the excellent BBEdit and may want to refer to this very helpful documentation for launchd.

Name your file something that makes sense to you, I matched the file name to the label, so: “com.geekanddad.DailyTimeMachineBackup.plist” is what I used.

Contents should be something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.geekanddad.DailyTimeMachineBackup</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/tmutil</string>
    <string>startbackup</string>
    <string>--auto</string>
    <string>--rotation</string>
  </array>
  <key>StartCalendarInterval</key>
  <array>
    <dict>
      <key>Hour</key>
      <integer>00</integer>
      <key>Minute</key>
      <integer>59</integer>
    </dict>
  </array>
</dict>
</plist>
  1. If doing this manually with a text editor, you’ll need to save this file into ~/Library/LaunchAgents/ and then you’ll need to tell launchd about it via something like:

    launchctl bootstrap gui/501/ /Users/dad/Library/LaunchAgents/com.geekanddad.DailyTimeMachineBackup.plist

    Note 1: If you don’t know what your uid (User ID) is (it’s the number after “gui” in the service domain target argument above), you can use launchctl manageruid command to find out.
    Note 2: If this doesn’t work, try running it via sudo and you’ll get more verbose error messages.
  1. Now all you need to do is make sure your Mac is awake at 12:59am 🙂

    A note about the time: Since waking up triggers connecting to the network which triggers auto-fetching of mail, likely calendar syncing, and who knows what other Apple stuff and this was an old and slow Mac, I chose to wake it up at 12:57am so it would have 2 minutes to complete all of the network stuff before the Time Machine backup tried to start. This is because if it’s too slow sometimes Time Machine on this old version of macOS will decide that “the backup destination isn’t available” and it will fail.

    I think you can do this via the Energy Saver (“Battery” in macOS 12.x) System Preferences pane under “Schedule” (for macOS 12.x) by selecting the “Startup or wake” checkbox and picking “Every Day” and the time you chose above.

    I didn’t go this route because I didn’t want it to wake up a shutdown Mac which might not be plugged in and then my wife would wake up to a dead laptop (not good for my reputation! :)).

    However, if that would work for you, I recommend trying that out because it’s easier.

    What I did:

    sudo pmset repeat wake MTWRFSU 00:57:00

Which sets a repeating wake event for 12:57am every day of the week. You’ll note that I didn’t use wakeorpoweron here because I only want it to wake up a sleeping computer and do not want it to turn on a shutdown computer.

That’s it!

All she has to do now is leave her laptop open at night when she wants it backed up (it won’t wake up if the lid is shut and no external monitor is attached).


Addendum – using (crontab):
  1. Using Terminal, initiate editing of the crontab for the current user (which is what I did), or potentially a system crontab (I didn’t look into this):

crontab -e

This puts you into your terminal editor with the current user’s crontab file open. Once there, add the following two lines

# For daily backups
59 0 * * * /usr/bin/tmutil startbackup --auto --rotation

This crontab entry says to run the tmutil command at 59 minutes after midnight every day. The parameters to tmutil are telling it to start a backup, run it as if it was a system initiated backup, and to automatically rotate between multiple backup destinations (you only need the last parameter if you have multiple defined).

  1. Give cron permissions necessary to run
    1. Open “System Preferences…” from the Apple menu and select the “Security & Privacy” icon.
    2. Then select the “Privacy” tab and scroll through the list on the left and select “Full Disk Access.”
    3. Select the lock icon in the lower left and enter an administrator’s credentials.
    4. select the + icon below the list of applications
    5. type Cmd+shift+g to display the path entry file selection interface and type: /usr/sbin/cron and select the cron binary

The window should look like this when you’ve got it added properly:

Security & Privacy panel inside System Preferences with the Privacy tab selected and Full Disk Access selected from the list on the left.  Shows the cron command line tool in the list on the right with the enabled checkbox to the left of the tool name checked.


Footnotes

1) She’s since upgraded to a late 2020 MacBook Air and is loving it, as you’d imagine :). Retina screen, insanely faster, insanely long battery life, etc. Only thing she’s annoyed by is all the dongles required to connect to her external keyboard, mouse, and external display 🙂 (back)