Add 'snapcfg.py'

This commit is contained in:
wp 2021-07-24 13:28:22 -05:00
parent 352619400c
commit d1088dadae
1 changed files with 37 additions and 0 deletions

37
snapcfg.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env python3
#from os import scandir
from hashlib import md5
from pathlib import Path
from datetime import datetime
def _copy(self, target):
import shutil
assert self.is_file()
shutil.copy(str(self), str(target))
def _md5(self):
from hashlib import md5
assert self.is_file()
return md5(self.read_bytes()).hexdigest()
Path.copy = _copy
Path.md5 = _md5
snapdir = Path("/srv/snapdisk")
cfgname = "snapraid.conf"
cfgmaster = Path(f"/etc/{cfgname}")
snapdisks = [x for x in snapdir.iterdir() if x.is_dir()]
print(f"[{datetime.now()}] snapcfg: validating snapraid configuration backups")
for snapdisk in snapdisks:
target = Path(snapdisk, cfgname)
if (target.exists() and (target.md5() != cfgmaster.md5())):
target.rename(target.with_suffix(".bak"))
print(f"[{datetime.now()}] Moved {target} to {target.with_suffix('.bak')}")
if not target.exists():
cfgmaster.copy(target)
if target.exists():
print(f"[{datetime.now()}] Copied {cfgmaster} to {target}")
else:
print(f"[{datetime.now()}] Backup copy of {cfgmaster} to {target} failed!")