From 193bf926cff7b409a58d238ff44ed8f2853abd46 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Mon, 7 Feb 2022 17:47:12 +0100 Subject: [PATCH 01/21] PoC souce files --- ...github.erikw.restic-automatic-backup.plist | 20 +++++++ Makefile | 58 ++++++++++++------- 2 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist new file mode 100644 index 0000000..3d42ca4 --- /dev/null +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -0,0 +1,20 @@ + + + + + + + Label + com.github.erikw.restic-automatic-backup + ProgramArguments + + /bin/sh + -c + . /Users/erikw/tmp/env.sh && /Users/erikw/tmp/exe.sh + + RunAtLoad + + StartInterval + 10 + + diff --git a/Makefile b/Makefile index ecc420d..aa580f7 100644 --- a/Makefile +++ b/Makefile @@ -39,45 +39,52 @@ MKDIR_PARENTS=sh -c '\ ' MKDIR_PARENTS # Source directories. -DIR_SCRIPT = bin -DIR_CONF = etc/restic -DIR_SYSTEMD = usr/lib/systemd/system -DIR_CRON = etc/cron.d +DIR_SCRIPT = bin +DIR_CONF = etc/restic +DIR_SYSTEMD = usr/lib/systemd/system +DIR_CRON = etc/cron.d +DIR_LAUNCHAGENT = Library/LaunchAgents # Source files. SRCS_SCRIPT = $(filter-out %cron_mail, $(wildcard $(DIR_SCRIPT)/*)) SRCS_CONF = $(wildcard $(DIR_CONF)/*) SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*) SRCS_CRON = $(wildcard $(DIR_CRON)/*) +SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) # Local build directory. Sources will be copied here, # modified and then installed from this directory. BUILD_DIR := build -BUILD_DIR_SCRIPT = $(BUILD_DIR)/$(DIR_SCRIPT) -BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF) -BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD) -BUILD_DIR_CRON = $(BUILD_DIR)/$(DIR_CRON) +BUILD_DIR_SCRIPT = $(BUILD_DIR)/$(DIR_SCRIPT) +BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF) +BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD) +BUILD_DIR_CRON = $(BUILD_DIR)/$(DIR_CRON) +BUILD_DIR_LAUNCHAGENT = $(BUILD_DIR)/$(DIR_LAUNCHAGENT) # Sources copied to build directory. -BUILD_SRCS_SCRIPT = $(addprefix $(BUILD_DIR)/, $(SRCS_SCRIPT)) -BUILD_SRCS_CONF = $(addprefix $(BUILD_DIR)/, $(SRCS_CONF)) -BUILD_SRCS_SYSTEMD = $(addprefix $(BUILD_DIR)/, $(SRCS_SYSTEMD)) -BUILD_SRCS_CRON = $(addprefix $(BUILD_DIR)/, $(SRCS_CRON)) +BUILD_SRCS_SCRIPT = $(addprefix $(BUILD_DIR)/, $(SRCS_SCRIPT)) +BUILD_SRCS_CONF = $(addprefix $(BUILD_DIR)/, $(SRCS_CONF)) +BUILD_SRCS_SYSTEMD = $(addprefix $(BUILD_DIR)/, $(SRCS_SYSTEMD)) +BUILD_SRCS_CRON = $(addprefix $(BUILD_DIR)/, $(SRCS_CRON)) +BUILD_SRCS_LAUNCHAGENT = $(addprefix $(BUILD_DIR)/, $(SRCS_LAUNCHAGENT)) # Destination directories DEST_DIR_SCRIPT = $(PREFIX)/$(DIR_SCRIPT) DEST_DIR_CONF = $(PREFIX)/$(DIR_CONF) DEST_DIR_SYSTEMD = $(PREFIX)/$(DIR_SYSTEMD) DEST_DIR_CRON = $(PREFIX)/$(DIR_CRON) +DEST_DIR_LAUNCHAGENT= /$(DIR_LAUNCHAGENT) # Destination file targets. -DEST_TARGS_SCRIPT = $(addprefix $(PREFIX)/, $(SRCS_SCRIPT)) -DEST_TARGS_CONF = $(addprefix $(PREFIX)/, $(SRCS_CONF)) -DEST_TARGS_SYSTEMD = $(addprefix $(PREFIX)/, $(SRCS_SYSTEMD)) -DEST_TARGS_CRON = $(addprefix $(PREFIX)/, $(SRCS_CRON)) +DEST_TARGS_SCRIPT = $(addprefix $(PREFIX)/, $(SRCS_SCRIPT)) +DEST_TARGS_CONF = $(addprefix $(PREFIX)/, $(SRCS_CONF)) +DEST_TARGS_SYSTEMD = $(addprefix $(PREFIX)/, $(SRCS_SYSTEMD)) +DEST_TARGS_CRON = $(addprefix $(PREFIX)/, $(SRCS_CRON)) +DEST_TARGS_LAUNCHAGENT = $(addprefix /, $(SRCS_LAUNCHAGENT)) INSTALLED_FILES = $(DEST_TARGS_SCRIPT) $(DEST_TARGS_CONF) \ - $(DEST_TARGS_SYSTEMD) $(DEST_TARGS_CRON) + $(DEST_TARGS_SYSTEMD) $(DEST_TARGS_CRON) \ + $(DEST_TARGS_LAUNCHAGENT) #### Targets ################################################################## @@ -101,17 +108,23 @@ uninstall: # $ PREFIX=/usr/local make install-systemd # $ PREFIX=/tmp/test make install-systemd # target: install-systemd - Install systemd setup. -install-systemd: install-targets-script install-targets-conf install-targets-systemd +install-systemd: install-targets-script install-targets-conf \ + install-targets-systemd # target: install-cron - Install cron setup. install-cron: install-targets-script install-targets-conf install-targets-cron +# target: install-launchagent - Install LaunchAgent setup. +install-launchagent: install-targets-script install-targets-conf \ + install-targets-launchagent + # Install targets. Prereq build sources as well, # so that build dir is re-created if deleted. install-targets-script: $(DEST_TARGS_SCRIPT) $(BUILD_SRCS_SCRIPT) install-targets-conf: $(DEST_TARGS_CONF) $(BUILD_SRCS_CONF) -install-targets-systemd: $(DEST_TARGS_SYSTEMD) $(BUILD_SRCS_SYSTEMD) -install-targets-cron: $(DEST_TARGS_CRON) $(BUILD_SRCS_CRON) +install-targets-systemd: $(DEST_TARGS_SYSTEMD) $(BUILD_SRCS_SYSTEMD) +install-targets-cron: $(DEST_TARGS_CRON) $(BUILD_SRCS_CRON) +install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) $(BUILD_SRCS_LAUNCHAGENT) # Copies sources to build directory & replace "$INSTALL_PREFIX". $(BUILD_DIR)/% : % @@ -138,3 +151,8 @@ $(DEST_DIR_SYSTEMD)/%: $(BUILD_DIR_SYSTEMD)/% $(DEST_DIR_CRON)/%: $(BUILD_DIR_CRON)/% @${MKDIR_PARENTS} $@ install -m 0644 $< $@ + +# Install destination launchagent files. +$(DEST_DIR_LAUNCHAGENT)/%: $(BUILD_DIR_LAUNCHAGENT)/% + ${MKDIR_PARENTS} $@ + install -m 0444 $< $@ From db26c4e5176db8211179ce59900134168d751100 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Mon, 7 Feb 2022 18:12:26 +0100 Subject: [PATCH 02/21] Launch instructions --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index e9113fa..f90d9bc 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Tip: use the Section icon in the top left of this document to navigate the secti # Setup Depending on your system, the setup will look different. Choose one of * [Linux + Systemd](#setup-linux-systemd) +* [macOS + LaunchAgent](#setup-macos-launchagent) * [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. ## Setup Linux Systemd @@ -260,6 +261,12 @@ straightforward (it needs to run with sudo to read environment). Just run: | `resticw stats` / `resticw stats snapshot-id ...` | Show the statistics for the whole repo or the specified snapshots | | `resticw mount /mnt/restic` | Mount your remote repository | +## Setup macOS LaunchAgent + +```console +$ sudo PREFIX=/usr/local make install-launchagent +$ launchctl load -w /Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +``` ## Setup Cron If you want to run an all-classic cron job instead, do like this: From 7973eedae4fcabe376e8d0f058fef22f307ed53d Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Mon, 7 Feb 2022 19:06:56 +0100 Subject: [PATCH 03/21] Modified: Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index aa580f7..6131ce6 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) # Local build directory. Sources will be copied here, # modified and then installed from this directory. -BUILD_DIR := build +BUILD_DIR := build BUILD_DIR_SCRIPT = $(BUILD_DIR)/$(DIR_SCRIPT) BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF) BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD) From ddd2233c5844d87d3d9267d9bdf52ae564d559e0 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Mon, 7 Feb 2022 19:20:13 +0100 Subject: [PATCH 04/21] Install LaunchAgent in user Library --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6131ce6..5d74dcc 100644 --- a/Makefile +++ b/Makefile @@ -73,14 +73,14 @@ DEST_DIR_SCRIPT = $(PREFIX)/$(DIR_SCRIPT) DEST_DIR_CONF = $(PREFIX)/$(DIR_CONF) DEST_DIR_SYSTEMD = $(PREFIX)/$(DIR_SYSTEMD) DEST_DIR_CRON = $(PREFIX)/$(DIR_CRON) -DEST_DIR_LAUNCHAGENT= /$(DIR_LAUNCHAGENT) +DEST_DIR_LAUNCHAGENT= $(HOME)/$(DIR_LAUNCHAGENT) # Destination file targets. DEST_TARGS_SCRIPT = $(addprefix $(PREFIX)/, $(SRCS_SCRIPT)) DEST_TARGS_CONF = $(addprefix $(PREFIX)/, $(SRCS_CONF)) DEST_TARGS_SYSTEMD = $(addprefix $(PREFIX)/, $(SRCS_SYSTEMD)) DEST_TARGS_CRON = $(addprefix $(PREFIX)/, $(SRCS_CRON)) -DEST_TARGS_LAUNCHAGENT = $(addprefix /, $(SRCS_LAUNCHAGENT)) +DEST_TARGS_LAUNCHAGENT = $(addprefix $(HOME)/, $(SRCS_LAUNCHAGENT)) INSTALLED_FILES = $(DEST_TARGS_SCRIPT) $(DEST_TARGS_CONF) \ $(DEST_TARGS_SYSTEMD) $(DEST_TARGS_CRON) \ From bd3123bef434604a4bcc4f7b1b6425cce395b112 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Mon, 7 Feb 2022 20:05:45 +0100 Subject: [PATCH 05/21] Modified: Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5d74dcc..b611604 100644 --- a/Makefile +++ b/Makefile @@ -96,7 +96,7 @@ help: clean: $(RM) -r $(BUILD_DIR) -# target: uninstall - Uninstall ALL files from all install targets. +# target: uninstall - Uninstall ALL installed (including config) files. uninstall: @for file in $(INSTALLED_FILES); do \ echo $(RM) $$file; \ From e9d77d0a31450a2d7d29c2ce4fed7bb465fffa84 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Tue, 8 Feb 2022 16:56:41 +0100 Subject: [PATCH 06/21] Surpress mkdir --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b611604..182cc77 100644 --- a/Makefile +++ b/Makefile @@ -154,5 +154,5 @@ $(DEST_DIR_CRON)/%: $(BUILD_DIR_CRON)/% # Install destination launchagent files. $(DEST_DIR_LAUNCHAGENT)/%: $(BUILD_DIR_LAUNCHAGENT)/% - ${MKDIR_PARENTS} $@ + @${MKDIR_PARENTS} $@ install -m 0444 $< $@ From 2f4f48a59bfae59508d39a0c367e1588de911b5f Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 13:09:10 +0100 Subject: [PATCH 07/21] Modified: CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3c9ce9..145fc2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- macos LaunchAgent support. Install with `make install-launchagent`. ## [5.0.0] - 2022-02-08 ### Added From f7f69a0142f0ff6c37ef9c37ccf0bf571f5e9cba Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 13:32:19 +0100 Subject: [PATCH 08/21] instructions --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f90d9bc..9b65bbd 100644 --- a/README.md +++ b/README.md @@ -262,12 +262,17 @@ straightforward (it needs to run with sudo to read environment). Just run: | `resticw mount /mnt/restic` | Mount your remote repository | ## Setup macOS LaunchAgent +LaunchAgent is the modern service schedulerin in macOS that uses [Launchd](https://www.launchd.info/). +[Launchd](https://www.launchd.info/) is the modern built-in service scheduler in macOS. It has support for running services as root (Daemon) or as a normal user (Agent). Here we we set up an Lauch Agent to be run as your normal user for starting regular backups. ```console -$ sudo PREFIX=/usr/local make install-launchagent -$ launchctl load -w /Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +$ PREFIX=/usr/local make install-launchagent +$ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist # Edit schedule if needed. +$ launchctl load -w ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +$ launchctl kickstart gui/501/com.github.erikw.restic-automatic-backup ``` + ## Setup Cron If you want to run an all-classic cron job instead, do like this: From c1c25d718c8a147386cb58592bba3d408b7ee439 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 13:32:25 +0100 Subject: [PATCH 09/21] schedule --- .../com.github.erikw.restic-automatic-backup.plist | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index 3d42ca4..92f12f5 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -10,11 +10,19 @@ /bin/sh -c - . /Users/erikw/tmp/env.sh && /Users/erikw/tmp/exe.sh + . /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh RunAtLoad - StartInterval - 10 + + StartCalendarInterval + + + Hour + 19 + Minute + 0 + + From c8ab0b4421252664554c4ababb6d9b8d74ce2cc2 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 14:04:07 +0100 Subject: [PATCH 10/21] iterate --- .../com.github.erikw.restic-automatic-backup.plist | 14 ++++++++++++-- Makefile | 8 +++++++- README.md | 4 ++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index 92f12f5..e4021a5 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -8,9 +8,9 @@ com.github.erikw.restic-automatic-backup ProgramArguments - /bin/sh + /bin/bash -c - . /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh + source /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh RunAtLoad @@ -24,5 +24,15 @@ 0 + EnvironmentVariables + + PATH + /usr/local/bin:/usr/bin:/bin + + + StandardOutPath + /Users/erikw/Library/Logs/restic/restic_stdout.txt + StandardErrorPath + /Users/erikw/Library/Logs/restic/restic_stderr.txt diff --git a/Makefile b/Makefile index 182cc77..62b80d0 100644 --- a/Makefile +++ b/Makefile @@ -74,6 +74,7 @@ DEST_DIR_CONF = $(PREFIX)/$(DIR_CONF) DEST_DIR_SYSTEMD = $(PREFIX)/$(DIR_SYSTEMD) DEST_DIR_CRON = $(PREFIX)/$(DIR_CRON) DEST_DIR_LAUNCHAGENT= $(HOME)/$(DIR_LAUNCHAGENT) +DEST_DIR_MAC_LOG = $(HOME)/Library/Logs/restic # Destination file targets. DEST_TARGS_SCRIPT = $(addprefix $(PREFIX)/, $(SRCS_SCRIPT)) @@ -124,7 +125,8 @@ install-targets-script: $(DEST_TARGS_SCRIPT) $(BUILD_SRCS_SCRIPT) install-targets-conf: $(DEST_TARGS_CONF) $(BUILD_SRCS_CONF) install-targets-systemd: $(DEST_TARGS_SYSTEMD) $(BUILD_SRCS_SYSTEMD) install-targets-cron: $(DEST_TARGS_CRON) $(BUILD_SRCS_CRON) -install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) $(BUILD_SRCS_LAUNCHAGENT) +install-targets-launchagent: $(DEST_TARGS_LAUNCHAGENT) \ + $(BUILD_SRCS_LAUNCHAGENT) $(DEST_DIR_MAC_LOG) # Copies sources to build directory & replace "$INSTALL_PREFIX". $(BUILD_DIR)/% : % @@ -156,3 +158,7 @@ $(DEST_DIR_CRON)/%: $(BUILD_DIR_CRON)/% $(DEST_DIR_LAUNCHAGENT)/%: $(BUILD_DIR_LAUNCHAGENT)/% @${MKDIR_PARENTS} $@ install -m 0444 $< $@ + +# Install destination mac log dir. +$(DEST_DIR_MAC_LOG): + mkdir -p $@ diff --git a/README.md b/README.md index 9b65bbd..758fe8c 100644 --- a/README.md +++ b/README.md @@ -268,8 +268,8 @@ LaunchAgent is the modern service schedulerin in macOS that uses [Launchd](https ```console $ PREFIX=/usr/local make install-launchagent $ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist # Edit schedule if needed. -$ launchctl load -w ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist -$ launchctl kickstart gui/501/com.github.erikw.restic-automatic-backup +$ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup +$ launchctl kickstart -p gui/$UID/com.github.erikw.restic-automatic-backup ``` From d38616d697dc9a0ff6e90812ec6f1f48b50e4bcd Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 14:58:31 +0100 Subject: [PATCH 11/21] Path working --- ...github.erikw.restic-automatic-backup.plist | 22 +++++++++---------- README.md | 11 ++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index e4021a5..5cf32db 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -6,12 +6,22 @@ Label com.github.erikw.restic-automatic-backup + EnvironmentVariables + + PATH + /usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + ProgramArguments /bin/bash -c - source /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh + export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin && source /usr/local/etc/restic/erikw.env.sh && PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin /usr/local/bin/restic_backup.sh + + StandardOutPath + /Users/erikw/Library/Logs/restic/restic_stdout.log + StandardErrorPath + /Users/erikw/Library/Logs/restic/restic_stderr.log RunAtLoad @@ -24,15 +34,5 @@ 0 - EnvironmentVariables - - PATH - /usr/local/bin:/usr/bin:/bin - - - StandardOutPath - /Users/erikw/Library/Logs/restic/restic_stdout.txt - StandardErrorPath - /Users/erikw/Library/Logs/restic/restic_stderr.txt diff --git a/README.md b/README.md index 758fe8c..7986436 100644 --- a/README.md +++ b/README.md @@ -268,10 +268,21 @@ LaunchAgent is the modern service schedulerin in macOS that uses [Launchd](https ```console $ PREFIX=/usr/local make install-launchagent $ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist # Edit schedule if needed. +$ launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist $ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup $ launchctl kickstart -p gui/$UID/com.github.erikw.restic-automatic-backup ``` +Debug with +```console +$ sudo launchctl debug gui/$UID/com.github.erikw.restic-automatic-backup --stdout --stderr +``` + +TODO how disable? +``` +$ launchctl disable gui/$UID/com.github.erikw.restic-automatic-backup +$ launchctl bootout gui/$UID/com.github.erikw.restic-automatic-backup +``` ## Setup Cron If you want to run an all-classic cron job instead, do like this: From 9412a8750c18f182d16615fe37bb42a8375e0ac1 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:08:57 +0100 Subject: [PATCH 12/21] trim --- .../LaunchAgents/com.github.erikw.restic-automatic-backup.plist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index 5cf32db..186bd63 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -15,7 +15,7 @@ /bin/bash -c - export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin && source /usr/local/etc/restic/erikw.env.sh && PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin /usr/local/bin/restic_backup.sh + source /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh StandardOutPath From 1977b3c67f84846a1c561f82894df17e1dea3058 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:31:42 +0100 Subject: [PATCH 13/21] Add OS icons --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7986436..97a7f57 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ Tip: use the Section icon in the top left of this document to navigate the secti # Setup Depending on your system, the setup will look different. Choose one of -* [Linux + Systemd](#setup-linux-systemd) -* [macOS + LaunchAgent](#setup-macos-launchagent) +* [Linux + Systemd](#setup-linux-systemd) +* [macOS + LaunchAgent](#setup-macos-launchagent) * [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. ## Setup Linux Systemd From a28160371c60e880bd7c8a7412117201cb1feda3 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:45:49 +0100 Subject: [PATCH 14/21] Fix log path --- ...github.erikw.restic-automatic-backup.plist | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index 186bd63..f859942 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -6,22 +6,21 @@ Label com.github.erikw.restic-automatic-backup - EnvironmentVariables - - PATH - /usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin - ProgramArguments /bin/bash -c - source /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh + source /usr/local/etc/restic/erikw.env.sh && /usr/local/bin/restic_backup.sh >>$HOME/$LOG_OUT 2>>$HOME/$LOG_ERR - - StandardOutPath - /Users/erikw/Library/Logs/restic/restic_stdout.log - StandardErrorPath - /Users/erikw/Library/Logs/restic/restic_stderr.log + EnvironmentVariables + + PATH + /usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + LOG_OUT + /Library/Logs/restic/restic_stdout.log + LOG_ERR + /Library/Logs/restic/restic_stderr.log + RunAtLoad From f073f10ddfafbfccd91b2c34b70c428689a00a66 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:47:04 +0100 Subject: [PATCH 15/21] Modified: Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist --- .../LaunchAgents/com.github.erikw.restic-automatic-backup.plist | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index f859942..540223a 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -7,6 +7,7 @@ Label com.github.erikw.restic-automatic-backup ProgramArguments + /bin/bash -c From c4429681d823e25ae195d84b814b43a37bc4b253 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:49:25 +0100 Subject: [PATCH 16/21] Modified: Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist --- .../LaunchAgents/com.github.erikw.restic-automatic-backup.plist | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist index 540223a..ca468c0 100644 --- a/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist +++ b/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist @@ -1,6 +1,7 @@ + From 991476715a163a08069c59b62e7d695475f630c8 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:56:37 +0100 Subject: [PATCH 17/21] install instructions --- Makefile | 1 + README.md | 35 ++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 62b80d0..a68ebd0 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,4 @@ +# TODO add install for launchagent completely, and unisntall target using bootstrap? #### Notes #################################################################### # This build process is done in three stages (out-of-source build): # 1. copy source files to the local build directory. diff --git a/README.md b/README.md index 97a7f57..3d64e9c 100644 --- a/README.md +++ b/README.md @@ -262,23 +262,28 @@ straightforward (it needs to run with sudo to read environment). Just run: | `resticw mount /mnt/restic` | Mount your remote repository | ## Setup macOS LaunchAgent -LaunchAgent is the modern service schedulerin in macOS that uses [Launchd](https://www.launchd.info/). -[Launchd](https://www.launchd.info/) is the modern built-in service scheduler in macOS. It has support for running services as root (Daemon) or as a normal user (Agent). Here we we set up an Lauch Agent to be run as your normal user for starting regular backups. +LaunchAgent is the modern service scheduler in in macOS that uses [Launchd](https://www.launchd.info/). +[Launchd](https://www.launchd.info/) is the modern built-in service scheduler in macOS. It has support for running services as root (Daemon) or as a normal user (Agent). Here we we set up an LauchAgent to be run as your normal user for starting regular backups. -```console -$ PREFIX=/usr/local make install-launchagent -$ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist # Edit schedule if needed. -$ launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist -$ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup -$ launchctl kickstart -p gui/$UID/com.github.erikw.restic-automatic-backup -``` +1. In general, follow the same setup as in (#setup-linux-systemd) except taht + * use `make install-launchagent` instead of `make install-systemd` + * Install everything to `/usr/local` and run restic as your own use, not root + * Thus, install with + ```console + $ PREFIX=/usr/local make install-launchagent + ``` +1. After installation with `make` , edit the installed launchagent if you want to change the default schedule or profile used + ```console + $ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist + ``` +1. Now install, enable and kickstart the first run! + ```console + $ launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist + $ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup + $ launchctl kickstart -p gui/$UID/com.github.erikw.restic-automatic-backup + ``` -Debug with -```console -$ sudo launchctl debug gui/$UID/com.github.erikw.restic-automatic-backup --stdout --stderr -``` - -TODO how disable? +Use the `disable` command to temporarily pause the agent, or `bootout` to uninstall it. ``` $ launchctl disable gui/$UID/com.github.erikw.restic-automatic-backup $ launchctl bootout gui/$UID/com.github.erikw.restic-automatic-backup From d20954b9f4c791095849a45f64bf8379b13217a1 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 15:58:26 +0100 Subject: [PATCH 18/21] Modified: README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d64e9c..1af509c 100644 --- a/README.md +++ b/README.md @@ -265,18 +265,18 @@ straightforward (it needs to run with sudo to read environment). Just run: LaunchAgent is the modern service scheduler in in macOS that uses [Launchd](https://www.launchd.info/). [Launchd](https://www.launchd.info/) is the modern built-in service scheduler in macOS. It has support for running services as root (Daemon) or as a normal user (Agent). Here we we set up an LauchAgent to be run as your normal user for starting regular backups. -1. In general, follow the same setup as in (#setup-linux-systemd) except taht +1. In general, follow the same setup as in (#setup-linux-systemd) except for: * use `make install-launchagent` instead of `make install-systemd` - * Install everything to `/usr/local` and run restic as your own use, not root + * install everything to `/usr/local` and run restic as your own use, not root * Thus, install with ```console $ PREFIX=/usr/local make install-launchagent ``` -1. After installation with `make` , edit the installed launchagent if you want to change the default schedule or profile used +1. After installation with `make` , edit the installed LaunchAgent if you want to change the default schedule or profile used: ```console $ vim ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist ``` -1. Now install, enable and kickstart the first run! +1. Now install, enable and start the first run! ```console $ launchctl bootstrap gui/$UID ~/Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist $ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup From 99b0bf5ac5e086391d20cb3f82e5aa3fa4112ce4 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 16:29:14 +0100 Subject: [PATCH 19/21] Modified: README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1af509c..a43f8c4 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,8 @@ $ launchctl disable gui/$UID/com.github.erikw.restic-automatic-backup $ launchctl bootout gui/$UID/com.github.erikw.restic-automatic-backup ``` +If you updated the `.plist` file, you need to issue the `bootout` followed by `bootrstrap` and `enable` sub-commands of `launchctl`. This will guarantee that the file is properly reloaded. + ## Setup Cron If you want to run an all-classic cron job instead, do like this: From caf95ef393d882d1e416ccfd3a64239980a3f80d Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 17:16:18 +0100 Subject: [PATCH 20/21] Add make target to activate launchagent --- Makefile | 18 +++++++++++++++++- README.md | 4 ++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a68ebd0..a4c1b28 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,8 @@ .PHONY: help clean uninstall \ install-systemd install-cron \ install-targets-script install-targets-conf install-targets-systemd \ - install-targets-cron + install-targets-cron \ + activate-launchagent deactivate-launchagent #### Macros ################################################################### NOW := $(shell date +%Y-%m-%d_%H:%M:%S) @@ -39,6 +40,11 @@ MKDIR_PARENTS=sh -c '\ test -d $$dir || mkdir -p $$dir \ ' MKDIR_PARENTS +# LaunchAgent names. +UID := $(shell id -u) +LAUNCHAGENT = com.github.erikw.restic-automatic-backup +LAUNCHAGENT_TARGET = gui/$(UID)/$(LAUNCHAGENT) + # Source directories. DIR_SCRIPT = bin DIR_CONF = etc/restic @@ -163,3 +169,13 @@ $(DEST_DIR_LAUNCHAGENT)/%: $(BUILD_DIR_LAUNCHAGENT)/% # Install destination mac log dir. $(DEST_DIR_MAC_LOG): mkdir -p $@ + +# target: activate-launchagent - Activate the LaunchAgent. +activate-launchagent: + launchctl bootstrap gui/$(UID) $(DEST_TARGS_LAUNCHAGENT) + launchctl enable $(LAUNCHAGENT_TARGET) + launchctl kickstart -p $(LAUNCHAGENT_TARGET) + +# target: deactivate-launchagent - Deactivate and remove the LaunchAgent. +deactivate-launchagent: + launchctl bootout $(LAUNCHAGENT_TARGET) diff --git a/README.md b/README.md index a43f8c4..4583c5d 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,11 @@ Tip: use the Section icon in the top left of this document to navigate the secti * Arch: part of the `base-devel` meta package, Debian/Ubuntu: part of the `build-essential` meta package, macOS: use the preinstalled or a more recent with Homebrew) - # Setup Depending on your system, the setup will look different. Choose one of * [Linux + Systemd](#setup-linux-systemd) * [macOS + LaunchAgent](#setup-macos-launchagent) -* [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. +* [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS. ## Setup Linux Systemd ### TL;DR Setup @@ -282,6 +281,7 @@ LaunchAgent is the modern service scheduler in in macOS that uses [Launchd](http $ launchctl enable gui/$UID/com.github.erikw.restic-automatic-backup $ launchctl kickstart -p gui/$UID/com.github.erikw.restic-automatic-backup ``` + As a convenience, a shortcut for the above commands are `$ make activate-launchagent`. Use the `disable` command to temporarily pause the agent, or `bootout` to uninstall it. ``` From 4cae1ca2f0646103fc42d1f7cf1eb3b3627d9243 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Wed, 9 Feb 2022 17:23:09 +0100 Subject: [PATCH 21/21] Update CHANGELOG.md Fixes #51 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 145fc2e..40f592c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- macos LaunchAgent support. Install with `make install-launchagent`. +- macos LaunchAgent support. Install with `make install-launchagent` and activate with `make activate-launchagent`. See [README.md](README.md) for details. ## [5.0.0] - 2022-02-08 ### Added