@jamiefutch/pi-backup 1.1.13 → 1.2.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.0
4
+
5
+ ### Added
6
+ - Backups include the memory database (`pi-hermes-memory/sessions.db`), taken with `sqlite3 .backup` so changes still in the WAL file are kept. Restore clears stale `-wal`/`-shm`/`-journal` files before putting the database back.
7
+ - Backups include memory skills (`pi-hermes-memory/skills/`) and the skill-migration marker.
8
+ - Backups include extension state: `missions/`, `extensions/`, `anti-stall.json`, `crashes.json`, `run-history.jsonl`, `models-store.json`, `pi-subagents/last-seen-version.json`.
9
+ - Opt-in items, each set with an environment variable: `PI_BACKUP_INCLUDE_SESSIONS=1` (session history), `PI_BACKUP_INCLUDE_GIT=1` (cloned git packages), `PI_BACKUP_INCLUDE_BIN=1` (bundled binaries).
10
+ - The manifest supports `sqlite|` and `optional|ENV|` entries, read by the bash and PowerShell scripts.
11
+
3
12
  ## 1.1.13
4
13
 
5
14
  ### Fixed
package/README.md CHANGED
@@ -216,6 +216,11 @@ The list lives in one file, [`backup-manifest.txt`](backup-manifest.txt), read b
216
216
  | **Local extension source** | `$LOCAL_EXT_ROOT/pi-timeout/`, `pi-chunk/`, `pi-hermes-memory-cleanup/`, `pi-context-docs/` (defaults to `~/projects/`; `node_modules`, `dist`, `build`, `.git`, `coverage` and `*.log` are skipped) |
217
217
  | **npm manifests** | `package.json`, `package-lock.json`, `npm-shrinkwrap.json`, `pnpm-lock.yaml` from `~/.pi/agent/npm/` (never `node_modules`) |
218
218
  | **Hermes memory (active)** | `MEMORY.md`, `USER.md`, `failures.md`, `retired-failures.md`, `retired-memory.md` |
219
+ | **Memory database & skills** | `sessions.db` in `~/.pi/agent/pi-hermes-memory/` (copied with `sqlite3 .backup`, so WAL changes are included), `~/.pi/agent/pi-hermes-memory/skills/`, `.skills-migrated-to-extension-storage` |
220
+ | **Extension state** | `pi-subagents/last-seen-version.json`, `anti-stall.json`, `crashes.json`, `run-history.jsonl`, `models-store.json`, `~/.pi/agent/missions/`, `~/.pi/agent/extensions/` |
221
+ | **Optional: sessions** | `~/.pi/agent/sessions/` (151 MB or more). Set `PI_BACKUP_INCLUDE_SESSIONS=1` |
222
+ | **Optional: git packages** | `~/.pi/agent/git/` (cloned git packages). Set `PI_BACKUP_INCLUDE_GIT=1` |
223
+ | **Optional: bin** | `~/.pi/agent/bin/` (bundled binaries such as `fd`). Set `PI_BACKUP_INCLUDE_BIN=1` |
219
224
  | **Cleanup backups** | Last 5 timestamped (`20*`) directories from `~/.pi/agent/hermes-cleanup/` |
220
225
 
221
226
  Example output of `./backup-pi.sh` (captured from a real run, abridged):
