From ca7f089fe6aaaa12fae48b180f320f99905c3597 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sat, 20 Dec 2025 10:48:40 +0300 Subject: [PATCH] Backups: use dataclass Application for app info --- files/backups/backup-all.py | 40 ++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/files/backups/backup-all.py b/files/backups/backup-all.py index 2d80286..6f7e416 100644 --- a/files/backups/backup-all.py +++ b/files/backups/backup-all.py @@ -10,8 +10,9 @@ import sys import subprocess import logging import pwd +from dataclasses import dataclass from pathlib import Path -from typing import List, Tuple, Optional +from typing import List, Optional import requests import configparser @@ -47,15 +48,21 @@ BACKUP_TARGETS_FILE = "backup-targets" BACKUP_DEFAULT_DIR = "backups" +@dataclass +class Application: + path: Path + owner: str + + class BackupManager: def __init__(self): - self.errors = [] - self.warnings = [] - self.successful_backups = [] + self.errors: List[str] = [] + self.warnings: List[str] = [] + self.successful_backups: List[str] = [] - def get_application_directories(self) -> List[Tuple[str, str]]: - """Get all home directories and their owners""" - app_dirs = [] + def find_applications(self) -> List[Application]: + """Get all application directories and their owners.""" + applications: List[Application] = [] applications_path = Path("/mnt/applications") source_dirs = applications_path.iterdir() @@ -64,14 +71,13 @@ class BackupManager: continue if app_dir.is_dir(): try: - # Get the owner of the directory stat_info = app_dir.stat() owner = pwd.getpwuid(stat_info.st_uid).pw_name - app_dirs.append((str(app_dir), owner)) + applications.append(Application(path=app_dir, owner=owner)) except (KeyError, OSError) as e: logger.warning(f"Could not get owner for {app_dir}: {e}") - return app_dirs + return applications def find_backup_script(self, app_dir: str) -> Optional[str]: """Find backup script in user's home directory""" @@ -134,7 +140,7 @@ class BackupManager: def get_backup_directories(self) -> List[str]: """Collect backup targets according to backup-targets rules""" backup_dirs: List[str] = [] - app_dirs = self.get_application_directories() + applications = self.find_applications() def parse_targets_file(targets_file: Path) -> List[str]: """Parse backup-targets file, skipping comments and empty lines.""" @@ -151,8 +157,8 @@ class BackupManager: self.warnings.append(warning_msg) return targets - for app_dir_str, _ in app_dirs: - app_dir = Path(app_dir_str) + for app in applications: + app_dir = app.path targets_file = app_dir / BACKUP_TARGETS_FILE resolved_targets: List[Path] = [] @@ -317,11 +323,13 @@ class BackupManager: logger.info("Starting backup process") # Get all home directories - app_dirs = self.get_application_directories() - logger.info(f"Found {len(app_dirs)} application directories") + applications = self.find_applications() + logger.info(f"Found {len(applications)} application directories") # Process each user's backup - for app_dir, username in app_dirs: + for app in applications: + app_dir = str(app.path) + username = app.owner logger.info(f"Processing backup for app: {app_dir} (user {username})") # Find backup script