@jamiefutch/pi-backup 1.1.11 → 1.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/restore-pi.sh CHANGED
@@ -20,6 +20,19 @@ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
20
20
  warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
21
21
  err() { echo -e "${RED}[ERR]${NC} $*"; }
22
22
 
23
+ # ─── Cleanup ─────────────────────────────────────────────────────
24
+ # Registered before any temp dir is created, so every exit path (abort, error,
25
+ # success) removes the extraction dir.
26
+ STAGE=""
27
+ cleanup() {
28
+ local rc=$?
29
+ if [[ -n "$STAGE" && -d "$STAGE" ]]; then
30
+ rm -rf "$STAGE"
31
+ fi
32
+ return $rc
33
+ }
34
+ trap cleanup EXIT
35
+
23
36
  # ─── Usage ───────────────────────────────────────────────────────
24
37
  usage() {
25
38
  cat <<EOF
@@ -38,6 +51,9 @@ zip/7z archive (as produced by backup-pi.sh).
38
51
  Prerequisites:
39
52
  - Fresh Pi install completed (pi --version works)
40
53
  - Pi NOT running
54
+
55
+ Environment:
56
+ PI_BACKUP_ASSUME_YES=1 Skip the confirmation prompt (for scripted use)
41
57
  EOF
42
58
  exit 1
43
59
  }
@@ -56,10 +72,10 @@ BACKUP_ARG="$1"
56
72
  if [[ -d "$BACKUP_ARG" ]]; then
57
73
  # Uncompressed directory backup
58
74
  BACKUP_DIR="$BACKUP_ARG"
59
- STAGE=""
60
75
  elif [[ -f "$BACKUP_ARG" && "$BACKUP_ARG" =~ \.(zip|7z)$ ]]; then
61
76
  # Compressed archive: extract to a temp dir first
62
- STAGE="$(mktemp -d)"
77
+ # Explicit template: BSD mktemp ignores $TMPDIR when called without one.
78
+ STAGE="$(mktemp -d "${TMPDIR:-/tmp}/pi-restore.XXXXXX")"
63
79
  log "Extracting archive: $BACKUP_ARG"
64
80
  tool="$(pick_extract_tool)"
65
81
  if [[ -n "$tool" ]]; then
@@ -71,7 +87,7 @@ elif [[ -f "$BACKUP_ARG" && "$BACKUP_ARG" =~ \.(zip|7z)$ ]]; then
71
87
  fi
72
88
  # The archive holds one top-level pi-backup-* dir
73
89
  BACKUP_DIR="$(find "$STAGE" -mindepth 1 -maxdepth 1 -type d | head -1)"
74
- [[ -n "$BACKUP_DIR" ]] || { err "No backup directory found inside archive."; rm -rf "$STAGE"; exit 1; }
90
+ [[ -n "$BACKUP_DIR" ]] || { err "No backup directory found inside archive."; exit 1; }
75
91
  log "Extracted to: $BACKUP_DIR"
76
92
  else
77
93
  err "Backup not found: $BACKUP_ARG (need a directory, .zip, or .7z)"
@@ -88,20 +104,44 @@ warn "This will OVERWRITE all current Pi configuration."
88
104
  warn "Backup source: $BACKUP_DIR"
89
105
  warn "Target: $PI_ROOT"
90
106
  warn "Pi must NOT be running."
107
+
108
+ # Best-effort running-Pi check. It only warns: process names are unreliable.
109
+ if command -v pgrep >/dev/null 2>&1 && { pgrep -x pi >/dev/null 2>&1 || pgrep -f 'pi-coding-agent' >/dev/null 2>&1; }; then
110
+ warn "A Pi process appears to be running. Exit Pi first: files restored under a"
111
+ warn "running Pi can be overwritten or corrupted when it exits."
112
+ fi
91
113
  echo
