fix: various check size script tweaks

This commit is contained in:
2023-01-07 13:52:46 +01:00
parent e82a11c526
commit 6fa577e221
2 changed files with 20 additions and 5 deletions

View File

@@ -1,14 +1,17 @@
#!/usr/bin/python
#!/usr/bin/python3
import subprocess
import sys
import os
import argparse
import json
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Backup Dir Size helper")
parser.add_argument('PATH')
parser.add_argument('--save-new-size', action='store_const',
default=False, const=True)
args = parser.parse_args()
# check parameter #
@@ -22,7 +25,8 @@ if __name__ == "__main__":
print("Path does not exist ({}".format(args.PATH))
sys.exit(1)
savepath = os.path.join("/", args.PATH.replace("/", "-"))
savedir = "/opt/backup-info"
savepath = os.path.join(savedir, args.PATH.lstrip("/").replace("/", "-"))
currentSize = 0
if os.path.isfile(savepath):
with open(savepath) as f:
@@ -33,9 +37,14 @@ if __name__ == "__main__":
size = int(p.stdout.split("\n")[-2].split("\t")[0])
if currentSize and currentSize == size:
sys.exit(2)
result = { "state" : "ok", "old" : currentSize, "new" : size }
else:
print("Directory has changed {} to {}".format(currentSize, size))
result = { "state" : "changed", "old" : currentSize, "new" : size }
if args.save_new_size:
with open(savepath, "w") as f:
f.write(str(size))
sys.exit(0)
# return result
print(json.dumps(result))

View File

@@ -13,6 +13,12 @@
state: directory
mode: 711
- name: Ensure backup info dir exists and accessible
file:
name: /opt/backup-info/
state: directory
mode: 700
- name: Copy Backup Helper script
copy:
src: check_dir_size_for_backup.py