@@ -255,7 +260,7 @@ An internal backup (`KEEP_UNCOMPRESSED=1`, what the TUI's **Create internal back
255
260
 
256
261
  | Excluded | Why |
257
262
  |----------|-----|
258
- | `~/.pi/agent/sessions/` | Session history — regenerated |
263
+ | `~/.pi/agent/sessions/` | Session history — regenerated. Include it with `PI_BACKUP_INCLUDE_SESSIONS=1` |
259
264
  | `~/.pi/agent/context-mode/kb.sqlite` | Knowledge base — rebuilt from indexed sources |
260
265
  | `~/.pi/agent/npm/node_modules/` | Dependencies — reinstalled via `npm install` |
261
266
  | `~/.pi/agent/loop-police-detections.jsonl` | Detection logs — regenerated |
@@ -342,12 +347,18 @@ If the default directory already contains backups, it is treated as configured a
342
347
  | `KEEP_UNCOMPRESSED` | `0` | `1` makes a plain, uncompressed staging directory and skips compression (no `zip`/7-Zip needed); the TUI sets this for internal backups. With `0` the staging directory is compressed and removed |
343
348
  | `PI_BACKUP_ASSUME_YES` | unset | Set to `1` to skip the restore confirmation prompt (scripted use). Without it, restore needs an interactive terminal and exits with a clear message otherwise |
344
349
  | `PI_BACKUP_EXCLUDE_AUTH` | unset | Set to `1` to leave `auth.json` (API credentials) out of the backup |
350
+ | `PI_BACKUP_INCLUDE_SESSIONS` | unset | Set to `1` to include `~/.pi/agent/sessions/` (session history; large) |
351
+ | `PI_BACKUP_INCLUDE_GIT` | unset | Set to `1` to include `~/.pi/agent/git/` (cloned git packages) |
352
+ | `PI_BACKUP_INCLUDE_BIN` | unset | Set to `1` to include `~/.pi/agent/bin/` (bundled binaries) |
345
353
  | `KEEP_OLD` | unset | Set to `1` to keep `~/.pi.pre-reinstall-<timestamp>` after a successful clean reinstall |
346
354
  | `PKG_MANAGER` | `bun` | Package manager used by the clean-reinstall scripts (`bun`, `npm`, etc.) |
347
355
 
348
356
  ```bash
349
357
  # Custom locations
350
358
  PI_ROOT=/custom/.pi BACKUP_ROOT=/mnt/backups ./backup-pi.sh
359
+
360
+ # Include session history and cloned git packages
361
+ PI_BACKUP_INCLUDE_SESSIONS=1 PI_BACKUP_INCLUDE_GIT=1 ./backup-pi.sh
351
362
  ```
352
363
 
353
364
  ---
@@ -10,6 +10,8 @@
10
10
  # local-ext|<name> source tree under LOCAL_EXT_ROOT (node_modules, dist, build, .git, coverage, *.log excluded)
11
11
  # npm|<pi path>|<backup path> package.json and lockfiles only (max depth 3), never node_modules
12
12
  # recent|<pi path>|<backup path>|<n> the n newest sub-directories whose name starts with "20" (timestamps)
13
+ # sqlite|<pi path>|<backup path> a SQLite database, copied with sqlite3 .backup so WAL changes are included
14
+ # optional|<ENV_VAR>|<entry> the entry (file, dir, sqlite) is backed up only when ENV_VAR=1
13
15
  #
14
16
  # Keep README.md "What Gets Backed Up" in sync; tests/sh/manifest.test.sh checks it.
15
17
 
@@ -57,5 +59,24 @@ file|agent/pi-hermes-memory/failures.md|pi-hermes-memory/failures.md
57
59
  file|agent/pi-hermes-memory/retired-failures.md|pi-hermes-memory/retired-failures.md
58
60
  file|agent/pi-hermes-memory/retired-memory.md|pi-hermes-memory/retired-memory.md
59
61
 
62
+ section|Memory database and skills
63
+ sqlite|agent/pi-hermes-memory/sessions.db|pi-hermes-memory/sessions.db
64
+ dir|agent/pi-hermes-memory/skills|pi-hermes-memory/skills
65
+ file|agent/pi-hermes-memory/.skills-migrated-to-extension-storage|pi-hermes-memory/.skills-migrated-to-extension-storage
66
+
67
+ section|Extension state
68
+ file|agent/pi-subagents/last-seen-version.json|pi-subagents/last-seen-version.json
69
+ file|agent/anti-stall.json|anti-stall.json
70
+ file|agent/crashes.json|crashes.json
71
+ file|agent/run-history.jsonl|run-history.jsonl
72
+ file|agent/models-store.json|models-store.json
73
+ dir|agent/missions|missions
74
+ dir|agent/extensions|extensions
75
+
76
+ section|Optional (opt-in with environment variables)
77
+ optional|PI_BACKUP_INCLUDE_SESSIONS|dir|agent/sessions|sessions
78
+ optional|PI_BACKUP_INCLUDE_GIT|dir|agent/git|git
79
+ optional|PI_BACKUP_INCLUDE_BIN|dir|agent/bin|bin
80
+
60
81
  section|Recent hermes-cleanup backups (last 5)
61
82
  recent|agent/hermes-cleanup|hermes-cleanup|5
package/backup-pi.ps1 CHANGED
@@ -42,12 +42,19 @@ function Read-Manifest([string]$path) {
42
42
  $l = $line.TrimEnd("`r")
43
43
  if ($l -eq '' -or $l.StartsWith('#')) { continue }
44
44
  $p = $l.Split('|')
45
- [pscustomobject]@{
45
+ $e = [pscustomobject]@{
46
46
  Kind = $p[0]
47
47
  A = if ($p.Count -gt 1) { $p[1] } else { '' }
48
48
  B = if ($p.Count -gt 2) { $p[2] } else { '' }
49
49
  C = if ($p.Count -gt 3) { $p[3] } else { '' }
50
+ D = if ($p.Count -gt 4) { $p[4] } else { '' }
51
+ Gate = ''
50
52
  }
53
+ # optional|ENV|kind|pi path|backup path: shift the fields left and remember the gate variable
54
+ if ($e.Kind -eq 'optional') {
55
+ $e = [pscustomobject]@{ Kind = $e.B; A = $e.C; B = $e.D; C = ''; D = ''; Gate = $e.A }
56
+ }
57
+ $e
51
58
  }
52
59
  }
53
60
 
@@ -96,6 +103,23 @@ function Copy-IfExists([string]$src, [string]$dst) {
96
103
  }
97
104
  }
98
105
 
106
+ # SQLite databases use WAL, so a plain copy can miss committed data. sqlite3's .backup
107
+ # reads through the WAL and writes a consistent snapshot.
108
+ function Backup-Sqlite([string]$src, [string]$dst) {
109
+ if (-not (Test-Path -LiteralPath $src)) { Warn " $(Get-RelativeLabel $dst) - not found, skipping"; return }
110
+ $dstDir = Split-Path $dst -Parent
111
+ if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
112
+ if (Get-Command sqlite3 -ErrorAction SilentlyContinue) {
113
+ & sqlite3 $src '.timeout 5000' ".backup '$dst'"
114
+ if ($LASTEXITCODE -ne 0) { throw "sqlite3 failed to back up $src" }
115
+ OK " $(Get-RelativeLabel $dst) (sqlite3 snapshot)"
116
+ } else {
117
+ Warn " sqlite3 not found: copying $([System.IO.Path]::GetFileName($src)) directly. Changes not yet checkpointed may be missing; install sqlite3 or quit Pi first."
118
+ Copy-Item -LiteralPath $src -Destination $dst -Force
119
+ OK " $(Get-RelativeLabel $dst)"
120
+ }
121
+ }
122
+
99
123
  function Copy-DirIfExists([string]$src, [string]$dst) {
100
124
  if (Test-Path -LiteralPath $src) {
101
125
  $dstDir = Split-Path $dst -Parent
@@ -153,8 +177,13 @@ $entries = @(Read-Manifest $ManifestFile)
153
177
  $total = @($entries | Where-Object { $_.Kind -eq 'section' }).Count
154
178
  $step = 0
155
179
  foreach ($e in $entries) {
180
+ if ($e.Gate -and [Environment]::GetEnvironmentVariable($e.Gate) -ne '1') {
181
+ Log " $($e.A) - optional, not included (set $($e.Gate)=1 to include)"
182
+ continue
183
+ }
156
184
  switch ($e.Kind) {
157
185
  'section' { $step++; Log "$step/$total $($e.A)..." }
186
+ 'sqlite' { Backup-Sqlite (P $PiRoot $e.A) (P $ArchiveDir $e.B) }
158
187
  'file' {
159
188
  if ($e.B -eq 'auth.json') { Backup-Auth (P $PiRoot $e.A) (P $ArchiveDir $e.B) }
160
189
  else { Copy-IfExists (P $PiRoot $e.A) (P $ArchiveDir $e.B) }
package/backup-pi.sh CHANGED
@@ -123,11 +123,38 @@ backup_auth() {
123
123
  fi
124
124
  }
125
125
 
126
+ # SQLite databases use WAL, so a plain copy can miss committed data. sqlite3's .backup
127
+ # reads through the WAL and writes a consistent snapshot.
128
+ backup_sqlite() {
129
+ local src="$1" dst="$2"
130
+ if [[ ! -f "$src" ]]; then
131
+ warn " ${dst#"$BACKUP_DIR"/} — not found, skipping"
132
+ return 0
133
+ fi
134
+ mkdir -p "$(dirname "$dst")"
135
+ if command -v sqlite3 >/dev/null 2>&1; then
136
+ sqlite3 "$src" ".timeout 5000" ".backup '$dst'" || { err " sqlite3 failed to back up ${src}"; return 1; }
137
+ ok " ${dst#"$BACKUP_DIR"/} (sqlite3 snapshot)"
138
+ else
139
+ warn " sqlite3 not found: copying ${src##*/} directly. Changes not yet checkpointed may be missing; install sqlite3 or quit Pi first."
140
+ cp -a "$src" "$dst"
141
+ ok " ${dst#"$BACKUP_DIR"/}"
142
+ fi
143
+ }
144
+
126
145
  backup_manifest_entries() {
127
- local total step=0 kind f1 f2 f3
146
+ local total step=0 kind f1 f2 f3 f4 gate
128
147
  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:-}")"
148
+ while IFS='|' read -r kind f1 f2 f3 f4 || [[ -n "$kind" ]]; do
149
+ kind="$(strip_cr "$kind")"; f1="$(strip_cr "${f1:-}")"; f2="$(strip_cr "${f2:-}")"; f3="$(strip_cr "${f3:-}")"; f4="$(strip_cr "${f4:-}")"
150
+ # optional|ENV|kind|pi path|backup path: shift the fields left, skip unless ENV=1
151
+ if [[ "$kind" == optional ]]; then
152
+ gate="$f1"; kind="$f2"; f1="$f3"; f2="$f4"; f3=""; f4=""
153
+ if [[ "${!gate:-0}" != "1" ]]; then
154
+ log " $f1 — optional, not included (set $gate=1 to include)"
155
+ continue
156
+ fi
157
+ fi
131
158
  case "$kind" in
132
159
  ''|'#'*) continue ;;
133
160
  section)
@@ -135,6 +162,8 @@ backup_manifest_entries() {
135
162
  file)
136
163
  if [[ "$f2" == "auth.json" ]]; then backup_auth "$PI_ROOT/$f1" "$BACKUP_DIR/$f2"
137
164
  else copy_if_exists "$PI_ROOT/$f1" "$BACKUP_DIR/$f2"; fi ;;
165
+ sqlite)
166
+ backup_sqlite "$PI_ROOT/$f1" "$BACKUP_DIR/$f2" ;;
138
167
  dir)
139
168
  copy_dir_if_exists "$PI_ROOT/$f1" "$BACKUP_DIR/$f2" ;;
140
169
  dir-each)
