@jamiefutch/pi-backup 1.1.11 → 1.1.13

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/backup-pi.sh CHANGED
@@ -5,6 +5,9 @@
5
5
 
6
6
  set -euo pipefail
7
7
 
8
+ # Backups contain credentials (auth.json): keep everything we create private to this user.
9
+ umask 077
10
+
8
11
  # ─── Config ──────────────────────────────────────────────────────
9
12
  PI_ROOT="${PI_ROOT:-$HOME/.pi}"
10
13
  BACKUP_ROOT="${BACKUP_ROOT:-$HOME/pi-backups}"
@@ -14,24 +17,46 @@ BACKUP_DIR="$BACKUP_ROOT/pi-backup-$TIMESTAMP" # staging dir (compressed into
14
17
  KEEP_UNCOMPRESSED="${KEEP_UNCOMPRESSED:-0}" # set to 1 to keep the staging dir after archiving
15
18
 
16
19
  # ─── Compression tool detection ────────────────────────────────
20
+ # Only needed for compressed (external) backups. Internal backups
21
+ # (KEEP_UNCOMPRESSED=1) are plain staging directories and need no tool.
17
22
  # Prefer 7zip (best ratio) if present, fall back to zip.
18
23
  ZIP_EXT="zip"
19
24
  ZIP_TOOL=""
20
- if command -v 7zz >/dev/null 2>&1; then
21
- ZIP_TOOL="7zz"
22
- ZIP_EXT="7z"
23
- elif command -v 7z >/dev/null 2>&1; then
24
- ZIP_TOOL="7z"
25
- ZIP_EXT="7z"
26
- elif command -v 7za >/dev/null 2>&1; then
27
- ZIP_TOOL="7za"
28
- ZIP_EXT="7z"
29
- elif command -v zip >/dev/null 2>&1; then
30
- ZIP_TOOL="zip"
31
- ZIP_EXT="zip"
25
+ ARCHIVE=""
26
+ if [[ "$KEEP_UNCOMPRESSED" != "1" ]]; then
27
+ if command -v 7zz >/dev/null 2>&1; then
28
+ ZIP_TOOL="7zz"
29
+ ZIP_EXT="7z"
30
+ elif command -v 7z >/dev/null 2>&1; then
31
+ ZIP_TOOL="7z"
32
+ ZIP_EXT="7z"
33
+ elif command -v 7za >/dev/null 2>&1; then
34
+ ZIP_TOOL="7za"
35
+ ZIP_EXT="7z"
36
+ elif command -v zip >/dev/null 2>&1; then
37
+ ZIP_TOOL="zip"
38
+ ZIP_EXT="zip"
39
+ fi
40
+ [[ -n "$ZIP_TOOL" ]] || { echo "[ERR] Neither 7zip nor zip found. Install one for compressed backups." >&2; exit 1; }
41
+ ARCHIVE="$BACKUP_ROOT/pi-backup-$TIMESTAMP.$ZIP_EXT"
32
42
  fi
33
- [[ -n "$ZIP_TOOL" ]] || { echo "[ERR] Neither 7zip nor zip found. Install one for compressed backups." >&2; exit 1; }
34
- ARCHIVE="$BACKUP_ROOT/pi-backup-$TIMESTAMP.$ZIP_EXT"
43
+
44
+ # ─── Cleanup on failure ───────────────────────────────────────
45
+ # A half-built staging dir (or partial archive) is removed if the script fails.
46
+ # A successful run, or a failed compression (staging is intact), keeps it.
47
+ STAGING_CREATED=0
48
+ KEEP_STAGING_ON_FAIL=0
49
+ cleanup() {
50
+ local rc=$?
51
+ if [[ $rc -ne 0 ]]; then
52
+ if [[ "$STAGING_CREATED" == "1" && "$KEEP_STAGING_ON_FAIL" != "1" ]]; then
53
+ rm -rf "$BACKUP_DIR"
54
+ fi
55
+ [[ -n "$ARCHIVE" ]] && rm -f "$ARCHIVE"
56
+ fi
57
+ return $rc
58
+ }
59
+ trap cleanup EXIT
35
60
 
36
61
  # ─── Colors ──────────────────────────────────────────────────────
37
62
  RED='\033[0;31m'
@@ -45,15 +70,22 @@ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
45
70
  warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
46
71
  err() { echo -e "${RED}[ERR]${NC} $*"; }
47
72
 
