41 Commits

Author SHA1 Message Date
Erik Westrup
c07f0ca027 Modified: CHANGELOG.md 2022-02-11 22:13:53 +01:00
Erik Westrup
a084830441 Modified: CHANGELOG.md 2022-02-11 22:13:41 +01:00
Erik Westrup
7d67142b6e Use correct profile in launchagent 2022-02-11 22:13:19 +01:00
Erik Westrup
b838ebcb1a Modified: CHANGELOG.md 2022-02-11 22:08:10 +01:00
Erik Westrup
8f118b6ddf Allow make to override etc dir 2022-02-11 22:07:26 +01:00
Erik Westrup
fc01b56e1e Modified: CHANGELOG.md 2022-02-11 21:47:34 +01:00
Erik Westrup
289016f8ea No need to write perm on scripts 2022-02-11 21:46:56 +01:00
Erik Westrup
0c43f93901 Modified: CHANGELOG.md 2022-02-11 20:45:49 +01:00
Erik Westrup
2a287910cb Allow override INSTALL_PREFIX on make cli 2022-02-11 20:34:33 +01:00
Erik Westrup
5f3ebd5b9d resticw: fix install prefix 2022-02-11 10:27:35 +01:00
Erik Westrup
8111a5f663 Bash is dependency 2022-02-10 10:58:41 +01:00
Erik Westrup
ae900a3834 Merge pull request #75 from gerardbosch/patch-1 2022-02-09 18:47:21 +01:00
Gerard Bosch
1db2395353 Minor typos 2022-02-09 18:41:20 +01:00
Erik Westrup
59899f5683 Merge pull request #74 from erikw/launchagent
Add macOS launchagent support
2022-02-09 17:25:31 +01:00
Erik Westrup
4cae1ca2f0 Update CHANGELOG.md
Fixes #51
2022-02-09 17:23:09 +01:00
Erik Westrup
caf95ef393 Add make target to activate launchagent 2022-02-09 17:16:18 +01:00
Erik Westrup
99b0bf5ac5 Modified: README.md 2022-02-09 16:29:14 +01:00
Erik Westrup
d20954b9f4 Modified: README.md 2022-02-09 15:58:26 +01:00
Erik Westrup
991476715a install instructions 2022-02-09 15:56:37 +01:00
Erik Westrup
c4429681d8 Modified: Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist 2022-02-09 15:49:25 +01:00
Erik Westrup
f073f10ddf Modified: Library/LaunchAgents/com.github.erikw.restic-automatic-backup.plist 2022-02-09 15:47:04 +01:00
Erik Westrup
a28160371c Fix log path 2022-02-09 15:45:49 +01:00
Erik Westrup
1977b3c67f Add OS icons 2022-02-09 15:31:42 +01:00
Erik Westrup
9412a8750c trim 2022-02-09 15:08:57 +01:00
Erik Westrup
d38616d697 Path working 2022-02-09 14:58:31 +01:00
Erik Westrup
c8ab0b4421 iterate 2022-02-09 14:04:07 +01:00
Erik Westrup
c1c25d718c schedule 2022-02-09 13:32:25 +01:00
Erik Westrup
f7f69a0142 instructions 2022-02-09 13:32:19 +01:00
Erik Westrup
2f4f48a59b Modified: CHANGELOG.md 2022-02-09 13:09:10 +01:00
Erik Westrup
e9d77d0a31 Surpress mkdir 2022-02-09 13:08:12 +01:00
Erik Westrup
bd3123bef4 Modified: Makefile 2022-02-09 13:08:12 +01:00
Erik Westrup
ddd2233c58 Install LaunchAgent in user Library 2022-02-09 13:08:12 +01:00
Erik Westrup
7973eedae4 Modified: Makefile 2022-02-09 13:08:12 +01:00
Erik Westrup
db26c4e517 Launch instructions 2022-02-09 13:08:12 +01:00
Erik Westrup
193bf926cf PoC souce files 2022-02-09 13:08:12 +01:00
Erik Westrup
8ea1d70a15 Merge pull request #72 from gerardbosch/patch-1
Update comment
2022-02-09 13:07:38 +01:00
Gerard Bosch
8eb7d56fdd Update comment 2022-02-09 12:59:54 +01:00
Erik Westrup
03d9399686 Merge pull request #71 from gerardbosch/fix/resticw-arguments
Fix resticw arguments line
2022-02-08 18:57:42 +01:00
Gerard Bosch
59da5da0da Fix resticw arguments line
It needs to be treated as an array so that complex commands like
`stats latest` work.
2022-02-08 18:51:20 +01:00
Erik Westrup
7a1409ca03 Allow users to execute scripts 2022-02-08 18:20:49 +01:00
Erik Westrup
b5dcce5d2a Adjust comment 2022-02-08 17:58:46 +01:00
6 changed files with 168 additions and 33 deletions

View File

