mirror of
https://codeberg.org/privacy1st/nix-git
synced 2024-11-24 22:29:33 +01:00
yodaHedgehog: WIP backup service
This commit is contained in:
parent
6687d387df
commit
e18ae48dd6
12
examples/block-opensmtpd-queue.sh
Normal file
12
examples/block-opensmtpd-queue.sh
Normal 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
|
@ -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
|
||||
'';
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user