Regularly back up Documents/ and some configuration files

This commit is contained in:
Ohad Livne 2025-06-04 22:44:52 +03:00
parent 4f268ea778
commit 75591ce5af
Signed by: libohad-dev
GPG key ID: 34FDC68B51191A4D
3 changed files with 75 additions and 0 deletions

52
.local/bin/dirtree-changed Executable file
View file

@ -0,0 +1,52 @@
#! /usr/bin/python3
from pathlib import Path
def get_subtree_mtime(root: str | Path) -> float:
from itertools import chain
from os import walk
from os.path import getmtime
maxtime = 0
for dirname, _, filenames in walk(root):
dirpath = Path(dirname)
maxtime = max(
chain(
[maxtime, getmtime(dirpath)],
(getmtime(dirpath / filename) for filename in filenames),
)
)
return maxtime
def tree_changed_p(root: str | Path, check_file: str | Path) -> bool:
from os.path import getmtime
try:
filetime = getmtime(check_file)
except FileNotFoundError:
return True
return get_subtree_mtime(root) > filetime
if __name__ == "__main__":
import argparse
import sys
parser = argparse.ArgumentParser(description="Manage the desktop containers")
parser.add_argument("--directory", required=True, help="Directory tree to check")
parser.add_argument(
"--check-file", required=True, help="Timestamp file for comparison"
)
args = parser.parse_args()
if tree_changed_p(root=args.directory, check_file=args.check_file):
sys.exit(0)
else:
sys.exit(1)