From 03a9ff353721ba8fb5f37cad2546d25788011451 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Thu, 25 Jun 2026 01:07:31 -0400 Subject: fix: harden rsyncshot destination, rotation, and mount handling Refuse to run when REMOTE_PATH or MOUNTDIR is empty or non-absolute. A blank config value otherwise resolves the destination to the filesystem root, where rotation runs rm -rf and --delete. Run the rotation cp/mv/rm as bare command names in remote mode, resolved by the remote PATH, instead of hardcoded /usr/bin paths that broke on remotes whose binaries live elsewhere. Resolve the real binary via command -v in local mode. Pass rsync -R so each source is stored under its full path. Two includes sharing a basename (/usr/local/bin and /usr/bin) previously both mapped to latest/bin, and the second sync's --delete wiped the first. This changes the stored layout: /usr/local/bin now lives at latest/usr/local/bin instead of latest/bin. /home and /etc are unchanged. Add rsync --numeric-ids so /etc and /home ownership survives a restore when the destination has a different passwd/group database. Match mount points exactly via is_mounted() instead of a substring grep of /proc/mounts, so /media/backup no longer matches /media/backup2, paths compare literally, and mount points with spaces work. Reject a retention count below 1, which previously still created one snapshot. Drop the grep "|| echo 0" that emitted a stray second line, assemble ssh options as arrays, and quote the RSYNC_RSH identity path. Extract derive_paths() and is_mounted() as sourceable functions and add unit, rsync-flag, and gated remote-mode test suites. The suite now runs 36 tests, and shellcheck is clean across the script and tests. --- tests/cases/test_remote.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/cases/test_remote.sh (limited to 'tests/cases/test_remote.sh') diff --git a/tests/cases/test_remote.sh b/tests/cases/test_remote.sh new file mode 100644 index 0000000..c2ee75e --- /dev/null +++ b/tests/cases/test_remote.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# ============================================================================== +# Remote (SSH) Mode Tests +# ============================================================================== +# First coverage of the remote path — backup + rotation run over SSH. The whole +# transfer and rotation happen via ssh to localhost, which exercises run_cmd's +# remote branch and the bare cp/mv/rm rotation commands. Gated: if root can't +# ssh to localhost with key auth, the suite skips rather than fails, so machines +# without loopback SSH still pass. + +source "$(dirname "${BASH_SOURCE[0]}")/../lib/test_helpers.sh" + +remote_available() { + ssh -o BatchMode=yes -o ConnectTimeout=5 localhost true 2>/dev/null +} + +# ------------------------------------------------------------------------------ +# Backup + rotation over SSH-to-localhost +# ------------------------------------------------------------------------------ +test_remote_backup_and_rotation() { + setup_test_env + cat > "$TEST_CONFIG_DIR/config" </dev/null + create_test_excludes >/dev/null + + # Two runs to exercise rotation over SSH (MANUAL.0 rotates to MANUAL.1) + INSTALLHOME="$TEST_CONFIG_DIR" "$SCRIPT_PATH" manual 2 >/dev/null 2>&1 + INSTALLHOME="$TEST_CONFIG_DIR" "$SCRIPT_PATH" manual 2 >/dev/null 2>&1 + + assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" "remote backup should create MANUAL.0" || { teardown_test_env; return 1; } + assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.1" "remote rotation should produce MANUAL.1" || { teardown_test_env; return 1; } + assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/home/testuser/file1.txt" "remote backup should copy files" || { teardown_test_env; return 1; } + + teardown_test_env +} + +# ------------------------------------------------------------------------------ +# Run tests +# ------------------------------------------------------------------------------ +run_remote_tests() { + echo "" + echo "Running remote (SSH) tests..." + echo "------------------------------------------------------------" + + if ! remote_available; then + echo " SKIP: root cannot ssh to localhost with key auth — skipping remote tests" + return 0 + fi + + run_test "remote backup + rotation over SSH" test_remote_backup_and_rotation +} + +# Run if executed directly +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + run_remote_tests + print_summary +fi -- cgit v1.2.3