From e18ae48dd6121f7415ebcdfa710ff1597ac3d438 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 20 Nov 2023 19:12:54 +0100 Subject: [PATCH] yodaHedgehog: WIP backup service --- examples/block-opensmtpd-queue.sh | 12 ++++++++ hosts/yodaHedgehog/host-specific.nix | 41 +++++++++++++++++++++------- 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 examples/block-opensmtpd-queue.sh diff --git a/examples/block-opensmtpd-queue.sh b/examples/block-opensmtpd-queue.sh new file mode 100644 index 0000000..59ffbf8 --- /dev/null +++ b/examples/block-opensmtpd-queue.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +echo -e "Subject: Test\n\nHi there, this is some test." | sendmail -f langbein@mail.de daniel@systemli.org +while :; do + # Note: smtpctl requires root privileges. + queue="$(smtpctl show queue)" + if [ "${queue}" = "" ]; then + break + fi + printf '%s\n' 'Delaying suspend due to non-empty smtpd email queue.' + sleep 5s +done diff --git a/hosts/yodaHedgehog/host-specific.nix b/hosts/yodaHedgehog/host-specific.nix index 7fee1da..e0f65e8 100644 --- a/hosts/yodaHedgehog/host-specific.nix +++ b/hosts/yodaHedgehog/host-specific.nix @@ -9,7 +9,11 @@ # 2.6W suspended, with 1 RAM, 1 SSD, 2 HDDs # 18.9W idle, with 1 RAM, 1 SSD, 2 HDDs -# journalctl -u daily-backup-and-suspend +# View service log: +# journalctl -u daily-backup-and-suspend + +# Print unit file: +# cat "$(systemctl show -P FragmentPath daily-backup-and-suspend.service)" { config, pkgs, ... }: let @@ -42,10 +46,12 @@ in # Packages required for this script. # For `ssh` and `journalwatch`, there are assertions above. path = with pkgs; [ - # Provides `echo`, `sleep`, `printf`. - coreutils # Provides `ssh` openssh + # Provides `awk`, `grep`, `sleep`, `printf`, `echo`, 'sendmail' + busybox + # Provides `smtpctl` + opensmtpd ]; # Script to execute as main process. script = '' @@ -56,8 +62,8 @@ in # Don't suspend as long as `${backup-source}:${stay-awake-file}` exists. while :; do - result="$(ssh ${backup-source} 'ls ${stay-awake-file} 2>&1')" - case "$${result}" in + result="$(ssh ${backup-source} 'ls ${stay-awake-file} 2>&1')" ||: + case "''${result}" in *"No such file or directory") break ;; @@ -72,21 +78,36 @@ in done # Wait until no BTRFS scrub service is running. - while systemctl list-units --type=service --plain --quiet | awk '{ print $1 }' | grep '^btrfs-scrub'; do + while :; do + running_services="$(systemctl list-units --type=service --plain --quiet | awk '{ print $1 }')" + if ! printf '%s' "''${running_services}" | grep '^btrfs-scrub'; then + break; + fi printf '%s\n' 'Delaying suspend due to running BTRFS scrub service.' sleep 60s done # Send filtered journal by email. systemctl start journalwatch.service ||: - # Short delay to let sendmail send the email. + # Send notification by email. + printf '%s\n\n%s' 'Subject: ${config.networking.hostName}' 'Finished backup.' | sendmail -f langbein@mail.de daniel@systemli.org + + # Let sendmail send emails. + #while :; do + # # TODO: Plain usage of `smtpctl` gives the error: + # # smtpctl: this program must be setgid smtpq + # queue="$(smtpctl show queue)" + # if [ "''${queue}" = "" ]; then + # break + # fi + # printf '%s\n' 'Delaying suspend due to non-empty smtpd email queue.' + # sleep 1s + #done sleep 15s printf '%s\n' 'Finished backup script.' - # Suspend to save power. - # TODO - #systemctl suspend + systemctl suspend ''; }; }