mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-14 02:54:28 +05:00
feat(backup): add specific backup methods for Klipper and Moonraker configs
This commit is contained in:
@@ -11,9 +11,12 @@ from __future__ import annotations
|
|||||||
import shutil
|
import shutil
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import List, Optional
|
||||||
|
|
||||||
|
from components.klipper.klipper import Klipper
|
||||||
|
from components.moonraker.moonraker import Moonraker
|
||||||
from core.logger import Logger
|
from core.logger import Logger
|
||||||
|
from utils.instance_utils import get_instances
|
||||||
|
|
||||||
|
|
||||||
class BackupService:
|
class BackupService:
|
||||||
@@ -24,6 +27,14 @@ class BackupService:
|
|||||||
def backup_root(self) -> Path:
|
def backup_root(self) -> Path:
|
||||||
return self._backup_root
|
return self._backup_root
|
||||||
|
|
||||||
|
@property
|
||||||
|
def timestamp(self) -> str:
|
||||||
|
return datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||||
|
|
||||||
|
################################################
|
||||||
|
# GENERIC BACKUP METHODS
|
||||||
|
################################################
|
||||||
|
|
||||||
def backup_file(
|
def backup_file(
|
||||||
self,
|
self,
|
||||||
source_path: Path,
|
source_path: Path,
|
||||||
@@ -47,9 +58,9 @@ class BackupService:
|
|||||||
try:
|
try:
|
||||||
self._backup_root.mkdir(parents=True, exist_ok=True)
|
self._backup_root.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
|
||||||
filename = (
|
filename = (
|
||||||
target_name or f"{source_path.stem}_{timestamp}{source_path.suffix}"
|
target_name
|
||||||
|
or f"{source_path.stem}_{self.timestamp}{source_path.suffix}"
|
||||||
)
|
)
|
||||||
if target_path is not None:
|
if target_path is not None:
|
||||||
backup_path = self._backup_root.joinpath(target_path, filename)
|
backup_path = self._backup_root.joinpath(target_path, filename)
|
||||||
@@ -91,8 +102,7 @@ class BackupService:
|
|||||||
try:
|
try:
|
||||||
self._backup_root.mkdir(parents=True, exist_ok=True)
|
self._backup_root.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
backup_dir_name = f"{backup_name}_{self.timestamp}"
|
||||||
backup_dir_name = f"{backup_name}_{timestamp}"
|
|
||||||
|
|
||||||
if target_path is not None:
|
if target_path is not None:
|
||||||
backup_path = self._backup_root.joinpath(target_path, backup_dir_name)
|
backup_path = self._backup_root.joinpath(target_path, backup_dir_name)
|
||||||
@@ -109,3 +119,31 @@ class BackupService:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
Logger.print_error(f"Failed to backup directory '{source_path}': {e}")
|
Logger.print_error(f"Failed to backup directory '{source_path}': {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
################################################
|
||||||
|
# SPECIFIC BACKUP METHODS
|
||||||
|
################################################
|
||||||
|
|
||||||
|
def backup_printer_cfg(self):
|
||||||
|
klipper_instances: List[Klipper] = get_instances(Klipper)
|
||||||
|
for instance in klipper_instances:
|
||||||
|
target_path: Path = self._backup_root.joinpath(
|
||||||
|
instance.data_dir.name, f"config_{self.timestamp}"
|
||||||
|
)
|
||||||
|
self.backup_file(
|
||||||
|
source_path=instance.cfg_file,
|
||||||
|
target_path=target_path,
|
||||||
|
target_name=instance.cfg_file.name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def backup_moonraker_conf(self):
|
||||||
|
moonraker_instances: List[Moonraker] = get_instances(Moonraker)
|
||||||
|
for instance in moonraker_instances:
|
||||||
|
target_path: Path = self._backup_root.joinpath(
|
||||||
|
instance.data_dir.name, f"config_{self.timestamp}"
|
||||||
|
)
|
||||||
|
self.backup_file(
|
||||||
|
source_path=instance.cfg_file,
|
||||||
|
target_path=target_path,
|
||||||
|
target_name=instance.cfg_file.name,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user