@@ -737,11 +737,19 @@ PI_BACKUP_ASSUME_YES=1 ./restore-pi.sh ~/pi-backups/pi-backup-20260811-143000
737
737
  ✅ Local extension source code (pi-timeout, pi-chunk, etc.)
738
738
  ✅ npm manifests (package.json, lockfiles)
739
739
  ✅ Hermes memory files (MEMORY.md, USER.md, failures.md)
740
+ ✅ Memory database (sessions.db, snapshotted with sqlite3) and memory skills
741
+ ✅ Extension state (missions, extensions, anti-stall, models-store, run-history)
740
742
  ✅ Recent hermes-cleanup backups
741
743
 
744
+ ## Optional (set to 1 to include)
745
+
746
+ 🔸 Session history (\`PI_BACKUP_INCLUDE_SESSIONS=1\`, large)
747
+ 🔸 Cloned git packages (\`PI_BACKUP_INCLUDE_GIT=1\`)
748
+ 🔸 Bundled binaries (\`PI_BACKUP_INCLUDE_BIN=1\`)
749
+
742
750
  ## What Does NOT Get Backed Up (Regeneratable)
743
751
 
744
- ❌ Sessions (\`~/.pi/agent/sessions/\`)
752
+ ❌ Sessions, unless \`PI_BACKUP_INCLUDE_SESSIONS=1\` (\`~/.pi/agent/sessions/\`)
745
753
  ❌ Context-mode KB (\`context-mode/kb.sqlite\`)
746
754
  ❌ node_modules/
747
755
  ❌ loop-police-detections.jsonl
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamiefutch/pi-backup",
3
- "version": "1.1.13",
3
+ "version": "1.2.0",
4
4
  "description": "Pi package: Pi backup/restore scripts and TUI extension. Back up and restore all Pi config, skills, extensions, and memory; /pi-backup interactive manager.",
5
5
  "type": "module",
6
6
  "scripts": {
package/restore-pi.ps1 CHANGED
@@ -37,12 +37,19 @@ function Read-Manifest([string]$path) {
37
37
  $l = $line.TrimEnd("`r")
38
38
  if ($l -eq '' -or $l.StartsWith('#')) { continue }
39
39
  $p = $l.Split('|')
40
- [pscustomobject]@{
40
+ $e = [pscustomobject]@{
41
41
  Kind = $p[0]
42
42
  A = if ($p.Count -gt 1) { $p[1] } else { '' }
43
43
  B = if ($p.Count -gt 2) { $p[2] } else { '' }
44
44
  C = if ($p.Count -gt 3) { $p[3] } else { '' }
45
+ D = if ($p.Count -gt 4) { $p[4] } else { '' }
46
+ Gate = ''
45
47
  }
48
+ # optional|ENV|kind|pi path|backup path: shift the fields left and remember the gate variable
49
+ if ($e.Kind -eq 'optional') {
50
+ $e = [pscustomobject]@{ Kind = $e.B; A = $e.C; B = $e.D; C = ''; D = ''; Gate = $e.A }
51
+ }
52
+ $e
46
53
  }
47
54
  }
