diff --git a/etc/systemd/system/nm-unmetered-connection.service b/etc/systemd/system/nm-unmetered-connection.service new file mode 100644 index 0000000..e2ce521 --- /dev/null +++ b/etc/systemd/system/nm-unmetered-connection.service @@ -0,0 +1,7 @@ +[Unit] +Description=Check if the current NetworkManager connection is metered + +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/nm-unmetered-connection.sh + diff --git a/etc/systemd/system/restic-backup.service b/etc/systemd/system/restic-backup.service index 060be03..d0d3108 100644 --- a/etc/systemd/system/restic-backup.service +++ b/etc/systemd/system/restic-backup.service @@ -1,10 +1,11 @@ [Unit] Description=Backup with restic to Backblaze B2 OnFailure=status-email-user@%n.service +Requires=nm-unmetered-connection.service [Service] Type=simple Nice=10 ExecStart=/usr/local/sbin/restic_backup.sh # $HOME or $XDG_CACHE_HOME must be set for restic to find /root/.cache/restic/ -Environment="HOME=/root" \ No newline at end of file +Environment="HOME=/root" diff --git a/etc/systemd/system/restic-backup.timer b/etc/systemd/system/restic-backup.timer index c492c33..2e681ee 100644 --- a/etc/systemd/system/restic-backup.timer +++ b/etc/systemd/system/restic-backup.timer @@ -1,5 +1,6 @@ [Unit] Description=Backup with restic on schedule +Requires=nm-unmetered-connection.service [Timer] OnCalendar=daily diff --git a/etc/systemd/system/restic-check.service b/etc/systemd/system/restic-check.service index 7f90e78..b0d68ea 100644 --- a/etc/systemd/system/restic-check.service +++ b/etc/systemd/system/restic-check.service @@ -2,6 +2,7 @@ Description=Check restic backup Backblaze B2 for errors OnFailure=status-email-user@%n.service Conflicts=restic.service +Requires=nm-unmetered-connection.service [Service] Type=simple diff --git a/etc/systemd/system/restic-check.timer b/etc/systemd/system/restic-check.timer index b637948..e43f2ac 100644 --- a/etc/systemd/system/restic-check.timer +++ b/etc/systemd/system/restic-check.timer @@ -1,9 +1,10 @@ [Unit] Description=Check restic backup Backblaze B2 for errors on a schedule +Requires=nm-unmetered-connection.service [Timer] OnCalendar=monthly Persistent=true [Install] -WantedBy=timers.target \ No newline at end of file +WantedBy=timers.target diff --git a/usr/local/sbin/nm-unmetered-connection.sh b/usr/local/sbin/nm-unmetered-connection.sh new file mode 100644 index 0000000..dd6cbc2 --- /dev/null +++ b/usr/local/sbin/nm-unmetered-connection.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +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 ".$") + +if [[ $metered_status =~ (1|3) ]]; then + echo Current connection is metered + exit 1 +else + exit 0 +fi +