GNAI-024: phase1-restore-test.sh — Swap-Schritte einzeln, sha256 als Pass-Kriterium
ci-static / secret-scan (push) Successful in 22s
ci-static / yamllint (push) Successful in 34s
ci-static / ansible-syntax (push) Successful in 32s
ci-static / markdown-links (push) Successful in 24s

Der &&-Chain meldete trotz erfolgreichem Swap FAIL (sha proof line direkt danach
PASS). Jetzt: stop litecoind mit Wait-Loop, einzelne cp/chown/chmod, sha256-Vergleich
allein entscheidet. pct unlock 114 im Preflight gegen Lock durch abgebrochenen Clone.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-12-01 07:57:58 +01:00
parent 75f3d25d75
commit e0610411bd
@@ -0,0 +1,168 @@
#!/bin/bash
# GNAI-024 Abschnitt 10, Phase 1: unattended restore test for CT 114.
# Run on the operator workstation (needs ~/.ssh aliases ghostnet-pve-ansible-deploy
# and ghostnet-ct114-ansible-deploy). No secrets are read or logged: the restic
# password stays on CT 114, the wallet passphrase is NOT needed for this test
# (wallet is only checked loaded + hdseedid; the passphrase unlock stays an
# operator step on the live node).
#
# Steps:
# 1. fresh backupwallet + restic backup on CT 114, note restic snapshot ID
# 2. restic check (metadata + 5% data) on the off-host repo
# 3. restic restore of the wallet into /tmp on CT 114, sha256 vs. local backup copy
# 4. pct clone CT 114 from its latest snapshot into a scratch CT (10.0.0.91)
# 5. replace the scratch wallet.dat with the restic-restored file, boot litecoind,
# compare getwalletinfo.hdseedid with the live node
# 6. destroy the scratch CT, clean temp files, print PASS/FAIL summary
set -uo pipefail
LOG="${LOG:-/tmp/ct114-phase1-restore-test.log}"
exec > >(tee -a "$LOG") 2>&1
SSHOPTS=(-o BatchMode=yes -o ControlMaster=no)
PVE=(ssh -F ~/.ssh/config "${SSHOPTS[@]}" ghostnet-pve-ansible-deploy sudo -n)
CT=(ssh -F ~/.ssh/config "${SSHOPTS[@]}" ghostnet-ct114-ansible-deploy sudo -n)
CTSSH=(ssh -F ~/.ssh/config "${SSHOPTS[@]}" ghostnet-ct114-ansible-deploy)
SCRATCH_ID=199
SCRATCH_IP=10.0.0.91 # static range <= .99; .91 verified free 2026-11-30
SKEY=~/.ssh/id_ed25519_ghostnet_ct114_ansible_deploy
SOPTS=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o ControlMaster=no -i "$SKEY" -o ConnectTimeout=5)
SCRATCH=(ssh -F /dev/null "${SOPTS[@]}" "ansible-deploy@${SCRATCH_IP}")
FAILURES=()
note() { echo "[phase1] $*"; }
pass() { echo "[phase1] PASS: $*"; }
fail() { echo "[phase1] FAIL: $*"; FAILURES+=("$*"); }
RESTIC_REPO='sftp:gn-backup@10.0.0.90:/var/lib/ghostnet-ai-backup/ct114/restic'
# single remote command string: the inner single quotes protect the sftp.command
# value from the remote shell re-splitting (ssh argv join pitfall)
ct_restic() {
"${CTSSH[@]}" "sudo -n /usr/bin/restic $* -r '$RESTIC_REPO' -o 'sftp.command=ssh -F /etc/litecoin-backup/ssh_config gn-backup@10.0.0.90 -s sftp' --password-file /etc/litecoin-backup/restic-password"
}
note "=== GNAI-024 Phase 1 restore test started $(date -Is) ==="
# --- 0. preflight: scratch ID/IP free -------------------------------------
if "${PVE[@]}" pct list | awk '{print $1}' | grep -qx "$SCRATCH_ID"; then
fail "CTID $SCRATCH_ID already in use"; else pass "CTID $SCRATCH_ID free"; fi
if ping -c1 -W1 "$SCRATCH_IP" >/dev/null 2>&1; then
fail "IP $SCRATCH_IP answers ping"; else pass "IP $SCRATCH_IP free"; fi
[ ${#FAILURES[@]} -eq 0 ] || { echo "PHASE1 RESULT: FAIL (preflight)"; exit 1; }
# a previously interrupted clone can leave a lock on the source CT
"${PVE[@]}" pct unlock 114 2>/dev/null || true
# --- 1. fresh wallet backup + restic snapshot on CT 114 -------------------
note "step 1: backupwallet + restic backup on CT 114"
"${CT[@]}" systemctl start litecoin-wallet-backup.service
rc=$?
if [ $rc -eq 0 ] && ! "${CT[@]}" systemctl is-failed litecoin-wallet-backup.service >/dev/null 2>&1; then
pass "litecoin-wallet-backup.service succeeded (rc=0, not failed)"
else
fail "litecoin-wallet-backup.service rc=$rc or failed state"
fi
SNAP_JSON=$(ct_restic snapshots --json --latest 1)
SNAP_ID=$(python3 -c 'import json,sys; print(json.load(sys.stdin)[0]["id"])' <<<"$SNAP_JSON" 2>/dev/null || true)
note "restic snapshot: $SNAP_ID"
[ -n "$SNAP_ID" ] && pass "restic snapshot $SNAP_ID exists" || { fail "no restic snapshot"; echo "PHASE1 RESULT: FAIL"; exit 1; }
# --- 2. restic check on the off-host repo ----------------------------------
note "step 2: restic check"
if ct_restic check --read-data-subset=5% >/dev/null 2>&1; then
pass "restic check clean"
else
fail "restic check reported problems"
fi
# --- 3. restore wallet from restic on CT 114, compare sha256 ---------------
note "step 3: restic restore + sha256 comparison"
"${CT[@]}" rm -rf /tmp/restore-test
ct_restic restore "$SNAP_ID" --target /tmp/restore-test >/dev/null
RESTORED=$("${CT[@]}" find /tmp/restore-test -name 'wallet-*' -type f | sort | tail -1)
[ -n "$RESTORED" ] && pass "restored file: $RESTORED" || fail "no wallet file in restic restore"
# find (not ls-glob): backups/ is 0700 litecoin, so the remote login shell cannot
# expand a glob there; sudo find runs as root and the quoted -name reaches find intact
LOCAL_BAK=$("${CT[@]}" find /var/lib/litecoin/backups -maxdepth 1 -name 'wallet-*' | sort | tail -1)
[ -n "$LOCAL_BAK" ] || fail "no local backup wallet file found"
H_R=$("${CT[@]}" sha256sum "$RESTORED" </dev/null | awk '{print $1}')
H_L=$("${CT[@]}" sha256sum "$LOCAL_BAK" </dev/null | awk '{print $1}')
[ -n "$H_R" ] && [ "$H_R" = "$H_L" ] && pass "sha256 match restic vs local backup ($H_R)" || fail "sha256 mismatch: '$H_R' vs '$H_L'"
# stage the restored wallet on the workstation for the scratch CT
"${CTSSH[@]}" "sudo -n cat $RESTORED" > /tmp/ct114-wallet-restored
[ -s /tmp/ct114-wallet-restored ] && pass "wallet file staged locally ($(stat -c%s /tmp/ct114-wallet-restored) bytes)" || fail "staging failed"
# --- 4. live wallet facts + clone scratch CT from latest snapshot ----------
note "step 4: live hdseedid + pct clone to scratch CT $SCRATCH_ID"
LIVE_HD=$("${CT[@]}" /opt/litecoin/bin/litecoin-cli -conf=/etc/litecoin/litecoin.conf getwalletinfo | python3 -c 'import json,sys; print(json.load(sys.stdin)["hdseedid"])')
note "live hdseedid: $LIVE_HD"
SNAPNAME=$("${PVE[@]}" pct listsnapshot 114 | awk '/ghostnet-daily/{s=$2} END{print s}')
note "cloning from snapshot $SNAPNAME"
"${PVE[@]}" pct clone 114 "$SCRATCH_ID" --snapname "$SNAPNAME"
clone_rc=$?
if "${PVE[@]}" pct list | awk '{print $1}' | grep -qx "$SCRATCH_ID"; then
pass "clone created (rc=$clone_rc; CT $SCRATCH_ID exists)"
else
fail "pct clone failed (rc=$clone_rc, no CT)"
fi
"${PVE[@]}" pct set "$SCRATCH_ID" -hostname ltc-restore-test -protection 0 \
-net0 "name=eth0,bridge=vmbr0,gw=10.0.0.1,ip=${SCRATCH_IP}/24,type=veth" \
-memory 4096 -swap 1024 \
&& pass "scratch CT configured ($SCRATCH_IP)" || fail "pct set failed"
"${PVE[@]}" pct start "$SCRATCH_ID" && pass "scratch CT started" || fail "pct start failed"
BOOTED=0
for i in $(seq 1 30); do
if "${SCRATCH[@]}" -o ConnectTimeout=3 true 2>/dev/null; then BOOTED=1; break; fi
sleep 5
done
[ "$BOOTED" = 1 ] && pass "scratch CT ssh reachable" || fail "scratch CT ssh unreachable"
# --- 5. swap in the restic-restored wallet, boot litecoind, compare --------
if [ "$BOOTED" = 1 ]; then
note "step 5: wallet swap + hdseedid comparison in scratch CT"
cat /tmp/ct114-wallet-restored | "${SCRATCH[@]}" sudo -n tee /tmp/wallet-restored >/dev/null
RSIZE=$("${SCRATCH[@]}" sudo -n stat -c%s /tmp/wallet-restored 2>/dev/null || echo 0)
[ "$RSIZE" = "$(stat -c%s /tmp/ct114-wallet-restored)" ] && pass "wallet staged on scratch ($RSIZE bytes)" || fail "staging on scratch failed (rsize=$RSIZE)"
"${SCRATCH[@]}" sudo -n systemctl stop litecoind
for i in $(seq 1 24); do
"${SCRATCH[@]}" sudo -n systemctl is-active litecoind 2>/dev/null | grep -qx inactive && break
sleep 5
done
# individual steps; the sha256 check below is the pass criterion for the swap
"${SCRATCH[@]}" sudo -n cp /var/lib/litecoin/cold/wallet.dat /var/lib/litecoin/cold/wallet.dat.pretest
"${SCRATCH[@]}" sudo -n cp /tmp/wallet-restored /var/lib/litecoin/cold/wallet.dat
"${SCRATCH[@]}" sudo -n chown litecoin:litecoin /var/lib/litecoin/cold/wallet.dat
"${SCRATCH[@]}" sudo -n chmod 600 /var/lib/litecoin/cold/wallet.dat
SWAP_H=$("${SCRATCH[@]}" sudo -n sha256sum /var/lib/litecoin/cold/wallet.dat </dev/null | awk '{print $1}')
STAGE_H=$(sha256sum /tmp/ct114-wallet-restored | awk '{print $1}')
[ "$SWAP_H" = "$STAGE_H" ] && pass "scratch wallet.dat == restic-restored file ($SWAP_H)" || fail "swap landed wrong content: $SWAP_H vs $STAGE_H"
"${SCRATCH[@]}" sudo -n systemctl start litecoind
RPC_OK=0
for i in $(seq 1 40); do
if "${SCRATCH[@]}" sudo -n /opt/litecoin/bin/litecoin-cli -conf=/etc/litecoin/litecoin.conf getwalletinfo >/dev/null 2>&1; then RPC_OK=1; break; fi
sleep 5
done
[ "$RPC_OK" = 1 ] && pass "litecoind up in scratch with restored wallet" || fail "litecoind did not come up in scratch"
if [ "$RPC_OK" = 1 ]; then
SCRATCH_HD=$("${SCRATCH[@]}" sudo -n /opt/litecoin/bin/litecoin-cli -conf=/etc/litecoin/litecoin.conf getwalletinfo | python3 -c 'import json,sys; d=json.load(sys.stdin); print(d.get("hdseedid",""))')
[ "$SCRATCH_HD" = "$LIVE_HD" ] && pass "hdseedid match: $SCRATCH_HD" || fail "hdseedid mismatch: scratch='$SCRATCH_HD' live='$LIVE_HD'"
ENC=$("${SCRATCH[@]}" sudo -n /opt/litecoin/bin/litecoin-cli -conf=/etc/litecoin/litecoin.conf getwalletinfo | python3 -c 'import json,sys; d=json.load(sys.stdin); print(d.get("unlocked_until"))')
[ "$ENC" = "0" ] && pass "restored wallet loads encrypted+locked (unlocked_until=0)" || fail "restored wallet not locked (unlocked_until=$ENC)"
fi
fi
# --- 6. cleanup -------------------------------------------------------------
note "step 6: cleanup"
"${PVE[@]}" pct stop "$SCRATCH_ID" 2>/dev/null
"${PVE[@]}" pct destroy "$SCRATCH_ID" && pass "scratch CT $SCRATCH_ID destroyed" || fail "scratch destroy failed"
"${CT[@]}" rm -rf /tmp/restore-test
rm -f /tmp/ct114-wallet-restored
echo
if [ ${#FAILURES[@]} -eq 0 ]; then
echo "PHASE1 RESULT: PASS — snapshot clone + off-host restic restore + wallet load verified (hdseedid match, encrypted/locked)"
else
echo "PHASE1 RESULT: FAIL — ${#FAILURES[@]} failure(s):"
printf ' - %s\n' "${FAILURES[@]}"
fi
echo "[phase1] === end $(date -Is), log: $LOG ==="