73
+ # ─── Manifest ────────────────────────────────────────────────────
74
+ # What gets backed up is defined once, in backup-manifest.txt (shared with restore).
75
+ MANIFEST_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/backup-manifest.txt"
76
+ [[ -f "$MANIFEST_FILE" ]] || { echo "[ERR] backup-manifest.txt not found next to $0" >&2; exit 1; }
77
+
48
78
  # ─── Helpers ─────────────────────────────────────────────────────
79
+ strip_cr() { printf '%s' "${1%$'\r'}"; }
80
+
49
81
  copy_if_exists() {
50
82
  local src="$1" dst="$2"
51
83
  if [[ -e "$src" ]]; then
52
84
  mkdir -p "$(dirname "$dst")"
53
85
  cp -a "$src" "$dst"
54
- ok " $(basename "$src")"
86
+ ok " ${dst#"$BACKUP_DIR"/}"
55
87
  else
56
- warn " $(basename "$src") — not found, skipping"
88
+ warn " ${dst#"$BACKUP_DIR"/} — not found, skipping"
57
89
  fi
58
90
  }
59
91
 
@@ -68,6 +100,91 @@ copy_dir_if_exists() {
68
100
  fi
69
101
  }
70
102
 
103
+ # Portable tree copy with exclusions (no rsync needed): tar pipe.
104
+ copy_source_tree() {
105
+ local src="$1" dst="$2"
106
+ mkdir -p "$dst"
107
+ (cd "$src" && tar -cf - --exclude=node_modules --exclude=dist --exclude=build --exclude=.git \
108
+ --exclude=coverage --exclude='*.log' .) | (cd "$dst" && tar -xf -)
109
+ }
110
+
111
+ # auth.json holds credentials: optionally excluded, always private, always flagged.
112
+ backup_auth() {
113
+ local src="$1" dst="$2"
114
+ if [[ "${PI_BACKUP_EXCLUDE_AUTH:-0}" == "1" ]]; then
115
+ warn " auth.json — excluded (PI_BACKUP_EXCLUDE_AUTH=1)"
116
+ return 0
117
+ fi
118
+ copy_if_exists "$src" "$dst"
119
+ if [[ -f "$dst" ]]; then
120
+ chmod 600 "$dst"
121
+ warn " auth.json holds your API credentials and is included in this backup."
122
+ warn " Keep the backup private; set PI_BACKUP_EXCLUDE_AUTH=1 to leave it out."
123
+ fi
124
+ }
125
+
126
+ backup_manifest_entries() {
127
+ local total step=0 kind f1 f2 f3
128
+ total="$(grep -c '^section|' "$MANIFEST_FILE" || true)"
129
+ while IFS='|' read -r kind f1 f2 f3 || [[ -n "$kind" ]]; do
130
+ kind="$(strip_cr "$kind")"; f1="$(strip_cr "${f1:-}")"; f2="$(strip_cr "${f2:-}")"; f3="$(strip_cr "${f3:-}")"
131
+ case "$kind" in
132
+ ''|'#'*) continue ;;
133
+ section)
134
+ step=$((step+1)); log "$step/$total $f1..." ;;
135
+ file)
136
+ if [[ "$f2" == "auth.json" ]]; then backup_auth "$PI_ROOT/$f1" "$BACKUP_DIR/$f2"
137
+ else copy_if_exists "$PI_ROOT/$f1" "$BACKUP_DIR/$f2"; fi ;;
138
+ dir)
139
+ copy_dir_if_exists "$PI_ROOT/$f1" "$BACKUP_DIR/$f2" ;;
140
+ dir-each)
141
+ if [[ -d "$PI_ROOT/$f1" ]]; then
142
+ mkdir -p "$BACKUP_DIR/$f2"
143
+ local sub name
144
+ for sub in "$PI_ROOT/$f1"/*/; do
145
+ [[ -d "$sub" ]] || continue
146
+ name="$(basename "$sub")"
147
+ copy_dir_if_exists "$sub" "$BACKUP_DIR/$f2/$name"
148
+ done
149
+ ok " $f2/ ($(ls -1 "$BACKUP_DIR/$f2" 2>/dev/null | wc -l | tr -d ' ') projects)"
150
+ else
151
+ warn " $f2/ — not found"
152
+ fi ;;
153
+ local-ext)
154
+ if [[ -d "$LOCAL_EXT_ROOT/$f1" ]]; then
155
+ copy_source_tree "$LOCAL_EXT_ROOT/$f1" "$BACKUP_DIR/local-extensions/$f1"
156
+ ok " $f1/ (source only)"
157
+ else
158
+ warn " $f1 — not found at $LOCAL_EXT_ROOT/$f1"
159
+ fi ;;
160
+ npm)
161
+ if [[ -d "$PI_ROOT/$f1" ]]; then
162
+ # Only package.json and lockfiles, never node_modules
163
+ local f rel
164
+ while IFS= read -r f; do
165
+ rel="${f#$PI_ROOT/$f1/}"
166
+ mkdir -p "$BACKUP_DIR/$f2/$(dirname "$rel")"
167
+ cp "$f" "$BACKUP_DIR/$f2/$rel"
168
+ done < <(find "$PI_ROOT/$f1" -maxdepth 3 -type f -not -path '*/node_modules/*' \( -name "package.json" -o -name "package-lock.json" -o -name "npm-shrinkwrap.json" -o -name "pnpm-lock.yaml" \))
169
+ ok " npm manifests (package.json, lockfiles)"
170
+ else
171
+ warn " $(basename "$f1")/ — not found"
172
+ fi ;;
173
+ recent)
174
+ if [[ -d "$PI_ROOT/$f1" ]]; then
175
+ mkdir -p "$BACKUP_DIR/$f2"
176
+ local d
177
+ while IFS= read -r d; do
178
+ copy_dir_if_exists "$d" "$BACKUP_DIR/$f2/$(basename "$d")"
179
+ done < <(find "$PI_ROOT/$f1" -maxdepth 1 -type d -name "20*" | sort -r | head -n "${f3:-5}")
180
+ else
181
+ warn " $(basename "$f1")/ — not found"
182
+ fi ;;
183
+ *) warn "Unknown manifest entry type: $kind" ;;
184
+ esac
185
+ done < "$MANIFEST_FILE"
186
+ }
187
+
71
188
  # ─── Main ────────────────────────────────────────────────────────
72
189
  main() {
73
190
  echo
@@ -79,109 +196,9 @@ main() {
79
196
  echo
80
197
 
81
198
  mkdir -p "$BACKUP_DIR"
199
+ STAGING_CREATED=1
82
200
 
83
- # ── 1. Core Config Files ──────────────────────────────────────
84
- log "1/8 Core configuration files..."
85
- copy_if_exists "$PI_ROOT/agent/settings.json" "$BACKUP_DIR/settings.json"
86
- copy_if_exists "$PI_ROOT/agent/auth.json" "$BACKUP_DIR/auth.json"
87
- copy_if_exists "$PI_ROOT/agent/models.json" "$BACKUP_DIR/models.json"
88
- copy_if_exists "$PI_ROOT/agent/trust.json" "$BACKUP_DIR/trust.json"
89
- copy_if_exists "$PI_ROOT/agent/keybindings.json" "$BACKUP_DIR/keybindings.json"
90
- copy_if_exists "$PI_ROOT/agent/pix.json" "$BACKUP_DIR/pix.json"
91
- copy_if_exists "$PI_ROOT/agent/optimizer.json" "$BACKUP_DIR/optimizer.json"
92
- copy_if_exists "$PI_ROOT/agent/lmstudio.json.bak" "$BACKUP_DIR/lmstudio.json.bak"
93
- copy_if_exists "$PI_ROOT/agent/hermes-memory-config.json" "$BACKUP_DIR/hermes-memory-config.json"
94
- copy_if_exists "$PI_ROOT/agent/pi.code-workspace" "$BACKUP_DIR/pi.code-workspace"
95
-
96
- # ── 2. Extension Configs ──────────────────────────────────────
97
- log "2/8 Extension configurations..."
98
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory-cleanup/scoped-memory.json" \
99
- "$BACKUP_DIR/pi-hermes-memory-cleanup/scoped-memory.json"
100
- copy_if_exists "$PI_ROOT/agent/loop-police.json" "$BACKUP_DIR/loop-police.json"
101
- copy_if_exists "$PI_ROOT/agent/pi-rtk/config.json" "$BACKUP_DIR/pi-rtk/config.json"
102
- copy_if_exists "$PI_ROOT/agent/context-mode/config.json" "$BACKUP_DIR/context-mode/config.json"
103
- copy_if_exists "$PI_ROOT/agent/pi-timeout/config.json" "$BACKUP_DIR/pi-timeout/config.json"
104
- copy_if_exists "$PI_ROOT/agent/pi-context-viewer/config.json" "$BACKUP_DIR/pi-context-viewer/config.json"
105
- copy_if_exists "$PI_ROOT/agent/pix-optimizer/config.json" "$BACKUP_DIR/pix-optimizer/config.json"
106
- copy_if_exists "$PI_ROOT/agent/pi-nvidia-nim/config.json" "$BACKUP_DIR/pi-nvidia-nim/config.json"
107
-
108
- # ── 3. Global Skills ──────────────────────────────────────────
109
- log "3/8 Global skills..."
110
- copy_dir_if_exists "$PI_ROOT/agent/skills" "$BACKUP_DIR/skills"
111
-
112
- # ── 4. Project Skills & Memory ────────────────────────────────
113
- log "4/8 Project skills & memory..."
114
- if [[ -d "$PI_ROOT/agent/projects-memory" ]]; then
115
- mkdir -p "$BACKUP_DIR/projects-memory"
116
- for proj in "$PI_ROOT/agent/projects-memory"/*/; do
117
- [[ -d "$proj" ]] || continue
118
- name="$(basename "$proj")"
119
- copy_dir_if_exists "$proj" "$BACKUP_DIR/projects-memory/$name"
120
- done
121
- ok " projects-memory/ ($(ls -1 "$BACKUP_DIR/projects-memory" 2>/dev/null | wc -l) projects)"
122
- else
123
- warn " projects-memory/ — not found"
124
- fi
125
-
126
- # ── 5. Local Extensions (source code only, no node_modules) ────
127
- log "5/8 Local extension source code..."
128
- local_exts=(
129
- "pi-timeout"
130
- "pi-chunk"
131
- "pi-hermes-memory-cleanup"
132
- "pi-context-docs"
133
- )
134
- for ext in "${local_exts[@]}"; do
135
- if [[ -d "$LOCAL_EXT_ROOT/$ext" ]]; then
136
- mkdir -p "$BACKUP_DIR/local-extensions/$ext"
137
- # Copy source, exclude node_modules, dist, build, .git, coverage
138
- rsync -a --exclude 'node_modules' --exclude 'dist' --exclude 'build' --exclude '.git' --exclude 'coverage' --exclude '*.log' \
139
- "$LOCAL_EXT_ROOT/$ext/" "$BACKUP_DIR/local-extensions/$ext/"
140
- ok " $ext/ (source only)"
141
- else
142
- warn " $ext — not found at $LOCAL_EXT_ROOT/$ext"
143
- fi
144
- done
145
-
146
- # ── 6. Installed npm Extensions (package.json + lock) ─────────
147
- log "6/8 npm extension manifests..."
148
- if [[ -d "$PI_ROOT/agent/npm" ]]; then
149
- # Only copy package.json and lockfiles, not node_modules
150
- find "$PI_ROOT/agent/npm" -maxdepth 3 -type f \( -name "package.json" -o -name "package-lock.json" -o -name "npm-shrinkwrap.json" -o -name "pnpm-lock.yaml" \) | while read -r f; do
151
- rel="${f#$PI_ROOT/agent/npm/}"
152
- mkdir -p "$BACKUP_DIR/npm-manifests/$(dirname "$rel")"
153
- cp "$f" "$BACKUP_DIR/npm-manifests/$rel"
154
- done
155
- ok " npm manifests (package.json, lockfiles)"
156
- else
157
- warn " npm/ — not found"
158
- fi
159
-
160
- # ── 7. Hermes Memory (active files only, not sessions DB) ─────
161
- log "7/8 Hermes memory (active files)..."
162
- if [[ -d "$PI_ROOT/agent/pi-hermes-memory" ]]; then
163
- mkdir -p "$BACKUP_DIR/pi-hermes-memory"
164
- for f in MEMORY.md USER.md failures.md; do
165
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory/$f" "$BACKUP_DIR/pi-hermes-memory/$f"
166
- done
167
- # Also backup retired/archived if they exist
168
- for f in retired-failures.md retired-memory.md; do
169
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory/$f" "$BACKUP_DIR/pi-hermes-memory/$f"
170
- done
171
- else
172
- warn " pi-hermes-memory/ — not found"
173
- fi
174
-
175
- # ── 8. Cleanup Backups (recent) ───────────────────────────────
176
- log "8/8 Recent hermes-cleanup backups (last 5)..."
177
- if [[ -d "$PI_ROOT/agent/hermes-cleanup" ]]; then
178
- mkdir -p "$BACKUP_DIR/hermes-cleanup"
179
- find "$PI_ROOT/agent/hermes-cleanup" -maxdepth 1 -type d -name "20*" | sort -r | head -5 | while read -r d; do
180
- copy_dir_if_exists "$d" "$BACKUP_DIR/hermes-cleanup/$(basename "$d")"
181
- done
182
- else
183
- warn " hermes-cleanup/ — not found"
184
- fi
201
+ backup_manifest_entries
185
202
 
