Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion nixos/home/laptop-backups-watchdog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
# so a nightly failure can never masquerade as freshness;
# (b) both backup timers (pm-dashboard-backup.timer and
# repo-archive-backup.timer) last triggered within the same
# threshold, per `systemctl --user show -p LastTriggerUSec`.
# threshold, per `systemctl --user show -p LastTriggerUSec`;
# (c) when LAPTOP_BACKUPS_OFFSITE_CHECK=1 (the module sets it for
# the service; #3914), the dashboard backup's own date - the
# run record's `date` - is present on the R2 offsite:
# r2bak:supabase-backups/dashboard/<date>/dashboard-<date>.tar.zst
# exists. A complete run record whose upload silently landed
# nothing (2026-10-29..2026-11-01, #3914) passes (a) and (b);
# only an object listing sees the absence. Credentials come
# from Bitwarden exactly as in pm-dashboard-backup.sh, and
# rclone/bws resolve via PATH so tests can stub both.
# repo-archive-backup is covered via its timer's last-trigger time
# only: its script and restic credentials live outside this repo, so
# no run record is required of it (stated in laptop-backups.nix too).
Expand Down Expand Up @@ -101,6 +110,61 @@ for timer in "${timers[@]}"; do
fi
done

# --- (c) offsite presence on R2 ----------------------------------------------
if want_job dashboard && [ "${LAPTOP_BACKUPS_OFFSITE_CHECK:-0}" = "1" ]; then
offsite_date="$(RECORD="$RECORD" python3 - <<'PYEOF' 2>/dev/null
import json, os
try:
print(json.load(open(os.environ["RECORD"]))["date"])
except Exception:
pass
PYEOF
)"
if [ -z "$offsite_date" ]; then
: # (a) already named the missing or unreadable record
elif ! command -v rclone >/dev/null 2>&1 || ! command -v bws >/dev/null 2>&1; then
complain "pm-dashboard-backup: cannot verify offsite presence for $offsite_date: rclone/bws not on PATH"
else
if [ -z "${BWS_ACCESS_TOKEN:-}" ] && [ -f "$HOME/.config/bws/access-token" ]; then
BWS_ACCESS_TOKEN="$(cat "$HOME/.config/bws/access-token")"
export BWS_ACCESS_TOKEN
fi
BWS_LIST="$(bws secret list 2>/dev/null || true)"
bws_get() {
printf '%s' "$BWS_LIST" | python3 -c 'import json,sys
k = sys.argv[1]
for s in json.load(sys.stdin):
if s.get("key") == k:
print(s.get("value", "")); break' "$1" 2>/dev/null
}
R2_ACCESS_KEY_ID="$(bws_get cloudflare.r2-backups-access-key-id)"
R2_SECRET_ACCESS_KEY="$(bws_get cloudflare.r2-backups-secret-access-key)"
R2_ENDPOINT="$(bws_get cloudflare.r2-endpoint)"
if [ -z "$R2_ACCESS_KEY_ID" ] || [ -z "$R2_SECRET_ACCESS_KEY" ] || [ -z "$R2_ENDPOINT" ]; then
complain "pm-dashboard-backup: cannot verify offsite presence for $offsite_date: R2 credentials incomplete in Bitwarden"
else
case "$R2_ENDPOINT" in
http://* | https://*) ;;
*) R2_ENDPOINT="https://$R2_ENDPOINT" ;;
esac
R2_ENDPOINT="${R2_ENDPOINT%/}"
export RCLONE_CONFIG_R2BAK_TYPE="s3"
export RCLONE_CONFIG_R2BAK_PROVIDER="Cloudflare"
export RCLONE_CONFIG_R2BAK_ACCESS_KEY_ID="$R2_ACCESS_KEY_ID"
export RCLONE_CONFIG_R2BAK_SECRET_ACCESS_KEY="$R2_SECRET_ACCESS_KEY"
export RCLONE_CONFIG_R2BAK_ENDPOINT="$R2_ENDPOINT"
export RCLONE_CONFIG_R2BAK_REGION="auto"
# The r2-backups token cannot HeadBucket (see pm-dashboard-backup.sh).
export RCLONE_CONFIG_R2BAK_NO_CHECK_BUCKET="true"
offsite_listing="$(rclone lsf "r2bak:supabase-backups/dashboard/$offsite_date/" 2>/dev/null || true)"
case "$offsite_listing" in
*"dashboard-$offsite_date.tar.zst"*) ;;
*) complain "pm-dashboard-backup: offsite absence on R2: dashboard/$offsite_date/dashboard-$offsite_date.tar.zst not found (run record says complete)" ;;
esac
fi
fi
fi

