backup: add application finder class
This commit is contained in:
+33
-30
@@ -43,7 +43,6 @@ logger = logging.getLogger(__name__)
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Config:
|
class Config:
|
||||||
host_name: str
|
host_name: str
|
||||||
roots: List[Path]
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -188,21 +187,9 @@ class AppriseNotifier(Notifier):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class BackupManager:
|
class ApplicationFinder:
|
||||||
def __init__(
|
def __init__(self, roots: List[Path]):
|
||||||
self,
|
self.roots = roots
|
||||||
config: Config,
|
|
||||||
roots: List[Path],
|
|
||||||
storages: List[Storage],
|
|
||||||
notifiers: List[Notifier],
|
|
||||||
):
|
|
||||||
self.errors: List[str] = []
|
|
||||||
self.warnings: List[str] = []
|
|
||||||
self.successful_backups: List[str] = []
|
|
||||||
self.config = config
|
|
||||||
self.roots: List[Path] = roots
|
|
||||||
self.storages = storages
|
|
||||||
self.notifiers = notifiers
|
|
||||||
|
|
||||||
def find_applications(self) -> List[Application]:
|
def find_applications(self) -> List[Application]:
|
||||||
"""Get all application directories and their owners."""
|
"""Get all application directories and their owners."""
|
||||||
@@ -222,6 +209,21 @@ class BackupManager:
|
|||||||
|
|
||||||
return applications
|
return applications
|
||||||
|
|
||||||
|
|
||||||
|
class BackupManager:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: Config,
|
||||||
|
storages: List[Storage],
|
||||||
|
notifiers: List[Notifier],
|
||||||
|
):
|
||||||
|
self.errors: List[str] = []
|
||||||
|
self.warnings: List[str] = []
|
||||||
|
self.successful_backups: List[str] = []
|
||||||
|
self.config = config
|
||||||
|
self.storages = storages
|
||||||
|
self.notifiers = notifiers
|
||||||
|
|
||||||
def find_backup_script(self, app_dir: str) -> Optional[str]:
|
def find_backup_script(self, app_dir: str) -> Optional[str]:
|
||||||
"""Find backup script in user's home directory"""
|
"""Find backup script in user's home directory"""
|
||||||
possible_scripts = [
|
possible_scripts = [
|
||||||
@@ -280,10 +282,9 @@ class BackupManager:
|
|||||||
self.errors.append(f"App {username}: {error_msg}")
|
self.errors.append(f"App {username}: {error_msg}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_backup_directories(self) -> List[str]:
|
def get_backup_directories(self, applications: List[Application]) -> List[str]:
|
||||||
"""Collect backup targets according to backup-targets rules"""
|
"""Collect backup targets according to backup-targets rules"""
|
||||||
backup_dirs: List[str] = []
|
backup_dirs: List[str] = []
|
||||||
applications = self.find_applications()
|
|
||||||
|
|
||||||
def parse_targets_file(targets_file: Path) -> List[str]:
|
def parse_targets_file(targets_file: Path) -> List[str]:
|
||||||
"""Parse backup-targets file, skipping comments and empty lines."""
|
"""Parse backup-targets file, skipping comments and empty lines."""
|
||||||
@@ -369,12 +370,9 @@ class BackupManager:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send notification: {str(e)}")
|
logger.error(f"Failed to send notification: {str(e)}")
|
||||||
|
|
||||||
def run_backup_process(self) -> bool:
|
def run_backup_process(self, applications: List[Application]) -> bool:
|
||||||
"""Main backup process"""
|
"""Main backup process"""
|
||||||
logger.info("Starting backup process")
|
logger.info("Starting backup process")
|
||||||
|
|
||||||
# Get all home directories
|
|
||||||
applications = self.find_applications()
|
|
||||||
logger.info(f"Found {len(applications)} application directories")
|
logger.info(f"Found {len(applications)} application directories")
|
||||||
|
|
||||||
# Process each user's backup
|
# Process each user's backup
|
||||||
@@ -397,7 +395,7 @@ class BackupManager:
|
|||||||
self.run_app_backup(backup_script, app_dir, username)
|
self.run_app_backup(backup_script, app_dir, username)
|
||||||
|
|
||||||
# Get backup directories
|
# Get backup directories
|
||||||
backup_dirs = self.get_backup_directories()
|
backup_dirs = self.get_backup_directories(applications)
|
||||||
logger.info(f"Found backup directories: {backup_dirs}")
|
logger.info(f"Found backup directories: {backup_dirs}")
|
||||||
|
|
||||||
overall_success = True
|
overall_success = True
|
||||||
@@ -426,7 +424,9 @@ class BackupManager:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def initialize(config_path: Path) -> BackupManager:
|
def initialize(
|
||||||
|
config_path: Path,
|
||||||
|
) -> tuple[ApplicationFinder, BackupManager]:
|
||||||
try:
|
try:
|
||||||
with config_path.open("rb") as config_file:
|
with config_path.open("rb") as config_file:
|
||||||
raw_config = tomllib.load(config_file)
|
raw_config = tomllib.load(config_file)
|
||||||
@@ -463,17 +463,20 @@ def initialize(config_path: Path) -> BackupManager:
|
|||||||
if not notifiers:
|
if not notifiers:
|
||||||
raise ValueError("At least one notification backend must be configured")
|
raise ValueError("At least one notification backend must be configured")
|
||||||
|
|
||||||
config = Config(host_name=host_name, roots=roots)
|
config = Config(host_name=host_name)
|
||||||
|
app_finder = ApplicationFinder(roots)
|
||||||
return BackupManager(
|
backup_manager = BackupManager(
|
||||||
config=config, roots=roots, storages=storages, notifiers=notifiers
|
config=config, storages=storages, notifiers=notifiers
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return app_finder, backup_manager
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
try:
|
try:
|
||||||
backup_manager = initialize(CONFIG_PATH)
|
app_finder, backup_manager = initialize(CONFIG_PATH)
|
||||||
success = backup_manager.run_backup_process()
|
applications = app_finder.find_applications()
|
||||||
|
success = backup_manager.run_backup_process(applications)
|
||||||
if not success:
|
if not success:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|||||||
Reference in New Issue
Block a user