feat: add archive listing and resolution to restore.sh

This commit is contained in:
kbe
2026-07-25 18:57:21 +02:00
parent 49ed87b46b
commit 769eeea2b9
3 changed files with 105 additions and 0 deletions
+13
View File
@@ -52,6 +52,19 @@ run_cmd() {
"$@"
}
resolve_archive() {
if [[ -n "$ARCHIVE_OVERRIDE" ]]; then
RESOLVED_ARCHIVE="$ARCHIVE_OVERRIDE"
return 0
fi
RESOLVED_ARCHIVE="$(borg list --short --lock-wait 60 2>/dev/null | tail -n1)"
[[ -n "$RESOLVED_ARCHIVE" ]] || die "no archives found in repo $REPO"
}
list_archives() {
borg list --lock-wait 60
}
usage() {
cat <<'EOF'
Usage:
+79
View File
@@ -0,0 +1,79 @@
#!/bin/bash
# Builds fake borg/docker/mysql/mariadb executables into $1 so restore.sh
# can be exercised without real infrastructure. Each mock logs its
# invocation to $1/mock.log.
setup_mock_bin() {
local dir="$1"
mkdir -p "$dir"
local mocklog="$dir/mock.log"
: > "$mocklog"
cat > "$dir/borg" <<EOF
#!/bin/bash
echo "borg \$*" >> "$mocklog"
case "\$1" in
list)
echo "host-2026-01-01T00-00-00"
echo "host-2026-06-01T00-00-00"
;;
extract)
shift
paths=()
for a in "\$@"; do
case "\$a" in
-*) ;;
*::*) ;;
*) paths+=("\$a") ;;
esac
done
for p in "\${paths[@]}"; do
if [[ "\$p" == *.* ]]; then
mkdir -p "\$(dirname "\$p")"
printf 'mock-content\n' > "\$p"
else
mkdir -p "\$p"
touch "\$p/RESTORED_MARKER"
fi
done
;;
*) ;;
esac
exit 0
EOF
cat > "$dir/docker" <<EOF
#!/bin/bash
echo "docker \$*" >> "$mocklog"
case "\$1" in
inspect)
if [[ "\$*" == *"State.Running"* ]]; then
echo true
else
echo healthy
fi
;;
start|stop)
exit 0
;;
exec)
case "\$*" in
*"command -v mariadb"*) exit 0 ;;
*) cat >/dev/null 2>&1 || true; exit 0 ;;
esac
;;
*) ;;
esac
exit 0
EOF
cat > "$dir/mysql" <<EOF
#!/bin/bash
echo "mysql \$*" >> "$mocklog"
cat >/dev/null 2>&1 || true
exit 0
EOF
cp "$dir/mysql" "$dir/mariadb"
chmod +x "$dir/borg" "$dir/docker" "$dir/mysql" "$dir/mariadb"
}
+13
View File
@@ -47,9 +47,22 @@ test_unknown_command_exits_one() {
assert_eq "1" "$rc" "unknown command exits 1"
}
source "$HERE/lib/setup_mocks.sh"
test_list_archives() {
local mockdir out
mockdir="$(mktemp -d)"
setup_mock_bin "$mockdir"
out="$(PATH="$mockdir:$PATH" bash "$RESTORE" --list-archives)"
assert_contains "$out" "host-2026-01-01T00-00-00" "list-archives shows first archive"
assert_contains "$out" "host-2026-06-01T00-00-00" "list-archives shows latest archive"
rm -rf "$mockdir"
}
test_help_exits_zero
test_no_args_exits_one
test_unknown_command_exits_one
test_list_archives
echo "-----"
if [[ "$FAILURES" -gt 0 ]]; then