yodaHedgehog: WIP backup service

This commit is contained in:
Daniel Langbein 2023-11-20 19:12:54 +01:00
parent 6687d387df
commit e18ae48dd6
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 43 additions and 10 deletions

View File

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

View File

@ -9,8 +9,12 @@
# 2.6W suspended, with 1 RAM, 1 SSD, 2 HDDs
# 18.9W idle, with 1 RAM, 1 SSD, 2 HDDs
# 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
backup-source = "rootNas";
@ -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
'';
};
}