From 667c418f09f6ae0786f53b717d94ca8afc303f8e Mon Sep 17 00:00:00 2001 From: furtest Date: Wed, 7 Jan 2026 23:32:24 +0100 Subject: [PATCH] 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. --- src/unisync/synchroniser.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/unisync/synchroniser.py b/src/unisync/synchroniser.py index 222fc7d..9646c06 100644 --- a/src/unisync/synchroniser.py +++ b/src/unisync/synchroniser.py @@ -14,8 +14,10 @@ import time import logging from pathlib import Path +from typing import cast from unisync.errors import RemoteMountedError, InvalidMountError +from unisync.config import BackupConfig logger = logging.getLogger(__name__) @@ -117,7 +119,9 @@ class Synchroniser: close = subprocess.Popen(command) 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. Args: @@ -127,11 +131,35 @@ class Synchroniser: Returns: 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( f"ssh://{self.remote_user}@{self.remote_ip}/{self.remote_dir}/.data", self.local, paths=paths, - force=force + force=force, + other=other ) def sync_links(self, ignore:list) -> int: