Browse Source

Made prune only prune files whose hostname matches

master
Raphael Roberts 5 years ago
parent
commit
a257bb5527
  1. 8
      openwrt_backup/__init__.py
  2. 7
      openwrt_backup/prune.py

8
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,
)

7
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:]
Loading…
Cancel
Save