#!/usr/bin/env bash # verify-rsync-command.sh # Verifies every factual claim made in rsync-command.html that can be tested # on this machine. Self-contained: builds its own scratch tree, cleans up on exit. # # Run: bash verify-rsync-command.sh # Exit: 0 if every claim PASSes, 1 otherwise. set -u WORK="$(mktemp -d "${TMPDIR:-/tmp}/verify-rsync.XXXXXX")" trap 'rm -rf "$WORK"' EXIT PASS=0 FAIL=0 ok() { printf 'PASS %s\n' "$1"; PASS=$((PASS+1)); } bad() { printf 'FAIL %s\n' "$1"; FAIL=$((FAIL+1)); } check(){ if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (expected [$3], got [$2])"; fi; } inode() { ls -di "$1" 2>/dev/null | awk '{print $1}'; } finode(){ ls -i "$1" 2>/dev/null | awk '{print $1}'; } printf '== rsync implementation under test ==\n' rsync --version 2>&1 | head -2 printf '\n' # --------------------------------------------------------------------------- # CLAIM 1: --ignore-existing does NOT transfer modified files (the core defect) # --------------------------------------------------------------------------- C1="$WORK/c1"; mkdir -p "$C1/src" "$C1/dst" printf 'v1\n' > "$C1/dst/file.txt" printf 'v2-MODIFIED-LONGER\n' > "$C1/src/file.txt" rsync -av --ignore-existing "$C1/src/" "$C1/dst/" > "$C1/out" 2>&1 IE_RC=$? check "1a --ignore-existing leaves the modified file at its OLD content" \ "$(cat "$C1/dst/file.txt")" "v1" check "1b --ignore-existing still exits 0 (failure is silent)" "$IE_RC" "0" # --------------------------------------------------------------------------- # CLAIM 2: the corrected command DOES transfer the modification # --------------------------------------------------------------------------- C2="$WORK/c2"; mkdir -p "$C2/src" "$C2/dst" printf 'v1\n' > "$C2/dst/file.txt" printf 'v2-MODIFIED-LONGER\n' > "$C2/src/file.txt" rsync -a "$C2/src/" "$C2/dst/" > /dev/null 2>&1 check "2 plain 'rsync -a' DOES propagate the modification" \ "$(cat "$C2/dst/file.txt")" "v2-MODIFIED-LONGER" # --------------------------------------------------------------------------- # CLAIM 3: without -a/-r, rsync refuses to recurse and copies nothing # --------------------------------------------------------------------------- C3="$WORK/c3"; mkdir -p "$C3/src/big" "$C3/dst" printf 'payload\n' > "$C3/src/big/data.bin" rsync --partial --progress "$C3/src/" "$C3/dst/" > "$C3/out" 2>&1 N_COPIED=$(find "$C3/dst" -type f | wc -l | tr -d ' ') check "3a no -a/-r copies ZERO files" "$N_COPIED" "0" if grep -q 'skipping directory' "$C3/out"; then ok "3b rsync prints 'skipping directory'" else bad "3b expected 'skipping directory' in output; got: $(cat "$C3/out")" fi # ...and that -a fixes it rsync -a --partial "$C3/src/" "$C3/dst/" > /dev/null 2>&1 check "3c adding -a copies the file" \ "$(find "$C3/dst" -type f | wc -l | tr -d ' ')" "1" # --------------------------------------------------------------------------- # CLAIM 4: trailing-slash semantics on the SOURCE # --------------------------------------------------------------------------- C4="$WORK/c4" mkdir -p "$C4/source/path" "$C4/destination/path" printf 'x\n' > "$C4/source/path/a.txt" rsync -a "$C4/source/path" "$C4/destination/path" > /dev/null 2>&1 if [ -f "$C4/destination/path/path/a.txt" ]; then ok "4a NO trailing slash nests: destination/path/path/a.txt" else bad "4a expected destination/path/path/a.txt; tree is: $(find "$C4/destination" | tr '\n' ' ')" fi rm -rf "$C4/destination"; mkdir -p "$C4/destination/path" rsync -a "$C4/source/path/" "$C4/destination/path/" > /dev/null 2>&1 if [ -f "$C4/destination/path/a.txt" ] && [ ! -e "$C4/destination/path/path" ]; then ok "4b WITH trailing slash does not nest: destination/path/a.txt" else bad "4b expected destination/path/a.txt only; tree is: $(find "$C4/destination" | tr '\n' ' ')" fi # --------------------------------------------------------------------------- # CLAIM 5: --link-dest hard-links unchanged files, and keeps changed ones apart # --------------------------------------------------------------------------- C5="$WORK/c5"; mkdir -p "$C5/src" printf 'unchanged content\n' > "$C5/src/stable.txt" printf 'day one\n' > "$C5/src/churn.txt" rsync -a "$C5/src/" "$C5/backup.1/" > /dev/null 2>&1 printf 'day two CHANGED\n' > "$C5/src/churn.txt" rsync -a --link-dest="$C5/backup.1" "$C5/src/" "$C5/backup.2/" > "$C5/out" 2>&1 LD_RC=$? check "5a --link-dest run exits 0" "$LD_RC" "0" S1=$(finode "$C5/backup.1/stable.txt"); S2=$(finode "$C5/backup.2/stable.txt") if [ -n "$S1" ] && [ "$S1" = "$S2" ]; then ok "5b unchanged file shares one inode across snapshots (inode $S1)" else bad "5b expected shared inode; got $S1 vs $S2" fi K1=$(finode "$C5/backup.1/churn.txt"); K2=$(finode "$C5/backup.2/churn.txt") if [ -n "$K1" ] && [ "$K1" != "$K2" ]; then ok "5c changed file gets a NEW inode ($K1 -> $K2)" else bad "5c expected different inodes; got $K1 vs $K2" fi LINKS=$(ls -l "$C5/backup.1/stable.txt" | awk '{print $2}') check "5d link count on the shared file is 2" "$LINKS" "2" check "5e old snapshot still holds the OLD content" \ "$(cat "$C5/backup.1/churn.txt")" "day one" check "5f new snapshot holds the NEW content" \ "$(cat "$C5/backup.2/churn.txt")" "day two CHANGED" # --------------------------------------------------------------------------- # CLAIM 6: --delete with --dry-run removes nothing; --max-delete caps + exits 25 # --------------------------------------------------------------------------- C6="$WORK/c6"; mkdir -p "$C6/src" "$C6/dst" printf 'keep\n' > "$C6/src/keep.txt" printf 'keep\n' > "$C6/dst/keep.txt" for i in 1 2 3 4 5; do printf 'old\n' > "$C6/dst/stale$i.txt"; done BEFORE=$(find "$C6/dst" -type f | wc -l | tr -d ' ') rsync -avn --delete "$C6/src/" "$C6/dst/" > "$C6/dry" 2>&1 check "6a --dry-run deletes nothing (file count unchanged)" \ "$(find "$C6/dst" -type f | wc -l | tr -d ' ')" "$BEFORE" if grep -q 'deleting' "$C6/dry"; then ok "6b --dry-run still REPORTS what it would delete" else bad "6b expected 'deleting' lines in dry-run output" fi rsync -av --delete --max-delete=2 "$C6/src/" "$C6/dst/" > "$C6/md" 2>&1 MD_RC=$? check "6c --max-delete=2 exits 25" "$MD_RC" "25" check "6d --max-delete=2 removed exactly 2 of the 5 stale files" \ "$(find "$C6/dst" -name 'stale*' -type f | wc -l | tr -d ' ')" "3" # --------------------------------------------------------------------------- # CLAIM 7: the recommended nightly command works end to end # --------------------------------------------------------------------------- C7="$WORK/c7"; mkdir -p "$C7/src" printf 'stable\n' > "$C7/src/keep.txt" printf 'one\n' > "$C7/src/edit.txt" printf 'bye\n' > "$C7/src/gone.txt" rsync -a "$C7/src/" "$C7/backup.prev/" > /dev/null 2>&1 printf 'two\n' > "$C7/src/edit.txt" # Give the edit a distinct mtime. Without this the write lands in the same # one-second window as the original and rsync's size+mtime quick check treats # the file as unchanged -- the exact trap documented in claim 8 below. touch -t 203001010101 "$C7/src/edit.txt" rm "$C7/src/gone.txt" rsync -a --delete --link-dest="$C7/backup.prev" "$C7/src/" "$C7/backup.today/" > /dev/null 2>&1 R7=$? check "7a recommended command exits 0" "$R7" "0" check "7b modified file IS updated in today's snapshot" \ "$(cat "$C7/backup.today/edit.txt")" "two" check "7c deleted file is absent from today's snapshot" \ "$([ -e "$C7/backup.today/gone.txt" ] && echo present || echo absent)" "absent" check "7d deleted file SURVIVES in yesterday's snapshot" \ "$([ -e "$C7/backup.prev/gone.txt" ] && echo present || echo absent)" "present" P7=$(finode "$C7/backup.prev/keep.txt"); T7=$(finode "$C7/backup.today/keep.txt") if [ -n "$P7" ] && [ "$P7" = "$T7" ]; then ok "7e untouched file is hard-linked, not re-copied (inode $P7)" else bad "7e expected shared inode; got $P7 vs $T7" fi # --------------------------------------------------------------------------- # CLAIM 8: the quick-check trap -- identical size AND identical mtime makes # rsync skip a genuinely changed file; -c catches it. This is the # behaviour described in the "quick-check trap" section. # --------------------------------------------------------------------------- C8="$WORK/c8"; mkdir -p "$C8/src" printf 'one\n' > "$C8/src/edit.txt" rsync -a "$C8/src/" "$C8/prev/" > /dev/null 2>&1 printf 'two\n' > "$C8/src/edit.txt" # same 4-byte size, same 1s mtime window SZ_S=$(wc -c < "$C8/src/edit.txt" | tr -d ' ') SZ_P=$(wc -c < "$C8/prev/edit.txt" | tr -d ' ') MT_S=$(ls -lT "$C8/src/edit.txt" 2>/dev/null | awk '{print $6,$7,$8,$9}') MT_P=$(ls -lT "$C8/prev/edit.txt" 2>/dev/null | awk '{print $6,$7,$8,$9}') check "8a precondition: the two versions are the same size" "$SZ_S" "$SZ_P" if [ "$MT_S" = "$MT_P" ]; then ok "8b precondition: same mtime to 1s ($MT_S)" rsync -a --link-dest="$C8/prev" "$C8/src/" "$C8/out_default/" > /dev/null 2>&1 rsync -ac --link-dest="$C8/prev" "$C8/src/" "$C8/out_csum/" > /dev/null 2>&1 check "8c default quick check SKIPS the change (stale content)" \ "$(cat "$C8/out_default/edit.txt")" "one" check "8d -c detects the change (correct content)" \ "$(cat "$C8/out_csum/edit.txt")" "two" else # Clock ticked between the two writes; the trap cannot be demonstrated. ok "8b SKIPPED - writes straddled a second boundary ($MT_P -> $MT_S); trap not reproducible this run" printf 'SKIP 8c/8d quick-check trap not reproducible (mtimes differ)\n' fi # --------------------------------------------------------------------------- printf '\n== %d passed, %d failed ==\n' "$PASS" "$FAIL" [ "$FAIL" -eq 0 ] || exit 1 exit 0