186
203
  # ── Manifest ──────────────────────────────────────────────────
187
204
  log "Creating manifest..."
@@ -198,20 +215,26 @@ Contents:
198
215
  EOF
199
216
  find "$BACKUP_DIR" -type f | sed "s|$BACKUP_DIR/||" | sort >> "$BACKUP_DIR/MANIFEST.txt"
200
217
 
201
- # ── 9. Compress ───────────────────────────────────────────────
202
- log "9/9 Compressing backup ($ZIP_TOOL)..."
203
- local name="$(basename "$BACKUP_DIR")"
204
- if [[ "$ZIP_TOOL" == "zip" ]]; then
205
- (cd "$BACKUP_ROOT" && zip -rq "$(basename "$ARCHIVE")" "$name")
206
- else
207
- (cd "$BACKUP_ROOT" && "$ZIP_TOOL" a -t7z -mx=9 "$(basename "$ARCHIVE")" "$name" >/dev/null)
208
- fi
209
- ok " $ARCHIVE"
210
-
211
- # ── 10. Clean up staging dir (unless told to keep it) ─────────
218
+ # ── 9. Compress (external backups only) ───────────────────────
212
219
  if [[ "$KEEP_UNCOMPRESSED" == "1" ]]; then
213
- log "Keeping uncompressed staging dir: $BACKUP_DIR"
220
+ log "9/9 Skipping compression for internal backup"
214
221
  else
