Compare commits
No commits in common. "a18b9ed6d0fa49039b95c851ef037baec74e2a79" and "ed1d79d4007f4c323a730671bddd4568f407c13a" have entirely different histories.
a18b9ed6d0
...
ed1d79d400
46
backup.py
46
backup.py
|
@ -251,12 +251,11 @@ 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_sh = str(base / "borg.sh")
|
borg = str(base / "borg.sh")
|
||||||
notify_sh = str(base / "notify.sh")
|
notify = 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:
|
||||||
|
@ -268,13 +267,14 @@ def main(argv: list[str]):
|
||||||
if var == "HOSTNAME":
|
if var == "HOSTNAME":
|
||||||
hostname = value
|
hostname = value
|
||||||
if var == "BORG":
|
if var == "BORG":
|
||||||
borg_sh = value
|
borg = value
|
||||||
if var == "BORG_DIR":
|
if var == "BORG_DIR":
|
||||||
notify_sh = str(pathlib.Path(value) / "notify.sh")
|
notify = pathlib.Path(value) / "notify.sh"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
backup.log('W', f"failed to parse variables from {args.vars}: {str(e)}")
|
self.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_sh,
|
borg = subprocess.Popen([borg,
|
||||||
"create",
|
"create",
|
||||||
"--verbose",
|
"--verbose",
|
||||||
"--progress",
|
"--progress",
|
||||||
|
@ -304,9 +304,6 @@ 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
|
||||||
|
@ -316,20 +313,7 @@ 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:
|
||||||
|
@ -375,12 +359,8 @@ 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 == 2 or borg_saw_errors:
|
elif ret != 0:
|
||||||
backup.log('E', f"borg exited with errors (ret={ret})")
|
backup.log('E', f"borg exited with return code {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')
|
||||||
|
@ -425,12 +405,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 or ""
|
summary = errmsg
|
||||||
else:
|
else:
|
||||||
summary = warnmsg or ""
|
summary = warnmsg
|
||||||
|
|
||||||
# Call notify.sh
|
# Call notify.sh
|
||||||
res = subprocess.run([notify_sh, summary, email], input=body_text)
|
res = subprocess.run([notify, 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