send notification on success or failure
This commit is contained in:
parent
78290f44cc
commit
b71910e379
1 changed files with 34 additions and 26 deletions
60
backup.nix
60
backup.nix
|
@ -17,8 +17,8 @@ let
|
|||
tar -cz -C /var/lib/redis-misskey .
|
||||
'';
|
||||
|
||||
notifyErrorPkg = pkgs.writeShellApplication {
|
||||
name = "notify-error";
|
||||
notifyEmailPkg = pkgs.writeShellApplication {
|
||||
name = "notify-email";
|
||||
|
||||
runtimeInputs = [ pkgs.system-sendmail pkgs.coreutils ];
|
||||
|
||||
|
@ -29,10 +29,15 @@ let
|
|||
|
||||
to="admin@heartles.xyz"
|
||||
|
||||
email="$(cat <<EOEMAIL
|
||||
header="$(cat <<EOEMAIL
|
||||
To: $to
|
||||
From: $from
|
||||
Subject: $subject
|
||||
EOEMAIL
|
||||
)"
|
||||
|
||||
email="$(cat <<EOEMAIL
|
||||
$header
|
||||
|
||||
$text
|
||||
|
||||
|
@ -40,7 +45,7 @@ let
|
|||
)"
|
||||
|
||||
echo "Sending message:"
|
||||
cat <<< "$email"
|
||||
cat <<< "$header"
|
||||
|
||||
sendmail -f "$from" "$to" <<< "$email"
|
||||
'';
|
||||
|
@ -102,6 +107,31 @@ in {
|
|||
echo "Backup complete to '$s3Dir'"
|
||||
'';
|
||||
|
||||
serviceConfig.ExecStopPost = let
|
||||
script = pkgs.writeScript "backup-notify" ''
|
||||
invocationId="$(systemctl show --value -p InvocationID misskey-backup.service)"
|
||||
logs="$(journalctl _SYSTEMD_INVOCATION_ID="$invocationId" -u misskey-backup.service)"
|
||||
|
||||
if [ "$SERVICE_RESULT" = "success"]; then
|
||||
${notifyEmailPkg}/bin/notify-email "backup" "SUCCESS: Misskey Backup Notification" "$(cat <<EOMSG
|
||||
A backup process has succeeded. Logs to follow:
|
||||
|
||||
$logs
|
||||
|
||||
EOMSG
|
||||
)"
|
||||
else
|
||||
${notifyEmailPkg}/bin/notify-email "backup" "FAILURE: Misskey Backup Notification" "$(cat <<EOMSG
|
||||
A backup process has failed. Logs to follow:
|
||||
|
||||
$logs
|
||||
|
||||
EOMSG
|
||||
)"
|
||||
fi
|
||||
'';
|
||||
in "${script}";
|
||||
|
||||
after = [ "network-online.target" ];
|
||||
wants = [ "network-online.target" ];
|
||||
requires = [ "postgresql.service" ];
|
||||
|
@ -109,28 +139,6 @@ in {
|
|||
unitConfig.OnFailure = "misskey-backup-failure.service";
|
||||
};
|
||||
|
||||
systemd.services.misskey-backup-failure = {
|
||||
description = "Misskey backup failure notification";
|
||||
|
||||
serviceConfig.User = user;
|
||||
serviceConfig.Type = "oneshot";
|
||||
|
||||
script = ''
|
||||
invocationId="$(systemctl show --value -p InvocationID misskey-backup.service)"
|
||||
logs="$(journalctl _SYSTEMD_INVOCATION_ID="$invocationId" -u misskey-backup.service)"
|
||||
|
||||
${notifyErrorPkg}/bin/notify-error "backup" "Misskey Backup Failure Notification" "$(cat <<EOMSG
|
||||
A backup process has failed. Logs to follow:
|
||||
|
||||
$logs
|
||||
|
||||
EOMSG
|
||||
)"
|
||||
'';
|
||||
|
||||
requires = [ "postfix.service" ];
|
||||
};
|
||||
|
||||
systemd.timers.misskey-backup = { timerConfig.Persistent = true; };
|
||||
|
||||
security.sudo.extraRules = [{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue