@jamiefutch/pi-backup 1.1.10 → 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/backup-pi.sh CHANGED
@@ -5,32 +5,58 @@
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
- BACKUP_ROOT="${BACKUP_ROOT:-$HOME/projects/personal/pi-utils/backups}"
13
+ BACKUP_ROOT="${BACKUP_ROOT:-$HOME/pi-backups}"
14
+ LOCAL_EXT_ROOT="${LOCAL_EXT_ROOT:-$HOME/projects}"
11
15
  TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
12
16
  BACKUP_DIR="$BACKUP_ROOT/pi-backup-$TIMESTAMP" # staging dir (compressed into archive below)
13
17
  KEEP_UNCOMPRESSED="${KEEP_UNCOMPRESSED:-0}" # set to 1 to keep the staging dir after archiving
14
18
 
15
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.
16
22
  # Prefer 7zip (best ratio) if present, fall back to zip.
17
23
  ZIP_EXT="zip"
18
24
  ZIP_TOOL=""
19
- if command -v 7zz >/dev/null 2>&1; then
20
- ZIP_TOOL="7zz"
21
- ZIP_EXT="7z"
22
- elif command -v 7z >/dev/null 2>&1; then
23
- ZIP_TOOL="7z"
24
- ZIP_EXT="7z"
25
- elif command -v 7za >/dev/null 2>&1; then
26
- ZIP_TOOL="7za"
27
- ZIP_EXT="7z"
28
- elif command -v zip >/dev/null 2>&1; then
29
- ZIP_TOOL="zip"
30
- 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"
31
42
  fi
32
- [[ -n "$ZIP_TOOL" ]] || { echo "[ERR] Neither 7zip nor zip found. Install one for compressed backups." >&2; exit 1; }
33
- 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
34
60
 
35
61
  # ─── Colors ──────────────────────────────────────────────────────
36
62
  RED='\033[0;31m'
@@ -44,15 +70,22 @@ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
44
70
  warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
45
71
  err() { echo -e "${RED}[ERR]${NC} $*"; }
46
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
+
47
78
  # ─── Helpers ─────────────────────────────────────────────────────
79
+ strip_cr() { printf '%s' "${1%$'\r'}"; }
80
+
48
81
  copy_if_exists() {
49
82
  local src="$1" dst="$2"
50
83
  if [[ -e "$src" ]]; then
51
84
  mkdir -p "$(dirname "$dst")"
52
85
  cp -a "$src" "$dst"
53
- ok " $(basename "$src")"
86
+ ok " ${dst#"$BACKUP_DIR"/}"
54
87
  else
55
- warn " $(basename "$src") — not found, skipping"
88
+ warn " ${dst#"$BACKUP_DIR"/} — not found, skipping"
56
89
  fi
57
90
  }
58
91
 
@@ -67,6 +100,91 @@ copy_dir_if_exists() {
67
100
  fi
68
101
  }
69
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
+
70
188
  # ─── Main ────────────────────────────────────────────────────────
