In case it helps anyone else, this is how I run backups on a regular basis on my MacBook.
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.)
Create a file called ~/Library/LaunchAgents/net.restic.backup.plist with these contents:
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…
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.
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.
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.
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
I have written couple of scripts and launch agents for unmonitored backup. I’d love to get some feedback on it. Should I post it in this thread itself or make another threads? Or maybe start a “script showcase” kind of thread?
Purpose: backing up personal data (as of now 5.3GB) from Mac to B2 using two scripts (backup and maintenance) and schedule them both using launch agents.
[0] Restic file/dir include/exclude files, credentials file, and a file with env vars are as described in this gist here.
Issues and things I have not figured out:
How can I trigger desktop notifications whenever the scripts run (and possibly an email too, with summary of what happened - esp. for fail cases)?
I could not find any option to compress data - did I miss something? (this is a trial backup set – I have actual data set that’s a lot larger than this)
For subsequent backups I would like to see what actually changed i.e.
What files/folders were new
What were modified (changed/deleted etc)
And if that means a little bigger log output, I can live with that. The thing is every backup is adding ~20MB files to repo and I can’t see why (whether it’s the Mail/V5 folder and I wanted to make sure).
Or in general please suggest anything that I should change to make it better, simpler, and more portable.