if [ "$violations" -gt 0 ]; then
echo "laptop-backups-watchdog: $violations violation(s)" >&2
exit 1
Expand Down
41 changes: 41 additions & 0 deletions nixos/home/laptop-backups-watchdog.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ with open(__import__("os").environ["RECORD_OUT"], "w") as fh:
PYEOF
}

# stub bws: `bws secret list` prints dummy entries for the three R2 keys
# (values are never used - the rclone stub does not dial out)
{ printf '#!%s\n' "$BASH"; cat <<'STUB'
printf '%s' '[{"key":"cloudflare.r2-backups-access-key-id","value":"stub-id"},{"key":"cloudflare.r2-backups-secret-access-key","value":"stub-secret"},{"key":"cloudflare.r2-endpoint","value":"stub.r2.example"}]'
STUB
} >"$BIN/bws"
chmod +x "$BIN/bws"

# stub rclone: `rclone lsf <path>` prints the fixture listing in
# $WATCHDOG_STUB_R2_LISTING (an absent/empty file = empty offsite)
{ printf '#!%s\n' "$BASH"; cat <<'STUB'
[[ -n "${WATCHDOG_STUB_R2_LISTING:-}" && -f "$WATCHDOG_STUB_R2_LISTING" ]] && cat "$WATCHDOG_STUB_R2_LISTING"
exit 0
STUB
} >"$BIN/rclone"
chmod +x "$BIN/rclone"

RECORD="$TMP_ROOT/record.json"
run_watchdog() {
LAPTOP_BACKUPS_RECORD="$RECORD" WATCHDOG_STUB_TRIGGERS="$TRIG" \
Expand Down Expand Up @@ -127,6 +144,30 @@ RECORD_OUT="$RECORD" write_record complete "1 hour ago"
set_triggers "1 hour ago" "2 hours"
run_watchdog; expect_named_failure future-timer repo-archive-backup

# offsite check: record date is 2026-10-28 (write_record pins it).
R2_LISTING="$TMP_ROOT/r2-listing.txt"
run_watchdog_offsite() {
LAPTOP_BACKUPS_RECORD="$RECORD" WATCHDOG_STUB_TRIGGERS="$TRIG" \
WATCHDOG_STUB_R2_LISTING="$R2_LISTING" LAPTOP_BACKUPS_OFFSITE_CHECK=1 \
PATH="$BIN:$PATH" bash "$SCRIPT" 26 >"$TMP_ROOT/out.log" 2>&1
RC=$?
}

# offsite present for the record date -> 0
RECORD_OUT="$RECORD" write_record complete "1 hour ago"
set_triggers "1 hour ago" "1 hour ago"
printf 'dashboard-2026-10-28.tar.zst\n' >"$R2_LISTING"
run_watchdog_offsite; expect_ok offsite-present

# offsite absent (planted) while the record says complete -> non-zero
# naming the R2 absence: this is the #3914 failure shape
printf '' >"$R2_LISTING"
run_watchdog_offsite; expect_named_failure offsite-absent R2

# offsite check disabled -> the same absence is not a violation
# (pre-#3914 behavior for jobs/hosts without the opt-in)
run_watchdog; expect_ok offsite-check-disabled

if (( failures > 0 )); then
echo "laptop-backups-watchdog tests: $failures failure(s)" >&2
exit 1
Expand Down
8 changes: 7 additions & 1 deletion nixos/home/laptop-backups.nix
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,13 @@ in
Service = {
Type = "oneshot";
ExecStart = "${watchdogPkg}/bin/laptop-backups-watchdog ${toString cfg.watchdog.maxAgeHours} ${watchdogJobs}";
Environment = "PATH=${servicePath}:${home}/tools/bin:/run/current-system/sw/bin";
# LAPTOP_BACKUPS_OFFSITE_CHECK (#3914): the watchdog also
# asserts the dashboard backup's date dir exists on R2.
# servicePath carries rclone and ~/tools/bin carries bws.
Environment = [
"PATH=${servicePath}:${home}/tools/bin:/run/current-system/sw/bin"
"LAPTOP_BACKUPS_OFFSITE_CHECK=1"
];
};
};
})
Expand Down