Browse Source

notify: fix notify.sh to work with server side; adjust text

master
Jim Paris 1 month ago
parent
commit
aff447c1b6
2 changed files with 40 additions and 13 deletions
  1. +28
    -8
      backup.py
  2. +12
    -5
      notify.sh

+ 28
- 8
backup.py View File

@@ -320,31 +320,51 @@ def main(argv: list[str]):
warnings = sum(1 for (letter, msg) in backup.logs if letter == 'W')
errors = sum(1 for (letter, msg) in backup.logs if letter == 'E')

def plural(num: int, word: str) -> str:
suffix = "" if num == 1 else "s"
return f"{num} {word}{suffix}"

warnmsg = plural(warnings, "warning") if warnings else None
errmsg = plural(errors, "error") if errors else None

if not warnings and not errors:
backup.log('I', f"backup successful", bold=True)

else:
if warnings:
backup.log('W', f"reported {warnings} warnings", bold=True)
if warnmsg:
backup.log('W', f"reported {warnmsg}", bold=True)
if errors:
backup.log('E', f"reported {errors} errors", bold=True)
backup.log('E', f"reported {errmsg}", bold=True)

# Send a notification of errors
email = backup.config.notify_email
if email and not args.dry_run:
backup.log('I', f"sending error notification to {email}")

# Show all of our warnings and errors
body = [ "Backup reported the following warnings and errors:" ]
# Show all of our warnings and errors. Use a ">" prefix
# so warnings and errors get highlighted by the mail reader.
body = [ "Logs from backup.py:" ]
for (letter, msg) in backup.logs:
body.append(f"{letter}: {msg}")
if letter == "E" or letter == "W":
prefix = ">"
else:
prefix = " "
body.append(f"{prefix}{letter}: {msg}")
body_text = "\n".join(body).encode()

# Followed by borg output
body_text += b"\nBorg output:\n" + b"".join(captured_output)
body_text += b"\n\nBorg output:\n" + b"".join(captured_output)

# Subject summary
if errmsg and warnmsg:
summary = f"{errmsg}, {warnmsg}"
elif errors:
summary = errmsg
else:
summary = warnmsg

# Call notify.sh
res = subprocess.run([args.notify, email], input=body_text)
res = subprocess.run([args.notify, summary, email], input=body_text)
if res.returncode != 0:
backup.log('E', f"failed to send notification")
errors += 1


+ 12
- 5
notify.sh View File

@@ -12,8 +12,15 @@ if tty -s ; then
exit 1
fi

ssh \
-F "$SSH/config" \
-i "$SSH/id_ecdsa_notify" \
"$BACKUP_USER@$BACKUP_HOST" \
borg/notify.sh "$HOSTNAME" "$1"
SUMMARY="$1"
EMAIL="$2"

# Remote notify.sh wants subject as first line, not as an argument,
# since it's a bit messy to pass complex strings through ssh command
# lines.
( echo "backup $HOSTNAME: $SUMMARY" ; cat ) | \
ssh \
-F "$SSH/config" \
-i "$SSH/id_ecdsa_notify" \
"$BACKUP_USER@$BACKUP_HOST" \
borg/notify.sh "$EMAIL"

Loading…
Cancel
Save