synchroniser : add backup to sync_files

Adds the option to enable backup when synchronising.
This is done in sync_files by passing the appropriate arguments to sync.
For this we need to add an argument to sync_files as the backup
configuration options are needed.
The configuration options are imported from unisync.config.BackupConfig.
Also import typing.cast to be able to narrow down a type.
This commit is contained in:
2026-01-07 23:32:24 +01:00
parent f618932584
commit 667c418f09

View File

@@ -14,8 +14,10 @@ import time
import logging import logging
from pathlib import Path from pathlib import Path
from typing import cast
from unisync.errors import RemoteMountedError, InvalidMountError from unisync.errors import RemoteMountedError, InvalidMountError
from unisync.config import BackupConfig
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -117,7 +119,9 @@ class Synchroniser:
close = subprocess.Popen(command) close = subprocess.Popen(command)
return close.wait() return close.wait()
def sync_files(self, paths:list, force:bool=False) -> int: def sync_files(self, paths:list, force:bool=False,
backup:BackupConfig | None = None
) -> int:
"""Synchronises the files. """Synchronises the files.
Args: Args:
@@ -127,11 +131,35 @@ class Synchroniser:
Returns: Returns:
The return code of sync. The return code of sync.
""" """
other = list()
if(backup != None and backup.enabled):
backup = cast(BackupConfig, backup)
other.append("-backup")
if(backup.selection != ""):
other.append(backup.selection)
else:
other.append("Name *")
other.append([
"-backuploc",
backup.location,
"-maxbackups",
backup.max_backups,
"-backupsuffix",
backup.backupsuffix,
"-backupprefix",
backup.backupprefix,
"-ignore",
backup.backupprefix
])
return self.sync( return self.sync(
f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data", f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data",
self.local, self.local,
paths=paths, paths=paths,
force=force force=force,
other=other
) )
def sync_links(self, ignore:list) -> int: def sync_links(self, ignore:list) -> int: