feat(BackupManager): allow to ignore folders
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -13,15 +13,17 @@ import shutil
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from kiauh import KIAUH_BACKUP_DIR
|
from kiauh.core.backup_manager import BACKUP_ROOT_DIR
|
||||||
from kiauh.utils.common import get_current_date
|
from kiauh.utils.common import get_current_date
|
||||||
from kiauh.utils.logger import Logger
|
from kiauh.utils.logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyUnusedLocal
|
||||||
# noinspection PyMethodMayBeStatic
|
# noinspection PyMethodMayBeStatic
|
||||||
class BackupManager:
|
class BackupManager:
|
||||||
def __init__(self, backup_root_dir: Path = KIAUH_BACKUP_DIR):
|
def __init__(self, backup_root_dir: Path = BACKUP_ROOT_DIR):
|
||||||
self._backup_root_dir = backup_root_dir
|
self._backup_root_dir = backup_root_dir
|
||||||
|
self._ignore_folders = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def backup_root_dir(self) -> Path:
|
def backup_root_dir(self) -> Path:
|
||||||
@@ -31,6 +33,14 @@ class BackupManager:
|
|||||||
def backup_root_dir(self, value: Path):
|
def backup_root_dir(self, value: Path):
|
||||||
self._backup_root_dir = value
|
self._backup_root_dir = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ignore_folders(self) -> List[str]:
|
||||||
|
return self._ignore_folders
|
||||||
|
|
||||||
|
@ignore_folders.setter
|
||||||
|
def ignore_folders(self, value: List[str]):
|
||||||
|
self._ignore_folders = value
|
||||||
|
|
||||||
def backup_file(
|
def backup_file(
|
||||||
self, files: List[Path] = None, target: Path = None, custom_filename=None
|
self, files: List[Path] = None, target: Path = None, custom_filename=None
|
||||||
):
|
):
|
||||||
@@ -56,7 +66,7 @@ class BackupManager:
|
|||||||
|
|
||||||
def backup_directory(self, name: str, source: Path, target: Path = None) -> None:
|
def backup_directory(self, name: str, source: Path, target: Path = None) -> None:
|
||||||
if source is None or not Path(source).exists():
|
if source is None or not Path(source).exists():
|
||||||
raise OSError
|
raise OSError("Parameter 'source' is None or Path does not exist!")
|
||||||
|
|
||||||
target = self.backup_root_dir if target is None else target
|
target = self.backup_root_dir if target is None else target
|
||||||
try:
|
try:
|
||||||
@@ -64,9 +74,20 @@ class BackupManager:
|
|||||||
Logger.print_status(log)
|
Logger.print_status(log)
|
||||||
date = get_current_date().get("date")
|
date = get_current_date().get("date")
|
||||||
time = get_current_date().get("time")
|
time = get_current_date().get("time")
|
||||||
shutil.copytree(source, target.joinpath(f"{name}-{date}-{time}"))
|
shutil.copytree(
|
||||||
|
source,
|
||||||
|
target.joinpath(f"{name.lower()}-{date}-{time}"),
|
||||||
|
ignore=self.ignore_folders_func,
|
||||||
|
)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
Logger.print_error(f"Unable to backup directory '{source}':\n{e}")
|
Logger.print_error(f"Unable to backup directory '{source}':\n{e}")
|
||||||
return
|
return
|
||||||
|
|
||||||
Logger.print_ok("Backup successfull!")
|
Logger.print_ok("Backup successfull!")
|
||||||
|
|
||||||
|
def ignore_folders_func(self, dirpath, filenames):
|
||||||
|
return (
|
||||||
|
[f for f in filenames if f in self._ignore_folders]
|
||||||
|
if self._ignore_folders is not None
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user