mirror of
https://github.com/FAUSheppy/no-secrets-athq-ansible
synced 2025-12-09 13:48:34 +01:00
feat: check dir size backup script
This commit is contained in:
41
files/check_dir_size_for_backup.py
Normal file
41
files/check_dir_size_for_backup.py
Normal file
@@ -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)
|
||||||
@@ -6,3 +6,15 @@
|
|||||||
- net-tools
|
- net-tools
|
||||||
- tcpdump
|
- tcpdump
|
||||||
- git
|
- 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
|
||||||
|
|||||||
Reference in New Issue
Block a user