Skip to content

Commit 1518362

Browse files
authored
fix: filter ssh commands where dest is not over ssh (#58)
This is one possible solution for the issue I'm facing where some commands: - check for `backup marker` - check that `dest` folder exists - creating the `latest` symlink were actually running on the `src` instead of the `dest`. Run `verbose` and you would note that some commands are now run locally Args are: `--verbose [email protected]:/home/josh/Documents /tmp/edesk.local/Documents ` Snipped output shows where some commands which used to be run `remote` are now run locally ``` rsync-time-machine.py: Running remote command: test -e '/home/josh/Documents' rsync-time-machine.py: Running local command: find '/tmp/edesk.local/Documents/backup.marker' rsync-time-machine.py: Command output: /tmp/edesk.local/Documents/backup.marker rsync-time-machine.py: Running local command: find '/tmp/edesk.local/Documents/' -maxdepth 1 - rsync-time-machine.py: Running remote command: find '/tmp/edesk.local/Documents/backup.inprogress' ... rsync-time-machine.py: Running local command: rm -f -- '/tmp/edesk.local/Documents/latest' rsync-time-machine.py: Running local command: ln -s -- '2024-03-25-172323' '/tmp/edesk.local/Documents/latest' rsync-time-machine.py: Running remote command: rm -f -- '/tmp/edesk.local/Documents/backup.inprogress' ``` * fix: filter ssh commands where dest is not over ssh * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * linting
1 parent 7b96667 commit 1518362

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

rsync_time_machine.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class SSH(NamedTuple):
3131
id_rsa: str | None
3232

3333

34+
def dest_is_ssh(ssh: SSH | None) -> SSH | None:
35+
"""Returns the SSH object only if the destination is remote."""
36+
return ssh if ssh and ssh.dest_folder_prefix else None
37+
38+
3439
COLORS = {
3540
"green": "\033[92m",
3641
"magenta": "\033[95m",
@@ -257,7 +262,7 @@ def find_backups(dest_folder: str, ssh: SSH | None = None) -> list[str]:
257262
(Replaces 'fn_find_backups' in the Bash script).
258263
"""
259264
cmd = f"find '{dest_folder}/' -maxdepth 1 -type d -name '????-??-??-??????' -prune | sort -r"
260-
return run_cmd(cmd, ssh).stdout.splitlines()
265+
return run_cmd(cmd, dest_is_ssh(ssh)).stdout.splitlines()
261266

262267

263268
def expire_backup(
@@ -359,7 +364,7 @@ def backup_marker_path(folder: str) -> str:
359364
def find_backup_marker(folder: str, ssh: SSH | None = None) -> str | None:
360365
"""Find the backup marker file in the given folder."""
361366
marker_path = backup_marker_path(folder)
362-
output = find(marker_path, ssh)
367+
output = find(marker_path, dest_is_ssh(ssh))
363368
return marker_path if output else None
364369

365370

@@ -472,9 +477,9 @@ def ln(src: str, dest: str, ssh: SSH | None = None) -> None:
472477
run_cmd(f"ln -s -- '{src}' '{dest}'", ssh)
473478

474479

475-
def test_file_exists_src(path: str) -> bool:
480+
def test_file_exists_src(path: str, ssh: SSH | None = None) -> bool:
476481
"""Test if a file exists."""
477-
return run_cmd(f"test -e '{path}'", None).returncode == 0
482+
return run_cmd(f"test -e '{path}'", ssh).returncode == 0
478483

479484

480485
def get_file_system_type(path: str, ssh: SSH | None = None) -> str:
@@ -615,8 +620,8 @@ def get_rsync_flags(
615620
rsync_flags += rsync_append_flags.split()
616621

617622
if (
618-
get_file_system_type(src_folder).lower() == "fat"
619-
or get_file_system_type(dest_folder, ssh).lower() == "fat"
623+
get_file_system_type(src_folder, ssh).lower() == "fat"
624+
or get_file_system_type(dest_folder, dest_is_ssh(ssh)).lower() == "fat"
620625
):
621626
log_info("File-system is a version of FAT.")
622627
log_info("Using the --modify-window rsync parameter with value 2.")
@@ -813,11 +818,11 @@ def backup(
813818
allow_host_only=allow_host_only,
814819
)
815820

816-
if not test_file_exists_src(src_folder):
821+
if not test_file_exists_src(src_folder, ssh):
817822
log_error(f"Source folder '{src_folder}' does not exist - aborting.")
818823
sys.exit(1)
819824

820-
check_dest_is_backup_folder(dest_folder, ssh)
825+
check_dest_is_backup_folder(dest_folder, dest_is_ssh(ssh))
821826

822827
now = now_str()
823828
dest = os.path.join(dest_folder, now)
@@ -893,11 +898,11 @@ def backup(
893898

894899
check_rsync_errors(log_file, auto_delete_log)
895900

896-
rm_file(os.path.join(dest_folder, "latest"), ssh)
901+
rm_file(os.path.join(dest_folder, "latest"), dest_is_ssh(ssh))
897902
ln(
898903
os.path.basename(dest),
899904
os.path.join(dest_folder, "latest"),
900-
ssh,
905+
dest_is_ssh(ssh),
901906
)
902907

903908
rm_file(inprogress_file, ssh)

0 commit comments

Comments
 (0)