icp-canister-snapshot-backup.sh bash
132 lines 3.3 KB
Raw
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor ⚠ breaking 2 days ago
1 #!/usr/bin/env bash
2 # Full ICP canister snapshots: stop -> snapshot create -> start per canister; optional download.
3 # Controller identity required. See docs/ICP-CANISTER-SNAPSHOT-RUNBOOK.md
4 set -euo pipefail
5
6 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7 ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
8 ICP_DIR="$ROOT/hub/icp"
9
10 NETWORK="ic"
11 DOWNLOAD_ROOT=""
12 YES=0
13 CANISTERS=(hub attestation)
14
15 usage() {
16 cat <<'EOF'
17 Usage: icp-canister-snapshot-backup.sh [options]
18
19 Creates an on-chain snapshot per canister: stop -> snapshot create -> start.
20 Default order: hub, then attestation (same as default canister list).
21
22 Options:
23 --network NAME dfx network (default: ic)
24 --download-dir DIR After each create, download snapshot under DIR/<canister>-<UTC>-<snapshotId>/
25 --canisters A,B Comma-separated dfx canister names (default: hub,attestation)
26 --yes Skip interactive confirmation
27 -h, --help This help
28
29 Env:
30 KNOWTATION_SNAPSHOT_YES=1 Same as --yes
31
32 Requires: dfx on PATH; identity with controller rights. Script cds to hub/icp.
33 EOF
34 }
35
36 while [[ $# -gt 0 ]]; do
37 case "$1" in
38 --network)
39 NETWORK="${2:-}"
40 shift 2
41 ;;
42 --download-dir)
43 DOWNLOAD_ROOT="${2:-}"
44 shift 2
45 ;;
46 --canisters)
47 IFS=',' read -ra CANISTERS <<<"${2:-}"
48 shift 2
49 ;;
50 --yes)
51 YES=1
52 shift
53 ;;
54 -h|--help)
55 usage
56 exit 0
57 ;;
58 *)
59 echo "Unknown option: $1" >&2
60 usage >&2
61 exit 1
62 ;;
63 esac
64 done
65
66 if [[ "${KNOWTATION_SNAPSHOT_YES:-}" == "1" ]]; then
67 YES=1
68 fi
69
70 if ! command -v dfx >/dev/null 2>&1; then
71 echo "ERROR: dfx not on PATH. Install IC SDK: https://internetcomputer.org/docs/current/developer-docs/setup/install" >&2
72 exit 1
73 fi
74
75 if [[ ! -d "$ICP_DIR" ]]; then
76 echo "ERROR: hub/icp not found at $ICP_DIR" >&2
77 exit 1
78 fi
79
80 if [[ "$YES" != "1" ]]; then
81 echo "This will STOP each canister on network '$NETWORK' (downtime), then snapshot create, then start."
82 echo "Canisters: ${CANISTERS[*]}"
83 read -r -p "Continue? [y/N] " ans
84 case "$ans" in
85 y | Y | yes | YES) ;;
86 *)
87 echo "Aborted."
88 exit 1
89 ;;
90 esac
91 fi
92
93 cd "$ICP_DIR"
94
95 extract_snapshot_id() {
96 sed -n 's/.*[Ss]napshot ID: *\([^[:space:]]*\).*/\1/p' | tail -1
97 }
98
99 TS="$(date -u +%Y%m%dT%H%M%SZ)"
100
101 for c in "${CANISTERS[@]}"; do
102 c="${c// /}"
103 [[ -z "$c" ]] && continue
104 echo "==> $c: stop"
105 dfx canister stop "$c" --network "$NETWORK"
106 echo "==> $c: snapshot create"
107 create_out="$(dfx canister snapshot create "$c" --network "$NETWORK" 2>&1)" || {
108 echo "$create_out"
109 echo "ERROR: snapshot create failed for $c" >&2
110 echo "Attempting start so canister is not left stopped..." >&2
111 dfx canister start "$c" --network "$NETWORK" || true
112 exit 1
113 }
114 echo "$create_out"
115 sid="$(echo "$create_out" | extract_snapshot_id)"
116 echo "==> $c: start"
117 dfx canister start "$c" --network "$NETWORK"
118
119 if [[ -n "$DOWNLOAD_ROOT" ]]; then
120 if [[ -z "$sid" ]]; then
121 echo "WARN: Could not parse snapshot id for $c; list snapshots manually:" >&2
122 dfx canister snapshot list "$c" --network "$NETWORK" || true
123 else
124 out="${DOWNLOAD_ROOT%/}/${c}-${TS}-${sid}"
125 mkdir -p "$out"
126 echo "==> $c: snapshot download -> $out"
127 dfx canister snapshot download "$c" "$sid" --dir "$out" --network "$NETWORK"
128 fi
129 fi
130 done
131
132 echo "icp-canister-snapshot-backup: OK"
File History 2 commits
sha256:65ccb454656ea5acdea0a10e559b78bcde1eb6ff753ecc2911bc99d1c3d7cadd feat(calendar): enforce agent context tiers in retrieval AP… Human minor 2 days ago
sha256:9103f98c89257ed2b01c237cea895dabb3e85ea337dccb1161c175e4422355b6 docs: accept Calendar Events v0 spec with Phase 0 security … Human 2 days ago