diff --git a/Makefile b/Makefile index 71527eb..af28e80 100644 --- a/Makefile +++ b/Makefile @@ -79,16 +79,23 @@ DIR_LAUNCHAGENT = Library/LaunchAgents DIR_SCHEDTASK = ScheduledTask # Source files. -SRCS_SCRIPT = $(filter-out %cron_mail, $(wildcard $(DIR_SCRIPT)/*)) +SRCS_SCRIPT = $(filter-out \ + %cron_mail \ + %systemd-email \ + %nm-unmetered-connection.sh \ + , $(wildcard $(DIR_SCRIPT)/*)) SRCS_CONF = $(wildcard $(DIR_CONF)/*) -SRCS_SYSTEMD = $(wildcard $(DIR_SYSTEMD)/*) +SRCS_SYSTEMD = $(filter-out \ + %status-email-user@.service \ + %nm-unmetered-connection.service \ + , $(wildcard $(DIR_SYSTEMD)/*)) SRCS_CRON = $(wildcard $(DIR_CRON)/*) SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) SRCS_SCHEDTASK = $(wildcard $(DIR_SCHEDTASK)/*) # 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) diff --git a/README.md b/README.md index 93638cf..68312be 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,10 @@ Put this file in `/bin`: Put this files in `/etc/systemd/system/`: * `status-email-user@.service`: A service that can notify you via email when a systemd service fails. Edit the target email address in this file. -As you maybe noticed already before, `restic-backup.service` is configured to start `status-email-user.service` on failure. +Now edit `restic-backup.service` and `status-email-user.service` to call this service failure. +``` +OnFailure=status-email-user@%n.service +``` #### 9. Optional: automated backup checks @@ -249,7 +252,23 @@ There is companion scripts, service and timer (`*check*`) to restic-backup.sh th $ sudo systemctl enable --now restic-check@default.timer ```` -#### 10. Optional: 🏃 Restic wrapper +#### 10. Optional: No backup on metered connections +For a laptop, it can make sense to not do heavy backups when your on a metered connection like a shared connection from you mobile phone. To solve this we can set up a systemd service that is in success state only when a connection is unmetered. Then we can tell our backup service to depend on this service simply! When the unmetered service detects an unmetered connection it will go to failed state. Then our backup service will not run as it requires this other service to be in success state. + +Put this file in `/bin`: +* `nm-unmetered-connection.sh`: Detects metered connections and returns will error code if one is detected. This scripts requires the Gnome [NetworkManager](https://wiki.gnome.org/Projects/NetworkManager) to be installed. Modify this script if your system has a different network manager. + +Put this files in `/etc/systemd/system/`: +* `nm-unmetered-connection.service`: A service that is in success state if the connection is unmetered only. + +Now edit `restic-backup.service` and `status-email-user.service` to require the new service to be in success state: +``` +Requires=nm-unmetered-connection.service +``` + + + +#### 11. Optional: 🏃 Restic wrapper For convenience there's a `restic` wrapper script that makes loading profiles and **running restic** straightforward (it needs to run with sudo to read environment). Just run: diff --git a/bin/nm-unmetered-connection.sh b/bin/nm-unmetered-connection.sh index dd6cbc2..624894c 100644 --- a/bin/nm-unmetered-connection.sh +++ b/bin/nm-unmetered-connection.sh @@ -1,19 +1,19 @@ -#!/bin/bash +#!/usr/bin/env bash +# Requires Gnome NetworkManager systemctl is-active dbus.service >/dev/null 2>&1 || exit 0 systemctl is-active NetworkManager.service >/dev/null 2>&1 || exit 0 metered_status=$(dbus-send --system --print-reply=literal \ - --system --dest=org.freedesktop.NetworkManager \ - /org/freedesktop/NetworkManager \ - org.freedesktop.DBus.Properties.Get \ - string:org.freedesktop.NetworkManager string:Metered \ - | grep -o ".$") + --system --dest=org.freedesktop.NetworkManager \ + /org/freedesktop/NetworkManager \ + org.freedesktop.DBus.Properties.Get \ + string:org.freedesktop.NetworkManager string:Metered \ + | grep -o ".$") if [[ $metered_status =~ (1|3) ]]; then echo Current connection is metered exit 1 -else +else exit 0 fi - diff --git a/etc/cron.d/restic b/etc/cron.d/restic index ed6f5ce..e7cb30e 100644 --- a/etc/cron.d/restic +++ b/etc/cron.d/restic @@ -4,5 +4,10 @@ PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/:$INSTALL_PREFIX/bin/ # minute hour mday month wday command # Reference: https://www.freebsd.org/doc/handbook/configtuning-cron.html # Reference: crontab(5). -@midnight root . $INSTALL_PREFIX/etc/restic/default.sh && cron_mail restic_backup.sh -@monthly root . $INSTALL_PREFIX/etc/restic/default.sh && cron_mail restic_check.sh + +@midnight root . $INSTALL_PREFIX/etc/restic/default.sh && restic_backup.sh +@monthly root . $INSTALL_PREFIX/etc/restic/default.sh && restic_check.sh + +# Email notification version. Make sure bin/cron_mail is in the above $PATH +#@midnight root . $INSTALL_PREFIX/etc/restic/default.sh && cron_mail restic_backup.sh +#@monthly root . $INSTALL_PREFIX/etc/restic/default.sh && cron_mail restic_check.sh diff --git a/usr/lib/systemd/system/restic-backup@.service b/usr/lib/systemd/system/restic-backup@.service index 34995cc..c185521 100644 --- a/usr/lib/systemd/system/restic-backup@.service +++ b/usr/lib/systemd/system/restic-backup@.service @@ -1,7 +1,9 @@ [Unit] Description=Backup with restic to Backblaze B2 -OnFailure=status-email-user@%n.service -Requires=nm-unmetered-connection.service +# Email on failure require special setup. See README.md +#OnFailure=status-email-user@%n.service +# Prevent backup on unmetered connection. Needs special setup. See README.md. +#Requires=nm-unmetered-connection.service [Service] Type=simple diff --git a/usr/lib/systemd/system/restic-backup@.timer b/usr/lib/systemd/system/restic-backup@.timer index 2e681ee..c492c33 100644 --- a/usr/lib/systemd/system/restic-backup@.timer +++ b/usr/lib/systemd/system/restic-backup@.timer @@ -1,6 +1,5 @@ [Unit] Description=Backup with restic on schedule -Requires=nm-unmetered-connection.service [Timer] OnCalendar=daily diff --git a/usr/lib/systemd/system/restic-check@.service b/usr/lib/systemd/system/restic-check@.service index cd47ff0..edf7d28 100644 --- a/usr/lib/systemd/system/restic-check@.service +++ b/usr/lib/systemd/system/restic-check@.service @@ -1,8 +1,10 @@ [Unit] Description=Check restic backup Backblaze B2 for errors -OnFailure=status-email-user@%n.service +# Email on failure require special setup. See README.md +#OnFailure=status-email-user@%n.service Conflicts=restic-backup.service -Requires=nm-unmetered-connection.service +# Prevent backup on unmetered connection. Needs special setup. See README.md. +#Requires=nm-unmetered-connection.service [Service] Type=simple diff --git a/usr/lib/systemd/system/restic-check@.timer b/usr/lib/systemd/system/restic-check@.timer index e43f2ac..097fd1e 100644 --- a/usr/lib/systemd/system/restic-check@.timer +++ b/usr/lib/systemd/system/restic-check@.timer @@ -1,6 +1,5 @@ [Unit] Description=Check restic backup Backblaze B2 for errors on a schedule -Requires=nm-unmetered-connection.service [Timer] OnCalendar=monthly