diff --git a/snapcfg.py b/snapcfg.py new file mode 100644 index 0000000..648f172 --- /dev/null +++ b/snapcfg.py @@ -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!")