48
55
 
@@ -237,8 +244,24 @@ $entries = @(Read-Manifest $ManifestFile)
237
244
  $total = @($entries | Where-Object { $_.Kind -eq 'section' }).Count
238
245
  $step = 0
239
246
  foreach ($e in $entries) {
247
+ # optional entries are restored only when the backup holds them, silently otherwise
248
+ if ($e.Gate -and -not (Test-Path -LiteralPath (P $BackupDir $e.B))) { continue }
240
249
  switch ($e.Kind) {
241
250
  'section' { $step++; Log "$step/$total $($e.A)..." }
251
+ 'sqlite' {
252
+ $src = P $BackupDir $e.B
253
+ $dst = P $PiRoot $e.A
254
+ if (Test-Path -LiteralPath $src -PathType Leaf) {
255
+ $dstDir = Split-Path $dst -Parent
256
+ if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
257
+ # stale -wal/-shm/-journal next to the destination would be applied to the restored file
258
+ foreach ($suffix in '-wal','-shm','-journal') { Remove-Item -LiteralPath ($dst + $suffix) -Force -ErrorAction SilentlyContinue }
259
+ Copy-Item -LiteralPath $src -Destination $dst -Force
260
+ OK " $($e.B)"
261
+ } else {
262
+ Warn " $($e.B) - not in backup, skipping"
263
+ }
264
+ }
242
265
  'file' {
243
266
  $dst = P $PiRoot $e.A
244
267
  $src = P $BackupDir $e.B
package/restore-pi.sh CHANGED
@@ -157,11 +157,30 @@ restore_dir() {
157
157
  fi
158
158
  }
159
159
 
160
+ # Restores a SQLite database. Stale -wal/-shm/-journal files next to the destination
161
+ # would be applied to the restored file, so they are removed first.
162
+ restore_sqlite() {
163
+ local src="$1" dst="$2"
164
+ if [[ -f "$src" ]]; then
165
+ mkdir -p "$(dirname "$dst")"
166
+ rm -f "$dst-wal" "$dst-shm" "$dst-journal"
167
+ cp "$src" "$dst"
168
+ ok " ${src#"$BACKUP_DIR"/}"
169
+ else
170
+ warn " ${src#"$BACKUP_DIR"/} — not in backup, skipping"
171
+ fi
172
+ }
173
+
160
174
  restore_manifest_entries() {
161
- local total step=0 kind f1 f2 f3
175
+ local total step=0 kind f1 f2 f3 f4 gate
162
176
  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:-}")"
177
+ while IFS='|' read -r kind f1 f2 f3 f4 || [[ -n "$kind" ]]; do
178
+ kind="$(strip_cr "$kind")"; f1="$(strip_cr "${f1:-}")"; f2="$(strip_cr "${f2:-}")"; f3="$(strip_cr "${f3:-}")"; f4="$(strip_cr "${f4:-}")"
179
+ # optional|ENV|kind|pi path|backup path: restore it only if the backup holds it, silently otherwise.
180
+ if [[ "$kind" == optional ]]; then
181
+ kind="$f2"; f1="$f3"; f2="$f4"; f3=""; f4=""
182
+ [[ -e "$BACKUP_DIR/$f2" ]] || continue
183
+ fi
165
184
  case "$kind" in
166
185
  ''|'#'*) continue ;;
167
186
  section)
@@ -170,6 +189,8 @@ restore_manifest_entries() {
170
189
  restore_file "$BACKUP_DIR/$f2" "$PI_ROOT/$f1"
171
190
  # Credentials: keep a restored auth.json private to this user
172
191
  if [[ "$f2" == "auth.json" && -f "$BACKUP_DIR/$f2" && -f "$PI_ROOT/$f1" ]]; then chmod 600 "$PI_ROOT/$f1"; fi ;;
192
+ sqlite)
193
+ restore_sqlite "$BACKUP_DIR/$f2" "$PI_ROOT/$f1" ;;
173
194
  dir)
174
195
  restore_dir "$BACKUP_DIR/$f2" "$PI_ROOT/$f1" ;;
175
196
  dir-each|recent)