92
- read -rp "Continue? [y/N] " -n 1
93
- echo
94
- [[ $REPLY =~ ^[Yy]$ ]] || { log "Aborted."; exit 0; }
114
+
115
+ if [[ "${PI_BACKUP_ASSUME_YES:-0}" == "1" ]]; then
116
+ log "PI_BACKUP_ASSUME_YES=1 set; skipping confirmation."
117
+ else
118
+ REPLY=""
119
+ if ! read -rp "Continue? [y/N] " -n 1 REPLY; then
120
+ echo
121
+ err "Cannot read a confirmation (stdin is not interactive)."
122
+ err "Run this in a terminal, or set PI_BACKUP_ASSUME_YES=1 to skip the prompt."
123
+ exit 1
124
+ fi
125
+ echo
126
+ [[ $REPLY =~ ^[Yy]$ ]] || { log "Aborted."; exit 0; }
127
+ fi
128
+
129
+ # ─── Manifest ────────────────────────────────────────────────────
130
+ # What gets restored is defined once, in backup-manifest.txt (shared with backup).
131
+ MANIFEST_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/backup-manifest.txt"
132
+ [[ -f "$MANIFEST_FILE" ]] || { err "backup-manifest.txt not found next to $0"; exit 1; }
95
133
 
96
134
  # ─── Helpers ─────────────────────────────────────────────────────
135
+ strip_cr() { printf '%s' "${1%$'\r'}"; }
136
+
97
137
  restore_file() {
98
138
  local src="$1" dst="$2"
99
139
  if [[ -f "$src" ]]; then
100
140
  mkdir -p "$(dirname "$dst")"
101
141
  cp "$src" "$dst"
102
- ok " $(basename "$src")"
142
+ ok " ${src#"$BACKUP_DIR"/}"
103
143
  else
104
- warn " $(basename "$src") — not in backup, skipping"
144
+ warn " ${src#"$BACKUP_DIR"/} — not in backup, skipping"
105
145
  fi
106
146
  }
107
147
 
@@ -117,6 +157,54 @@ restore_dir() {
117
157
  fi
118
158
  }
119
159
 
