unit-status-mail for backups

This commit is contained in:
Motiejus Jakštys 2023-02-25 15:47:49 +02:00
parent 4d21eda97f
commit c7bd826234

View File

@ -386,19 +386,53 @@ in {
options = "--delete-older-than 14d"; options = "--delete-older-than 14d";
}; };
systemd.services."make-snapshot-dirs" = let systemd.services = {
vals = builtins.attrValues backup_paths; "make-snapshot-dirs" = let
mountpoints = builtins.catAttrs "mountpoint" vals; vals = builtins.attrValues backup_paths;
unique_mountpoints = lib.unique mountpoints; mountpoints = builtins.catAttrs "mountpoint" vals;
in { unique_mountpoints = lib.unique mountpoints;
description = "prepare snapshot directories for backups"; in {
wantedBy = ["multi-user.target"]; description = "prepare snapshot directories for backups";
serviceConfig = { wantedBy = ["multi-user.target"];
Type = "oneshot"; serviceConfig = {
ExecStart = builtins.map (d: "${pkgs.coreutils}/bin/mkdir -p ${d}/.snapshot-latest") unique_mountpoints; Type = "oneshot";
RemainAfterExit = true; ExecStart = builtins.map (d: "${pkgs.coreutils}/bin/mkdir -p ${d}/.snapshot-latest") unique_mountpoints;
RemainAfterExit = true;
};
}; };
};
# https://northernlightlabs.se/2014-07-05/systemd-status-mail-on-unit-failure.html
"unit-status-mail@" = {
description = "Send an email on unit failure";
serviceConfig = {
Type = "simple";
ExecStart = pkgs.writeShellScript "unit-status-mail" ''
MAILTO="motiejus+alerts@jakstys.lt"
UNIT=$1
EXTRA=""
for e in "''${@:2}"; do
EXTRA+="$e"$'\n'
done
UNITSTATUS=$(${pkgs.systemd}/bin/systemctl status $UNIT)
${pkgs.postfix}/bin/sendmail $MAILTO <<EOF
Subject:Status mail for unit: $UNIT
Status report for unit: $UNIT
$EXTRA
$UNITSTATUS
EOF
echo -e "Status mail sent to: $MAILTO for unit: $UNIT"
'';
};
};
} // lib.mapAttrs' (name: value: {
name = "borgbackup-job-${name}";
value = {
serviceConfig.OnFailure = "unit-status-mail@${name}.service";
};
}) backup_paths;
# Do not change # Do not change
system.stateVersion = "22.11"; system.stateVersion = "22.11";