From e82a11c526735811e1e679046ec36fb329129e25 Mon Sep 17 00:00:00 2001 From: Sheppy Date: Sat, 7 Jan 2023 13:41:54 +0100 Subject: [PATCH] feat: check dir size backup script --- files/check_dir_size_for_backup.py | 41 ++++++++++++++++++++++++++++++ roles/base/tasks/main.yaml | 12 +++++++++ 2 files changed, 53 insertions(+) create mode 100644 files/check_dir_size_for_backup.py diff --git a/files/check_dir_size_for_backup.py b/files/check_dir_size_for_backup.py new file mode 100644 index 0000000..ded8a1e --- /dev/null +++ b/files/check_dir_size_for_backup.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +import subprocess +import sys +import os +import argparse + +if __name__ == "__main__": + + parser = argparse.ArgumentParser(description="Backup Dir Size helper") + parser.add_argument('PATH') + args = parser.parse_args() + + # check parameter # + if not args.PATH.replace("/", "").isalnum(): + print("Illegal Path: {} (must be alphanum + /)".format(args.PATH)) + sys.exit(1) + elif not args.PATH.startswith("/"): + print("Path mus be absolute ({})".format(args.PATH)) + sys.exit(1) + elif not os.path.isdir(args.PATH): + print("Path does not exist ({}".format(args.PATH)) + sys.exit(1) + + savepath = os.path.join("/", args.PATH.replace("/", "-")) + currentSize = 0 + if os.path.isfile(savepath): + with open(savepath) as f: + currentSize = int(f.read()) + + # check # + p = subprocess.run(["du", args.PATH], capture_output=True, encoding="utf-8") + size = int(p.stdout.split("\n")[-2].split("\t")[0]) + + if currentSize and currentSize == size: + sys.exit(2) + else: + print("Directory has changed {} to {}".format(currentSize, size)) + with open(savepath, "w") as f: + f.write(str(size)) + sys.exit(0) diff --git a/roles/base/tasks/main.yaml b/roles/base/tasks/main.yaml index 3102b9f..39f7c92 100644 --- a/roles/base/tasks/main.yaml +++ b/roles/base/tasks/main.yaml @@ -6,3 +6,15 @@ - net-tools - tcpdump - git + +- name: Ensure Opt dir exists and accessible + file: + name: /opt/ + state: directory + mode: 711 + +- name: Copy Backup Helper script + copy: + src: check_dir_size_for_backup.py + dest: /opt/check_dir_size_for_backup.py + mode: 755