Files
linux-backuper/linux_backuper/core/profiles.py
T

105 lines
4.1 KiB
Python

"""Backup profile manager supporting built-in and user-defined custom profiles."""
import json
import os
from typing import Dict, List, Optional
from linux_backuper.core.constants import DEFAULT_CONFIG_PATH
from linux_backuper.models import ProfileConfig
class ProfileManager:
"""Manages built-in profiles (quick, system, full) and custom profiles."""
BUILTIN_PROFILES: Dict[str, ProfileConfig] = {
"quick": ProfileConfig(
name="quick",
description="Default lightweight snapshot of /etc and full host telemetry",
included_paths=["/etc"],
exclude_patterns=[
"*.sock", "*.fifo", "*.lock", "*.pid",
"/etc/mtab", "/etc/resolv.conf"
],
collect_users=True,
collect_kernel=True,
collect_storage=True,
collect_network=True,
collect_packages=True
),
"system": ProfileConfig(
name="system",
description="System configuration, /boot, systemd units and kernel state",
included_paths=["/etc", "/boot", "/usr/lib/systemd/system"],
exclude_patterns=[
"*.sock", "*.fifo", "*.lock", "*.pid",
"/etc/mtab", "/etc/resolv.conf"
],
collect_users=True,
collect_kernel=True,
collect_storage=True,
collect_network=True,
collect_packages=True
),
"full": ProfileConfig(
name="full",
description="Complete disaster-recovery profile including cron, NetworkManager and systemd state",
included_paths=[
"/etc", "/boot",
"/var/spool/cron",
"/var/lib/systemd",
"/var/lib/NetworkManager"
],
exclude_patterns=[
"*.sock", "*.fifo", "*.lock", "*.pid",
"/etc/mtab", "/etc/resolv.conf"
],
collect_users=True,
collect_kernel=True,
collect_storage=True,
collect_network=True,
collect_packages=True,
max_journal_mb=50
)
}
def __init__(self, config_path: str = DEFAULT_CONFIG_PATH):
self.config_path = config_path
self._profiles: Dict[str, ProfileConfig] = dict(self.BUILTIN_PROFILES)
self._load_custom_profiles()
def _load_custom_profiles(self) -> None:
"""Load optional user-defined profiles from JSON configuration."""
if not os.path.isfile(self.config_path):
return
try:
with open(self.config_path, "r", encoding="utf-8") as f:
data = json.load(f)
custom_dict = data.get("profiles", {})
for name, cfg in custom_dict.items():
self._profiles[name] = ProfileConfig(
name=name,
description=cfg.get("description", f"Custom profile '{name}'"),
included_paths=cfg.get("included_paths", ["/etc"]),
exclude_patterns=cfg.get("exclude_patterns", ["*.sock", "*.lock"]),
collect_users=cfg.get("collect_users", True),
collect_kernel=cfg.get("collect_kernel", True),
collect_storage=cfg.get("collect_storage", True),
collect_network=cfg.get("collect_network", True),
collect_packages=cfg.get("collect_packages", True),
max_journal_mb=cfg.get("max_journal_mb", 0)
)
except (OSError, json.JSONDecodeError):
pass
def get_profile(self, name: str) -> ProfileConfig:
"""Retrieve profile configuration by name. Raises KeyError if not found."""
if name not in self._profiles:
valid_names = ", ".join(self._profiles.keys())
raise KeyError(f"Unknown backup profile '{name}'. Available profiles: {valid_names}")
return self._profiles[name]
def list_profiles(self) -> List[ProfileConfig]:
"""List all available profiles."""
return list(self._profiles.values())