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