Made scripts optional

This commit is contained in:
Erik Westrup
2022-02-13 19:15:55 +01:00
parent c50e66b48f
commit 9449d78596
8 changed files with 48 additions and 20 deletions

View File

@@ -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)

View File

@@ -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:

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 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

View File

@@ -8,6 +8,6 @@ PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/:$INSTALL_PREFIX/bin/
@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:
# 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

View File

@@ -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

View File

@@ -1,6 +1,5 @@
[Unit]
Description=Backup with restic on schedule
Requires=nm-unmetered-connection.service
[Timer]
OnCalendar=daily

View File

@@ -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

View File

@@ -1,6 +1,5 @@
[Unit]
Description=Check restic backup Backblaze B2 for errors on a schedule
Requires=nm-unmetered-connection.service
[Timer]
OnCalendar=monthly