71
189
  main() {
72
190
  echo
@@ -78,109 +196,9 @@ main() {
78
196
  echo
79
197
 
80
198
  mkdir -p "$BACKUP_DIR"
199
+ STAGING_CREATED=1
81
200
 
82
- # ── 1. Core Config Files ──────────────────────────────────────
83
- log "1/8 Core configuration files..."
84
- copy_if_exists "$PI_ROOT/agent/settings.json" "$BACKUP_DIR/settings.json"
85
- copy_if_exists "$PI_ROOT/agent/auth.json" "$BACKUP_DIR/auth.json"
86
- copy_if_exists "$PI_ROOT/agent/models.json" "$BACKUP_DIR/models.json"
87
- copy_if_exists "$PI_ROOT/agent/trust.json" "$BACKUP_DIR/trust.json"
88
- copy_if_exists "$PI_ROOT/agent/keybindings.json" "$BACKUP_DIR/keybindings.json"
89
- copy_if_exists "$PI_ROOT/agent/pix.json" "$BACKUP_DIR/pix.json"
90
- copy_if_exists "$PI_ROOT/agent/optimizer.json" "$BACKUP_DIR/optimizer.json"
91
- copy_if_exists "$PI_ROOT/agent/lmstudio.json.bak" "$BACKUP_DIR/lmstudio.json.bak"
92
- copy_if_exists "$PI_ROOT/agent/hermes-memory-config.json" "$BACKUP_DIR/hermes-memory-config.json"
93
- copy_if_exists "$PI_ROOT/agent/pi.code-workspace" "$BACKUP_DIR/pi.code-workspace"
94
-
95
- # ── 2. Extension Configs ──────────────────────────────────────
96
- log "2/8 Extension configurations..."
97
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory-cleanup/scoped-memory.json" \
98
- "$BACKUP_DIR/pi-hermes-memory-cleanup/scoped-memory.json"
99
- copy_if_exists "$PI_ROOT/agent/loop-police.json" "$BACKUP_DIR/loop-police.json"
100
- copy_if_exists "$PI_ROOT/agent/pi-rtk/config.json" "$BACKUP_DIR/pi-rtk/config.json"
101
- copy_if_exists "$PI_ROOT/agent/context-mode/config.json" "$BACKUP_DIR/context-mode/config.json"
102
- copy_if_exists "$PI_ROOT/agent/pi-timeout/config.json" "$BACKUP_DIR/pi-timeout/config.json"
103
- copy_if_exists "$PI_ROOT/agent/pi-context-viewer/config.json" "$BACKUP_DIR/pi-context-viewer/config.json"
104
- copy_if_exists "$PI_ROOT/agent/pix-optimizer/config.json" "$BACKUP_DIR/pix-optimizer/config.json"
105
- copy_if_exists "$PI_ROOT/agent/pi-nvidia-nim/config.json" "$BACKUP_DIR/pi-nvidia-nim/config.json"
106
-
107
- # ── 3. Global Skills ──────────────────────────────────────────
108
- log "3/8 Global skills..."
109
- copy_dir_if_exists "$PI_ROOT/agent/skills" "$BACKUP_DIR/skills"
110
-
111
- # ── 4. Project Skills & Memory ────────────────────────────────
112
- log "4/8 Project skills & memory..."
113
- if [[ -d "$PI_ROOT/agent/projects-memory" ]]; then
114
- mkdir -p "$BACKUP_DIR/projects-memory"
115
- for proj in "$PI_ROOT/agent/projects-memory"/*/; do
116
- [[ -d "$proj" ]] || continue
117
- name="$(basename "$proj")"
118
- copy_dir_if_exists "$proj" "$BACKUP_DIR/projects-memory/$name"
119
- done
120
- ok " projects-memory/ ($(ls -1 "$BACKUP_DIR/projects-memory" 2>/dev/null | wc -l) projects)"
121
- else
122
- warn " projects-memory/ — not found"
123
- fi
124
-
125
- # ── 5. Local Extensions (source code only, no node_modules) ────
126
- log "5/8 Local extension source code..."
127
- local_exts=(
128
- "pi-timeout"
129
- "pi-chunk"
130
- "pi-hermes-memory-cleanup"
131
- "pi-context-docs"
132
- )
133
- for ext in "${local_exts[@]}"; do
134
- if [[ -d "$HOME/projects/personal/$ext" ]]; then
135
- mkdir -p "$BACKUP_DIR/local-extensions/$ext"
136
- # Copy source, exclude node_modules, dist, build, .git, coverage
137
- rsync -a --exclude 'node_modules' --exclude 'dist' --exclude 'build' --exclude '.git' --exclude 'coverage' --exclude '*.log' \
138
- "$HOME/projects/personal/$ext/" "$BACKUP_DIR/local-extensions/$ext/"
139
- ok " $ext/ (source only)"
140
- else
141
- warn " $ext — not found at ~/projects/personal/$ext"
142
- fi
143
- done
144
-
145
- # ── 6. Installed npm Extensions (package.json + lock) ─────────
146
- log "6/8 npm extension manifests..."
147
- if [[ -d "$PI_ROOT/agent/npm" ]]; then
148
- # Only copy package.json and lockfiles, not node_modules
149
- 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
150
- rel="${f#$PI_ROOT/agent/npm/}"
151
- mkdir -p "$BACKUP_DIR/npm-manifests/$(dirname "$rel")"
152
- cp "$f" "$BACKUP_DIR/npm-manifests/$rel"
153
- done
154
- ok " npm manifests (package.json, lockfiles)"
155
- else
156
- warn " npm/ — not found"
157
- fi
158
-
159
- # ── 7. Hermes Memory (active files only, not sessions DB) ─────
160
- log "7/8 Hermes memory (active files)..."
161
- if [[ -d "$PI_ROOT/agent/pi-hermes-memory" ]]; then
162
- mkdir -p "$BACKUP_DIR/pi-hermes-memory"
163
- for f in MEMORY.md USER.md failures.md; do
164
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory/$f" "$BACKUP_DIR/pi-hermes-memory/$f"
165
- done
166
- # Also backup retired/archived if they exist
167
- for f in retired-failures.md retired-memory.md; do
168
- copy_if_exists "$PI_ROOT/agent/pi-hermes-memory/$f" "$BACKUP_DIR/pi-hermes-memory/$f"
169
- done
170
- else
171
- warn " pi-hermes-memory/ — not found"
172
- fi
173
-
174
- # ── 8. Cleanup Backups (recent) ───────────────────────────────
175
- log "8/8 Recent hermes-cleanup backups (last 5)..."
176
- if [[ -d "$PI_ROOT/agent/hermes-cleanup" ]]; then
177
- mkdir -p "$BACKUP_DIR/hermes-cleanup"
178
- find "$PI_ROOT/agent/hermes-cleanup" -maxdepth 1 -type d -name "20*" | sort -r | head -5 | while read -r d; do
179
- copy_dir_if_exists "$d" "$BACKUP_DIR/hermes-cleanup/$(basename "$d")"
180
- done
181
- else
182
- warn " hermes-cleanup/ — not found"
183
- fi
201
+ backup_manifest_entries
184
202
 
185
203
  # ── Manifest ──────────────────────────────────────────────────
186
204
  log "Creating manifest..."
@@ -197,20 +215,26 @@ Contents:
197
215
  EOF
198
216
  find "$BACKUP_DIR" -type f | sed "s|$BACKUP_DIR/||" | sort >> "$BACKUP_DIR/MANIFEST.txt"
199
217
 
200
- # ── 9. Compress ───────────────────────────────────────────────
201
- log "9/9 Compressing backup ($ZIP_TOOL)..."
202
- local name="$(basename "$BACKUP_DIR")"
203
- if [[ "$ZIP_TOOL" == "zip" ]]; then
204
- (cd "$BACKUP_ROOT" && zip -rq "$(basename "$ARCHIVE")" "$name")
205
- else
206
- (cd "$BACKUP_ROOT" && "$ZIP_TOOL" a -t7z -mx=9 "$(basename "$ARCHIVE")" "$name" >/dev/null)
207
- fi
208
- ok " $ARCHIVE"
209
-
210
- # ── 10. Clean up staging dir (unless told to keep it) ─────────
218
+ # ── 9. Compress (external backups only) ───────────────────────
211
219
  if [[ "$KEEP_UNCOMPRESSED" == "1" ]]; then
212
- log "Keeping uncompressed staging dir: $BACKUP_DIR"
220
+ log "9/9 Skipping compression for internal backup"
213
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 ────────────────────────────────
214
238
  rm -rf "$BACKUP_DIR"
215
239
  fi
216
240
 
@@ -218,12 +242,19 @@ EOF
218
242
  echo
219
243
  log "════════════════════════════════════════════════════════════"
220
244
  ok "Backup complete!"
221
- log "Archive: $ARCHIVE"
222
- log "Size: $(du -h "$ARCHIVE" | cut -f1)"
223
- [[ "$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
224
255
  echo
225
256
  log "To restore after reinstall:"
226
- log " ~/projects/personal/pi-backup/restore-pi.sh $ARCHIVE"
257
+ log " ./restore-pi.sh $result"
227
258
  echo
228
259
  }
229
260