Example: Scheduling regular backups on macOS


#1

In case it helps anyone else, this is how I run backups on a regular basis on my MacBook.

  1. Use .bashrc or .bash_profile to export any environment variables your restic command may need, for example, the repository password. (This is convenient because it also allows you to get them in any Terminal you start.)

  2. Create a file called ~/Library/LaunchAgents/net.restic.backup.plist with these contents:

<?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>net.restic.backup</string>
	
	<key>ProgramArguments</key>
	<array>
		<string>/bin/bash</string>
		<string>-c</string>
		<string>source /Users/USERNAME/.bashrc ; restic backup DATA_FOLDER</string>
	</array>

	<key>StandardOutPath</key>
	<string>LOG_FILE</string>
	
	<key>StandardErrorPath</key>
	<string>LOG_FILE</string>
	
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>22</integer>
		<key>Minute</key>
		<integer>0</integer>
		<key>Weekday</key>
		<integer>3</integer>
	</dict>
</dict>
</plist>
  1. Replace USERNAME with your login username, DATA_FOLDER with the folder to back up (you should put your proper restic command there, actually), and LOG_FILE with the path to the log file to back up. If your env variables are in .bash_profile instead of .bashrc, then make that change too. If your Mac is mobile or backing up over a network, I highly recommend using the --hostname flag; see this thread

  2. This particular script is configured to run every 3rd day of the week (Wednesday; 0 is Sunday, 6 is Saturday) at 10pm. You can change this, of course.

  3. Run launchctl load -w ~/Library/LaunchAgents/net.restic.backup.plist to permanently load the task; the -w persists it through restarts (I think). Note that this will cause your backup to run when the computer is ON. If it is sleeping during the scheduled time, it will run when you log back in. If the computer is OFF, it will skip that particular run. See https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/ScheduledJobs.html for more info.

You can then invoke a backup to test it with launchctl start ~/Library/LaunchAgents/net.restic.backup (no .plist extension – start goes by the Label in the XML doc)

You can use unload -w similarly to stop the scheduled task.


#2

Very useful, thanks ! :+1:


#3

Epilogue: If you backup to a network location, and your network interface takes a few moments to come online like mine does, you might want to prepend the command (the third argument to ProgramArguments) with something like sleep 300 to wait 5 minutes before running the backup. However, if your computer powers down before 5 minutes, I don’t know what will happen, but my guess is that the backup will not run for that time period. Still, if you turn on your computer and usually leave it on for more than a few minutes it should help.


#4

I found another blog article on the same topic, so I’m including it here just for reference and diversity, even though it’s a bit like hijacking the thread :stuck_out_tongue:

http://www.jussihuotari.com/2017/01/14/computer-backup-using-restic-on-mac/