222
+ log "9/9 Compressing backup ($ZIP_TOOL)..."
223
+ local name comp_out comp_rc=0
224
+ name="$(basename "$BACKUP_DIR")"
225
+ if [[ "$ZIP_TOOL" == "zip" ]]; then
226
+ comp_out="$(cd "$BACKUP_ROOT" && zip -rq "$(basename "$ARCHIVE")" "$name" 2>&1)" || comp_rc=$?
227
+ else
228
+ comp_out="$(cd "$BACKUP_ROOT" && "$ZIP_TOOL" a -t7z -mx=9 "$(basename "$ARCHIVE")" "$name" 2>&1)" || comp_rc=$?
229
+ fi
230
+ if [[ $comp_rc -ne 0 ]]; then
231
+ warn " Compression with $ZIP_TOOL failed (exit $comp_rc): $comp_out"
232
+ warn " Keeping uncompressed staging dir: $BACKUP_DIR"
233
+ KEEP_STAGING_ON_FAIL=1
234
+ exit 1
235
+ fi
236
+ ok " $ARCHIVE"
237
+ # ── 10. Clean up staging dir ────────────────────────────────
215
238
  rm -rf "$BACKUP_DIR"
216
239
  fi
217
240
 
@@ -219,12 +242,19 @@ EOF
219
242
  echo
220
243
  log "════════════════════════════════════════════════════════════"
221
244
  ok "Backup complete!"
222
- log "Archive: $ARCHIVE"
223
- log "Size: $(du -h "$ARCHIVE" | cut -f1)"
224
- [[ "$KEEP_UNCOMPRESSED" == "1" ]] && log "Staging: $BACKUP_DIR"
245
+ local result
246
+ if [[ "$KEEP_UNCOMPRESSED" == "1" ]]; then
247
+ result="$BACKUP_DIR"
248
+ log "Staging: $BACKUP_DIR"
249
+ log "Size: $(du -sh "$BACKUP_DIR" | cut -f1)"
250
+ else
251
+ result="$ARCHIVE"
252
+ log "Archive: $ARCHIVE"
253
+ log "Size: $(du -h "$ARCHIVE" | cut -f1)"
254
+ fi
225
255
  echo
226
256
  log "To restore after reinstall:"
227
- log " ./restore-pi.sh $ARCHIVE"
257
+ log " ./restore-pi.sh $result"
228
258
  echo
229
259
  }
230
260