Browse Source

backup: parse vars.sh and use hostname from that

master
Jim Paris 7 months ago
parent
commit
f2b47dcba2
1 changed files with 26 additions and 7 deletions
  1. +26
    -7
      backup.py

+ 26
- 7
backup.py View File

@@ -241,10 +241,8 @@ def main(argv: list[str]):
base = pathlib.Path(__file__).parent
parser.add_argument('-c', '--config',
help="Config file", default=str(base / "config.yaml"))
parser.add_argument('-b', '--borg',
help="Borg command", default=str(base / "borg.sh"))
parser.add_argument('-N', '--notify',
help="Notify command", default=str(base / "notify.sh"))
parser.add_argument('-v', '--vars',
help="Variables file", default=str(base / "vars.sh"))
parser.add_argument('-n', '--dry-run', action="store_true",
help="Just print log output, don't run borg")
parser.add_argument('-d', '--debug', action="store_true",
@@ -253,6 +251,27 @@ def main(argv: list[str]):
args = parser.parse_args()
config = Config(args.config)

# Parse variables from vars.sh
hostname = os.uname().nodename
borg = str(base / "borg.sh")
notify = str(base / "notify.sh")
try:
with open(args.vars) as f:
for line in f:
m = re.match(r"\s*export\s*([A-Z_]+)=(.*)", line)
if not m:
continue
var = m.group(1)
value = m.group(2)
if var == "HOSTNAME":
hostname = value
if var == "BORG":
borg = value
if var == "BORG_DIR":
notify = pathlib.Path(value) / "notify.sh"
except Exception as e:
self.log('W', f"failed to parse variables from {args.vars}: {str(e)}")

# Run backup
backup = Backup(config, args.dry_run)
captured_output: list[bytes] = []
@@ -265,7 +284,7 @@ def main(argv: list[str]):
backup.run(out)
sys.stdout.flush()
else:
borg = subprocess.Popen([args.borg,
borg = subprocess.Popen([borg,
"create",
"--verbose",
"--list",
@@ -275,7 +294,7 @@ def main(argv: list[str]):
"--compression", "zstd,3",
"--paths-from-stdin",
"--paths-delimiter", "\\0",
"::{hostname}-{now:%Y%m%d-%H%M%S}"],
"::" + hostname + "-{now:%Y%m%d-%H%M%S}"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
@@ -367,7 +386,7 @@ def main(argv: list[str]):
summary = warnmsg

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


Loading…
Cancel
Save