From a257bb5527ed0af243eb0722edb07457a31d7744 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 28 May 2021 00:11:53 -0500 Subject: [PATCH] Made prune only prune files whose hostname matches --- openwrt_backup/__init__.py | 8 +++++--- openwrt_backup/prune.py | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/openwrt_backup/__init__.py b/openwrt_backup/__init__.py index e1bd0db..edac979 100644 --- a/openwrt_backup/__init__.py +++ b/openwrt_backup/__init__.py @@ -13,8 +13,8 @@ from openwrt_backup.config import CONFIG_DATA # noqa from openwrt_backup.prune import get_all_but_last_n -def do_prune(dest_path: Path, n: int, dry): - to_delete = get_all_but_last_n(dest_path, n) +def do_prune(dest_path: Path, n: int, dry, hostname=None): + to_delete = get_all_but_last_n(dest_path, n, hostname) for f in to_delete: if dry: print(f"Deleting: {f}") @@ -42,6 +42,7 @@ def do_backup( else: backup_on_remote = make_backup_on_remote(client, tmp_path) retrieve_backup_on_remote(client, backup_on_remote, dest) + return hostname def main(): @@ -86,7 +87,7 @@ def main(): print(toml.dumps(CONFIG_DATA)) exit(0) - do_backup( + hostname = do_backup( args.name, Path(CONFIG_DATA["ssh"]["root"]).expanduser(), Path(CONFIG_DATA["backup"]["path"]).expanduser(), @@ -99,4 +100,5 @@ def main(): Path(CONFIG_DATA["backup"]["path"]).expanduser(), CONFIG_DATA["backup"]["keep"], args.dry, + hostname, ) diff --git a/openwrt_backup/prune.py b/openwrt_backup/prune.py index 20e3f4c..2c11236 100644 --- a/openwrt_backup/prune.py +++ b/openwrt_backup/prune.py @@ -1,7 +1,10 @@ from pathlib import Path -def get_all_but_last_n(p: Path, n: int): +def get_all_but_last_n(p: Path, n: int, hostname): files = list(p.glob("*.tar.gz")) - sorted_files = sorted(files, key=lambda f: int(f.name.split("__")[0]), reverse=True) + hostname_files = list(filter(lambda f: f.name.split("__")[1] == hostname, files)) + sorted_files = sorted( + hostname_files, key=lambda f: int(f.name.split("__")[0]), reverse=True + ) return sorted_files[n:]