Merge branch 'optionals'

* optionals:
  Made scripts optional
  Cron not using cron_email by default
This commit is contained in:
Erik Westrup
2022-02-13 19:18:37 +01:00
8 changed files with 54 additions and 21 deletions
+10 -3
View File
@@ -79,16 +79,23 @@ DIR_LAUNCHAGENT = Library/LaunchAgents
DIR_SCHEDTASK = ScheduledTask DIR_SCHEDTASK = ScheduledTask
# Source files. # 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_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_CRON = $(wildcard $(DIR_CRON)/*)
SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*) SRCS_LAUNCHAGENT= $(wildcard $(DIR_LAUNCHAGENT)/*)
SRCS_SCHEDTASK = $(wildcard $(DIR_SCHEDTASK)/*) SRCS_SCHEDTASK = $(wildcard $(DIR_SCHEDTASK)/*)
# Local build directory. Sources will be copied here, # Local build directory. Sources will be copied here,
# modified and then installed from this directory. # modified and then installed from this directory.
BUILD_DIR := build BUILD_DIR = build
BUILD_DIR_SCRIPT = $(BUILD_DIR)/$(DIR_SCRIPT) BUILD_DIR_SCRIPT = $(BUILD_DIR)/$(DIR_SCRIPT)
BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF) BUILD_DIR_CONF = $(BUILD_DIR)/$(DIR_CONF)
BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD) BUILD_DIR_SYSTEMD = $(BUILD_DIR)/$(DIR_SYSTEMD)
+21 -2
View File
@@ -237,7 +237,10 @@ Put this file in `/bin`:
Put this files in `/etc/systemd/system/`: 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. * `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 #### 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 $ 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** 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: straightforward (it needs to run with sudo to read environment). Just run:
+8 -8
View File
@@ -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 dbus.service >/dev/null 2>&1 || exit 0
systemctl is-active NetworkManager.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 \ metered_status=$(dbus-send --system --print-reply=literal \
--system --dest=org.freedesktop.NetworkManager \ --system --dest=org.freedesktop.NetworkManager \
/org/freedesktop/NetworkManager \ /org/freedesktop/NetworkManager \
org.freedesktop.DBus.Properties.Get \ org.freedesktop.DBus.Properties.Get \
string:org.freedesktop.NetworkManager string:Metered \ string:org.freedesktop.NetworkManager string:Metered \
| grep -o ".$") | grep -o ".$")
if [[ $metered_status =~ (1|3) ]]; then if [[ $metered_status =~ (1|3) ]]; then
echo Current connection is metered echo Current connection is metered
exit 1 exit 1
else else
exit 0 exit 0
fi fi
+7 -2
View File
@@ -4,5 +4,10 @@ PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/:$INSTALL_PREFIX/bin/
# minute hour mday month wday command # minute hour mday month wday command
# Reference: https://www.freebsd.org/doc/handbook/configtuning-cron.html # Reference: https://www.freebsd.org/doc/handbook/configtuning-cron.html
# Reference: crontab(5). # 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
@@ -1,7 +1,9 @@
[Unit] [Unit]
Description=Backup with restic to Backblaze B2 Description=Backup with restic to Backblaze B2
OnFailure=status-email-user@%n.service # Email on failure require special setup. See README.md
Requires=nm-unmetered-connection.service #OnFailure=status-email-user@%n.service
# Prevent backup on unmetered connection. Needs special setup. See README.md.
#Requires=nm-unmetered-connection.service
[Service] [Service]
Type=simple Type=simple
@@ -1,6 +1,5 @@
[Unit] [Unit]
Description=Backup with restic on schedule Description=Backup with restic on schedule
Requires=nm-unmetered-connection.service
[Timer] [Timer]
OnCalendar=daily OnCalendar=daily
+4 -2
View File
@@ -1,8 +1,10 @@
[Unit] [Unit]
Description=Check restic backup Backblaze B2 for errors 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 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] [Service]
Type=simple Type=simple
@@ -1,6 +1,5 @@
[Unit] [Unit]
Description=Check restic backup Backblaze B2 for errors on a schedule Description=Check restic backup Backblaze B2 for errors on a schedule
Requires=nm-unmetered-connection.service
[Timer] [Timer]
OnCalendar=monthly OnCalendar=monthly