160
+ restore_manifest_entries() {
161
+ local total step=0 kind f1 f2 f3
162
+ total="$(grep -c '^section|' "$MANIFEST_FILE" || true)"
163
+ while IFS='|' read -r kind f1 f2 f3 || [[ -n "$kind" ]]; do
164
+ kind="$(strip_cr "$kind")"; f1="$(strip_cr "${f1:-}")"; f2="$(strip_cr "${f2:-}")"; f3="$(strip_cr "${f3:-}")"
165
+ case "$kind" in
166
+ ''|'#'*) continue ;;
167
+ section)
168
+ step=$((step+1)); log "$step/$total $f1..." ;;
169
+ file)
170
+ restore_file "$BACKUP_DIR/$f2" "$PI_ROOT/$f1"
171
+ # Credentials: keep a restored auth.json private to this user
172
+ if [[ "$f2" == "auth.json" && -f "$BACKUP_DIR/$f2" && -f "$PI_ROOT/$f1" ]]; then chmod 600 "$PI_ROOT/$f1"; fi ;;
173
+ dir)
174
+ restore_dir "$BACKUP_DIR/$f2" "$PI_ROOT/$f1" ;;
175
+ dir-each|recent)
176
+ if [[ -d "$BACKUP_DIR/$f2" ]]; then
177
+ local sub name
178
+ for sub in "$BACKUP_DIR/$f2"/*/; do
179
+ [[ -d "$sub" ]] || continue
180
+ name="$(basename "$sub")"
181
+ restore_dir "$sub" "$PI_ROOT/$f1/$name"
182
+ done
183
+ ok " $f2/ restored"
184
+ else
185
+ warn " No $f2 in backup"
186
+ fi ;;
187
+ local-ext)
188
+ if [[ -d "$BACKUP_DIR/local-extensions/$f1" ]]; then
189
+ mkdir -p "$LOCAL_EXT_ROOT/$f1"
190
+ cp -a "$BACKUP_DIR/local-extensions/$f1/." "$LOCAL_EXT_ROOT/$f1/"
191
+ ok " $f1/ restored"
192
+ else
193
+ warn " $f1 — not in backup, skipping"
194
+ fi ;;
195
+ npm)
196
+ if [[ -d "$BACKUP_DIR/$f2" ]]; then
197
+ mkdir -p "$PI_ROOT/$f1"
198
+ cp -a "$BACKUP_DIR/$f2/." "$PI_ROOT/$f1/"
199
+ ok " npm manifests restored (run 'npm install' or 'bun install' to reinstall deps)"
200
+ else
201
+ warn " No npm manifests in backup"
202
+ fi ;;
203
+ *) warn "Unknown manifest entry type: $kind" ;;
204
+ esac
205
+ done < "$MANIFEST_FILE"
206
+ }
207
+
120
208
  # ─── Main ────────────────────────────────────────────────────────
121
209
  main() {
122
210
  log "════════════════════════════════════════════════════════════"
@@ -124,95 +212,7 @@ main() {
124
212
  log "════════════════════════════════════════════════════════════"
125
213
  echo
126
214
 
127
- # ── 1. Core Config Files ──────────────────────────────────────
128
- log "1/8 Core configuration files..."
129
- restore_file "$BACKUP_DIR/settings.json" "$PI_ROOT/agent/settings.json"
130
- restore_file "$BACKUP_DIR/auth.json" "$PI_ROOT/agent/auth.json"
131
- restore_file "$BACKUP_DIR/models.json" "$PI_ROOT/agent/models.json"
132
- restore_file "$BACKUP_DIR/trust.json" "$PI_ROOT/agent/trust.json"
133
- restore_file "$BACKUP_DIR/keybindings.json" "$PI_ROOT/agent/keybindings.json"
134
- restore_file "$BACKUP_DIR/pix.json" "$PI_ROOT/agent/pix.json"
135
- restore_file "$BACKUP_DIR/optimizer.json" "$PI_ROOT/agent/optimizer.json"
136
- restore_file "$BACKUP_DIR/lmstudio.json.bak" "$PI_ROOT/agent/lmstudio.json.bak"
137
- restore_file "$BACKUP_DIR/hermes-memory-config.json" "$PI_ROOT/agent/hermes-memory-config.json"
138
- restore_file "$BACKUP_DIR/pi.code-workspace" "$PI_ROOT/agent/pi.code-workspace"
139
-
140
- # ── 2. Extension Configs ──────────────────────────────────────
141
- log "2/8 Extension configurations..."
142
- restore_file "$BACKUP_DIR/pi-hermes-memory-cleanup/scoped-memory.json" \
143
- "$PI_ROOT/agent/pi-hermes-memory-cleanup/scoped-memory.json"
144
- restore_file "$BACKUP_DIR/loop-police.json" "$PI_ROOT/agent/loop-police.json"
145
- restore_file "$BACKUP_DIR/pi-rtk/config.json" "$PI_ROOT/agent/pi-rtk/config.json"
146
- restore_file "$BACKUP_DIR/context-mode/config.json" "$PI_ROOT/agent/context-mode/config.json"
147
- restore_file "$BACKUP_DIR/pi-timeout/config.json" "$PI_ROOT/agent/pi-timeout/config.json"
148
- restore_file "$BACKUP_DIR/pi-context-viewer/config.json" "$PI_ROOT/agent/pi-context-viewer/config.json"
149
- restore_file "$BACKUP_DIR/pix-optimizer/config.json" "$PI_ROOT/agent/pix-optimizer/config.json"
150
- restore_file "$BACKUP_DIR/pi-nvidia-nim/config.json" "$PI_ROOT/agent/pi-nvidia-nim/config.json"
151
-
152
- # ── 3. Global Skills ──────────────────────────────────────────
153
- log "3/8 Global skills..."
154
- restore_dir "$BACKUP_DIR/skills" "$PI_ROOT/agent/skills"
155
-
156
- # ── 4. Project Skills & Memory ────────────────────────────────
157
- log "4/8 Project skills & memory..."
158
- if [[ -d "$BACKUP_DIR/projects-memory" ]]; then
159
- for proj in "$BACKUP_DIR/projects-memory"/*/; do
160
- [[ -d "$proj" ]] || continue
161
- name="$(basename "$proj")"
162
- restore_dir "$proj" "$PI_ROOT/agent/projects-memory/$name"
163
- done
164
- ok " projects-memory/ restored"
165
- else
166
- warn " No project memory in backup"
167
- fi
168
-
169
- # ── 5. Local Extensions ───────────────────────────────────────
170
- log "5/8 Local extension source code..."
171
- if [[ -d "$BACKUP_DIR/local-extensions" ]]; then
172
- for ext in "$BACKUP_DIR/local-extensions"/*/; do
173
- [[ -d "$ext" ]] || continue
174
- name="$(basename "$ext")"
175
- mkdir -p "$LOCAL_EXT_ROOT/$name"
176
- rsync -a "$ext/" "$LOCAL_EXT_ROOT/$name/"
177
- ok " $name/ restored"
178
- done
179
- else
180
- warn " No local extensions in backup"
181
- fi
182
-
183
- # ── 6. npm Extension Manifests ────────────────────────────────
184
- log "6/8 npm extension manifests..."
185
- if [[ -d "$BACKUP_DIR/npm-manifests" ]]; then
186
- mkdir -p "$PI_ROOT/agent/npm"
187
- cp -a "$BACKUP_DIR/npm-manifests/"* "$PI_ROOT/agent/npm/" 2>/dev/null || true
188
- ok " npm manifests restored (run 'npm install' or 'bun install' to reinstall deps)"
189
- else
190
- warn " No npm manifests in backup"
191
- fi
192
-
193
- # ── 7. Hermes Memory ──────────────────────────────────────────
194
- log "7/8 Hermes memory (active files)..."
195
- if [[ -d "$BACKUP_DIR/pi-hermes-memory" ]]; then
196
- mkdir -p "$PI_ROOT/agent/pi-hermes-memory"
197
- for f in MEMORY.md USER.md failures.md retired-failures.md retired-memory.md; do
198
- restore_file "$BACKUP_DIR/pi-hermes-memory/$f" "$PI_ROOT/agent/pi-hermes-memory/$f"
199
- done
200
- else
201
- warn " No Hermes memory in backup"
202
- fi
203
-
204
- # ── 8. Cleanup Backups ────────────────────────────────────────
205
- log "8/8 Hermes-cleanup backups..."
206
- if [[ -d "$BACKUP_DIR/hermes-cleanup" ]]; then
207
- mkdir -p "$PI_ROOT/agent/hermes-cleanup"
208
- for d in "$BACKUP_DIR/hermes-cleanup"/*/; do
209
- [[ -d "$d" ]] || continue
210
- name="$(basename "$d")"
211
- restore_dir "$d" "$PI_ROOT/agent/hermes-cleanup/$name"
212
- done
213
- else
214
- warn " No cleanup backups in backup"
215
- fi
215
+ restore_manifest_entries
216
216
 
217
217
  # ── Verify ────────────────────────────────────────────────────
218
218
  echo
@@ -231,10 +231,4 @@ main() {
231
231
  echo
232
232
  }
233
233
 
234
- main "$@"
235
-
236
- # ─── Cleanup ────────────────────────────────────────────────────
237
- if [[ -n "${STAGE:-}" && -d "$STAGE" ]]; then
238
- rm -rf "$STAGE"
239
- log "Removed temp extraction dir."
240
- fi
234
+ main "$@"