@@ -6,6 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [5.2.1] - 2022-02-11
### Fixed
- Use default profile in LaunchAgent.
## [5.2.0] - 2022-02-11
### Added
- Make option to override destination dir for configuration files. Needed for Homebrew.
### Changed
- Write permissions on installed scripts removed (0755 -> 0555). Homebrew was complaining.
## [5.1.0] - 2022-02-11
### Added
- macos LaunchAgent support. Install with `make install-launchagent` and activate with `make activate-launchagent`. See [README.md](README.md) for details.
- make option INSTALL_PREFIX to make PKGBUILD and such easier to write.
## [5.0.0] - 2022-02-08
### Added
- `resticw` wrapper for working with different profiles without the need to source the profiles first.

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- See launchd.plist(5)for documentation on this file. -->
<!-- See https://www.launchd.info/ for a tutorial. -->
<!-- Debug with: $ tail -f /var/log/com.apple.xpc.launchd/launchd.log | grep erikw.restic -->
<!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.github.erikw.restic-automatic-backup</string>
<key>ProgramArguments</key>
<!-- exec in subshell to 1) source *.env.sh 2) expand $HOME to logpath (ref. https://apple.stackexchange.com/a/365880/197493) -->
<array>
<string>/bin/bash</string>
<string>-c</string>
<string>source /usr/local/etc/restic/default.env.sh &amp;&amp; /usr/local/bin/restic_backup.sh >>$HOME/$LOG_OUT 2>>$HOME/$LOG_ERR</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
<key>LOG_OUT</key>
<string>/Library/Logs/restic/restic_stdout.log</string>
<key>LOG_ERR</key>
<string>/Library/Logs/restic/restic_stderr.log</string>
</dict>
<key>RunAtLoad</key>
<true/>
<!-- Will schedule backup every day at 19:00 -->
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>19</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
</dict>
</plist>

101
Makefile
View File

@@ -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.
@@ -17,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)
@@ -38,46 +40,67 @@ 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)
# What to substitute $INSTALL_PREFIX in sources to.
# This can be useful to set to empty on commandline when building e.g. an AUR
# package in a separate build directory (PREFIX).
INSTALL_PREFIX := $(PREFIX)
# Where to install persistent configuration files. Used by Homebrew.
SYSCONFDIR := $(PREFIX)
# 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 := 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_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_CONF = $(SYSCONFDIR)/$(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))
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 $(SYSCONFDIR)/, $(SRCS_CONF))
DEST_TARGS_SYSTEMD = $(addprefix $(PREFIX)/, $(SRCS_SYSTEMD))
DEST_TARGS_CRON = $(addprefix $(PREFIX)/, $(SRCS_CRON))
DEST_TARGS_LAUNCHAGENT = $(addprefix $(HOME)/, $(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 ##################################################################
@@ -89,7 +112,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; \
@@ -101,28 +124,35 @@ 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) $(DEST_DIR_MAC_LOG)
# Copies sources to build directory & replace "$INSTALL_PREFIX".
$(BUILD_DIR)/% : %
@${MKDIR_PARENTS} $@
cp $< $@
sed -i.bak -e 's|$$INSTALL_PREFIX|$(PREFIX)|g' $@; rm $@.bak
sed -i.bak -e 's|$$INSTALL_PREFIX|$(INSTALL_PREFIX)|g' $@; rm $@.bak
# Install destination script files.
$(DEST_DIR_SCRIPT)/%: $(BUILD_DIR_SCRIPT)/%
@${MKDIR_PARENTS} $@
install -m 0744 $< $@
install -m 0555 $< $@
# Install destination conf files. Additionally backup existing files.
$(DEST_DIR_CONF)/%: $(BUILD_DIR_CONF)/%
@@ -138,3 +168,22 @@ $(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 $< $@
# 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)

View File

@@ -40,15 +40,16 @@ Tip: use the Section icon in the top left of this document to navigate the secti
# Requirements
* `restic >=v0.9.6`
* `bash >=v4.0.0`
* (recommended) GNU `make` if you want an automated install
* 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)
* [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS.
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/linux.svg" /> [Linux + Systemd](#setup-linux-systemd)
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/apple.svg" /> [macOS + LaunchAgent](#setup-macos-launchagent)
* <img height="16" width="16" src="https://unpkg.com/simple-icons@v6/icons/clockify.svg" /> [Cron](#setup-cron) - for any system having a cron daemon. Tested on FreeBSD and macOS.
## Setup Linux Systemd
### TL;DR Setup
@@ -260,6 +261,36 @@ 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
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 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 for:
* use `make install-launchagent` instead of `make install-systemd`
* install everything to `/usr/local` and run restic as your own user, 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 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
$ 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.
```
$ 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:

View File

@@ -125,7 +125,7 @@ eval "$(docopt "$@")" # See https://github.com/andsens/docopt.sh for the magic :
# Exit on error, unbound variable, pipe error
set -euo pipefail
ENV_DIR=/etc/restic
ENV_DIR=$INSTALL_PREFIX/etc/restic
ERR_NO_SUCH_PROFILE=2
ERR_PROFILE_NO_READ_PERM=3
@@ -136,11 +136,11 @@ profile_file="${ENV_DIR}/${__profile}.env.sh"
[[ ! -f "$profile_file" ]] && echo "Invalid profile: No such environment file ${profile_file}" && exit "$ERR_NO_SUCH_PROFILE"
if [[ ! -r "$profile_file" ]]; then
echo "Error: could not read the environment file ${profile_file}. Are you running this script as the correct user? Maybe try sudo with the right user."
echo "Error: Could not read the environment file ${profile_file}. Are you running this script as the correct user? Maybe try sudo with the right user."
exit "$ERR_PROFILE_NO_READ_PERM"
fi
echo -e "‣ Using profile: ${__profile} -- (${profile_file})\n"
# shellcheck disable=SC2154,SC1090
source "$profile_file" && restic "$_restic_arguments_line_"
source "$profile_file" && restic "${_restic_arguments_line_[@]}"

View File

@@ -14,7 +14,7 @@
# shellcheck source=etc/restic/_global.env.sh
. "$INSTALL_PREFIX/etc/restic/_global.env.sh"
# Below envvar will override those in _global.env.sh
# Envvars below will override those in _global.env.sh if present.
export RESTIC_REPOSITORY="b2:<b2-repo-name>" # *EDIT* fill with your repo name