Compare commits
2 Commits
ed1d79d400
...
a18b9ed6d0
Author | SHA1 | Date | |
---|---|---|---|
a18b9ed6d0 | |||
756dbe1898 |
46
backup.py
46
backup.py
|
@ -251,11 +251,12 @@ def main(argv: list[str]):
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
config = Config(args.config)
|
config = Config(args.config)
|
||||||
|
backup = Backup(config, args.dry_run)
|
||||||
|
|
||||||
# Parse variables from vars.sh
|
# Parse variables from vars.sh
|
||||||
hostname = os.uname().nodename
|
hostname = os.uname().nodename
|
||||||
borg = str(base / "borg.sh")
|
borg_sh = str(base / "borg.sh")
|
||||||
notify = str(base / "notify.sh")
|
notify_sh = str(base / "notify.sh")
|
||||||
try:
|
try:
|
||||||
with open(args.vars) as f:
|
with open(args.vars) as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
|
@ -267,14 +268,13 @@ def main(argv: list[str]):
|
||||||
if var == "HOSTNAME":
|
if var == "HOSTNAME":
|
||||||
hostname = value
|
hostname = value
|
||||||
if var == "BORG":
|
if var == "BORG":
|
||||||
borg = value
|
borg_sh = value
|
||||||
if var == "BORG_DIR":
|
if var == "BORG_DIR":
|
||||||
notify = pathlib.Path(value) / "notify.sh"
|
notify_sh = str(pathlib.Path(value) / "notify.sh")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log('W', f"failed to parse variables from {args.vars}: {str(e)}")
|
backup.log('W', f"failed to parse variables from {args.vars}: {str(e)}")
|
||||||
|
|
||||||
# Run backup
|
# Run backup
|
||||||
backup = Backup(config, args.dry_run)
|
|
||||||
captured_output: list[bytes] = []
|
captured_output: list[bytes] = []
|
||||||
|
|
||||||
if args.dry_run:
|
if args.dry_run:
|
||||||
|
@ -285,7 +285,7 @@ def main(argv: list[str]):
|
||||||
backup.run(out)
|
backup.run(out)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
else:
|
else:
|
||||||
borg = subprocess.Popen([borg,
|
borg = subprocess.Popen([borg_sh,
|
||||||
"create",
|
"create",
|
||||||
"--verbose",
|
"--verbose",
|
||||||
"--progress",
|
"--progress",
|
||||||
|
@ -304,6 +304,9 @@ def main(argv: list[str]):
|
||||||
if borg.stdin is None:
|
if borg.stdin is None:
|
||||||
raise Exception("no pipe")
|
raise Exception("no pipe")
|
||||||
|
|
||||||
|
borg_saw_warnings = 0
|
||||||
|
borg_saw_errors = 0
|
||||||
|
|
||||||
# Use a thread to capture output
|
# Use a thread to capture output
|
||||||
def reader_thread(fh):
|
def reader_thread(fh):
|
||||||
last_progress = 0
|
last_progress = 0
|
||||||
|
@ -313,7 +316,20 @@ def main(argv: list[str]):
|
||||||
if ((data['type'] == 'log_message' or
|
if ((data['type'] == 'log_message' or
|
||||||
data['type'] == 'progress_message')
|
data['type'] == 'progress_message')
|
||||||
and 'message' in data):
|
and 'message' in data):
|
||||||
line = (data['message'] + '\n').encode()
|
|
||||||
|
# Count warnings and errors, but ignore some.
|
||||||
|
changed_msg = "file changed while we backed it up"
|
||||||
|
if data['levelname'] == 'WARNING':
|
||||||
|
prefix = "warning: "
|
||||||
|
if changed_msg not in data['message']:
|
||||||
|
borg_saw_warnings += 1
|
||||||
|
elif data['levelname'] not in ('DEBUG', 'INFO'):
|
||||||
|
prefix = "error: "
|
||||||
|
borg_saw_errors += 1
|
||||||
|
else:
|
||||||
|
prefix = ""
|
||||||
|
|
||||||
|
line = (prefix + data['message'] + '\n').encode()
|
||||||
elif data['type'] == 'archive_progress':
|
elif data['type'] == 'archive_progress':
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if now - last_progress > 10:
|
if now - last_progress > 10:
|
||||||
|
@ -359,8 +375,12 @@ def main(argv: list[str]):
|
||||||
ret = borg.returncode
|
ret = borg.returncode
|
||||||
if ret < 0:
|
if ret < 0:
|
||||||
backup.log('E', f"borg exited with signal {-ret}")
|
backup.log('E', f"borg exited with signal {-ret}")
|
||||||
elif ret != 0:
|
elif ret == 2 or borg_saw_errors:
|
||||||
backup.log('E', f"borg exited with return code {ret}")
|
backup.log('E', f"borg exited with errors (ret={ret})")
|
||||||
|
elif ret == 1 or borg_saw_warnings:
|
||||||
|
backup.log('W', f"borg exited with warnings (ret={ret})")
|
||||||
|
else:
|
||||||
|
backup.log('E', f"borg exited with unknown error code {ret}")
|
||||||
|
|
||||||
# See if we had any errors
|
# See if we had any errors
|
||||||
warnings = sum(1 for (letter, msg) in backup.logs if letter == 'W')
|
warnings = sum(1 for (letter, msg) in backup.logs if letter == 'W')
|
||||||
|
@ -405,12 +425,12 @@ def main(argv: list[str]):
|
||||||
if errmsg and warnmsg:
|
if errmsg and warnmsg:
|
||||||
summary = f"{errmsg}, {warnmsg}"
|
summary = f"{errmsg}, {warnmsg}"
|
||||||
elif errors:
|
elif errors:
|
||||||
summary = errmsg
|
summary = errmsg or ""
|
||||||
else:
|
else:
|
||||||
summary = warnmsg
|
summary = warnmsg or ""
|
||||||
|
|
||||||
# Call notify.sh
|
# Call notify.sh
|
||||||
res = subprocess.run([notify, summary, email], input=body_text)
|
res = subprocess.run([notify_sh, summary, email], input=body_text)
|
||||||
if res.returncode != 0:
|
if res.returncode != 0:
|
||||||
backup.log('E', f"failed to send notification")
|
backup.log('E', f"failed to send notification")
|
||||||
errors += 1
|
errors += 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user