@jamiefutch/pi-backup 1.0.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/LICENSE +21 -0
- package/README.md +335 -0
- package/backup-pi.ps1 +233 -0
- package/backup-pi.sh +230 -0
- package/extensions/pi-backup-manager.ts +565 -0
- package/package.json +35 -0
- package/pi-clean-reinstall.ps1 +79 -0
- package/pi-clean-reinstall.sh +94 -0
- package/restore-pi.ps1 +255 -0
- package/restore-pi.sh +245 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jamie Futch
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# pi-backup — Pi Backup & Restore Utilities
|
|
2
|
+
|
|
3
|
+
Complete backup/restore for Pi coding agent. Backs up **everything** needed to restore to identical state after a clean reinstall.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### Via Pi Package Manager (recommended)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# From npm (when published)
|
|
11
|
+
pi install npm:@jamiefutch/pi-backup@latest
|
|
12
|
+
|
|
13
|
+
# From git
|
|
14
|
+
pi install git:github.com/jamiefutch/pi-backup@main
|
|
15
|
+
|
|
16
|
+
# From local path
|
|
17
|
+
pi install /path/to/pi-backup
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Manual (add to settings.json)
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"packages": ["npm:@jamiefutch/pi-backup"]
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then restart Pi or run `/reload`.
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Scripts
|
|
33
|
+
|
|
34
|
+
| Script | Purpose |
|
|
35
|
+
|--------|---------|
|
|
36
|
+
| `backup-pi.sh` / `backup-pi.ps1` | Creates timestamped backup of all Pi config, skills, extensions, memory |
|
|
37
|
+
| `pi-clean-reinstall.sh` / `pi-clean-reinstall.ps1` | Nuclear reinstall — wipes `~/.pi`, uninstalls/reinstalls Pi (dangerous) |
|
|
38
|
+
| `restore-pi.sh` / `restore-pi.ps1` | Restores backup after fresh Pi install |
|
|
39
|
+
|
|
40
|
+
| Platform | Extensions |
|
|
41
|
+
|----------|------------|
|
|
42
|
+
| **macOS / Linux (bash)** | `backup-pi.sh`, `restore-pi.sh`, `pi-clean-reinstall.sh` |
|
|
43
|
+
| **Windows (PowerShell)** | `backup-pi.ps1`, `restore-pi.ps1`, `pi-clean-reinstall.ps1` (requires PowerShell 7 / `pwsh`) |
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### TUI Extension (after package install)
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
/pi-backup
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Opens the **Pi Backup Manager** interactive menu:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
Pi Backup Manager — 8 backups (1 internal, 7 external)
|
|
59
|
+
External dir: ~/projects/personal/pi-utils/backups
|
|
60
|
+
|
|
61
|
+
→ 📦 Create internal backup (~/.pi/backups/ — quick rollback)
|
|
62
|
+
💾 Create external backup (survives reinstall)
|
|
63
|
+
📋 List all backups
|
|
64
|
+
⚙️ Configure external backup directory
|
|
65
|
+
❓ Help
|
|
66
|
+
← Exit
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Main Menu Options
|
|
70
|
+
|
|
71
|
+
| Option | Description | Location |
|
|
72
|
+
|--------|-------------|----------|
|
|
73
|
+
| **📦 Create internal backup** | Fast uncompressed backup for quick rollback during normal use | `~/.pi/backups/` |
|
|
74
|
+
| **💾 Create external backup** | Compressed archive for disaster recovery — survives clean reinstall | resolved per priority chain |
|
|
75
|
+
| **📋 List all backups** | Browse, restore, copy, delete, or view manifests of all backups | Both locations |
|
|
76
|
+
| **⚙️ Configure external backup directory** | Set or reset the `settings.json` `externalBackupDir` value | settings.json |
|
|
77
|
+
| **❓ Help** | Full documentation on backup locations, what's backed up, restore process | — |
|
|
78
|
+
| **← Exit** | Close the backup manager | — |
|
|
79
|
+
|
|
80
|
+
#### Backup List View
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
Backups (8 total)
|
|
84
|
+
|
|
85
|
+
→ 📁 pi-backup-20260812-112053 │ 2026-08-12 11:20:53 │ 1.6 MB │ 347 files
|
|
86
|
+
💾 pi-backup-20260812-112019.zip │ 2026-08-12 11:20:19 │ 1.6 MB │ 1 files • compressed
|
|
87
|
+
💾 pi-backup-20260812-095402.zip │ 2026-08-12 09:54:02 │ 1.6 MB │ 1 files • compressed
|
|
88
|
+
...
|
|
89
|
+
🔄 Refresh
|
|
90
|
+
← Back
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Columns: **Location icon** • **Name** • **Timestamp** • **Size** • **File count** • **Compression status**
|
|
94
|
+
|
|
95
|
+
#### Backup Actions (after selecting a backup)
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
Backup: pi-backup-20260812-112019.zip
|
|
99
|
+
|
|
100
|
+
→ 🔄 Restore this backup (2026-08-12 11:20:19)
|
|
101
|
+
📋 Copy to internal (~/.pi/backups/)
|
|
102
|
+
📄 View manifest
|
|
103
|
+
🗑️ Delete
|
|
104
|
+
← Back
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
| Action | Description |
|
|
108
|
+
|--------|-------------|
|
|
109
|
+
| **🔄 Restore** | Runs `restore-pi.sh` — overwrites all Pi config (requires confirmation) |
|
|
110
|
+
| **📋 Copy to internal** | Copies external backup to `~/.pi/backups/` for quick access (external only) |
|
|
111
|
+
| **📄 View manifest** | Opens `MANIFEST.txt` in editor (extracts from compressed archives) |
|
|
112
|
+
| **🗑️ Delete** | Permanently removes backup (requires confirmation) |
|
|
113
|
+
|
|
114
|
+
#### Backup Location Summary
|
|
115
|
+
|
|
116
|
+
| Location | Path | Survives Clean Reinstall? | Format |
|
|
117
|
+
|----------|------|---------------------------|--------|
|
|
118
|
+
| **Internal** | `~/.pi/backups/` | ❌ NO (wiped by `rm -rf ~/.pi`) | Uncompressed dirs |
|
|
119
|
+
| **External** | `~/projects/personal/pi-utils/backups/` | ✅ YES | Compressed (.7z/.zip) |
|
|
120
|
+
|
|
121
|
+
> **Key point**: Always create an **external backup** before a clean reinstall. Internal backups are for quick rollbacks during normal use only.
|
|
122
|
+
|
|
123
|
+
### Command Line (scripts directly)
|
|
124
|
+
|
|
125
|
+
#### 1. Backup (before reinstall)
|
|
126
|
+
```bash
|
|
127
|
+
~/projects/personal/pi-backup/backup-pi.sh
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### 2. Clean Reinstall
|
|
131
|
+
```bash
|
|
132
|
+
~/projects/personal/pi-backup/pi-clean-reinstall.sh
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### 3. Restore
|
|
136
|
+
```bash
|
|
137
|
+
~/projects/personal/pi-backup/restore-pi.sh ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### 4. Reinstall npm deps + verify
|
|
141
|
+
```bash
|
|
142
|
+
cd ~/.pi/agent && npm install # or bun install
|
|
143
|
+
pi --version
|
|
144
|
+
/ctx-doctor
|
|
145
|
+
/loop-police-status
|
|
146
|
+
/scoped-memory-status
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Windows (PowerShell)
|
|
150
|
+
|
|
151
|
+
On Windows, use the `.ps1` scripts from PowerShell 7 (`pwsh`). The backup data directory defaults to `~/projects/personal/pi-utils/backups`.
|
|
152
|
+
|
|
153
|
+
```powershell
|
|
154
|
+
# 1. Backup (before reinstall)
|
|
155
|
+
.\backup-pi.ps1
|
|
156
|
+
|
|
157
|
+
# 2. Clean Reinstall (nuclear - wipes ~/.pi, uninstalls/reinstalls Pi)
|
|
158
|
+
.\pi-clean-reinstall.ps1
|
|
159
|
+
|
|
160
|
+
# 3. Restore
|
|
161
|
+
.\restore-pi.ps1 ~\projects\personal\pi-utils\backups\pi-backup-20260811-143000
|
|
162
|
+
|
|
163
|
+
# 4. Reinstall npm deps + verify
|
|
164
|
+
cd ~\.pi\agent; npm install
|
|
165
|
+
pi --version
|
|
166
|
+
/ctx-doctor
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Customize locations in PowerShell with environment variables (same names as the bash scripts):
|
|
170
|
+
|
|
171
|
+
```powershell
|
|
172
|
+
$env:PI_ROOT='C:\Users\you\.pi'; $env:BACKUP_ROOT='D:\pi-backups'; .\backup-pi.ps1
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
> **Note**: If the Execution Policy blocks scripts, run once: `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned`. Backups are created as `.zip` via the built-in `Compress-Archive` (no external tools needed).
|
|
176
|
+
|
|
177
|
+
## What Gets Backed Up
|
|
178
|
+
|
|
179
|
+
| Category | Paths |
|
|
180
|
+
|----------|-------|
|
|
181
|
+
| **Core config** | `settings.json`, `auth.json`, `models.json`, `trust.json`, `keybindings.json`, `pix.json`, `optimizer.json`, `hermes-memory-config.json` |
|
|
182
|
+
| **Extension configs** | `pi-hermes-memory-cleanup/scoped-memory.json`, `loop-police.json`, `pi-rtk/config.json`, `context-mode/config.json`, `pi-timeout/config.json`, `pi-context-viewer/config.json`, `pix-optimizer/config.json`, `pi-nvidia-nim/config.json` |
|
|
183
|
+
| **Global skills** | `~/.pi/agent/skills/` |
|
|
184
|
+
| **Project skills & memory** | `~/.pi/agent/projects-memory/<project>/` |
|
|
185
|
+
| **Local extension source** | `~/projects/personal/pi-timeout/`, `pi-chunk/`, `pi-hermes-memory-cleanup/`, `pi-context-docs/` |
|
|
186
|
+
| **npm manifests** | `package.json`, `package-lock.json`, `npm-shrinkwrap.json` from `~/.pi/agent/npm/` |
|
|
187
|
+
| **Hermes memory (active)** | `MEMORY.md`, `USER.md`, `failures.md`, `retired-*.md` |
|
|
188
|
+
| **Cleanup backups** | Last 5 from `~/.pi/agent/hermes-cleanup/` |
|
|
189
|
+
|
|
190
|
+
The backup script output:
|
|
191
|
+
```
|
|
192
|
+
════════════════════════════════════════════════════════════
|
|
193
|
+
Pi Complete Backup
|
|
194
|
+
════════════════════════════════════════════════════════════
|
|
195
|
+
Source: /Users/jamiefutch/.pi
|
|
196
|
+
Destination: ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000
|
|
197
|
+
...
|
|
198
|
+
Backup complete!
|
|
199
|
+
Location: ~/projects/personal/pi-utils/backups/pi-backup-20260811-143000
|
|
200
|
+
Size: 12M
|
|
201
|
+
Files: 347
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## What Does NOT Get Backed Up (Regeneratable)
|
|
205
|
+
|
|
206
|
+
| Excluded | Why |
|
|
207
|
+
|----------|-----|
|
|
208
|
+
| `~/.pi/agent/sessions/` | Session history — regenerated |
|
|
209
|
+
| `~/.pi/agent/context-mode/kb.sqlite` | Knowledge base — rebuilt from indexed sources |
|
|
210
|
+
| `~/.pi/agent/npm/node_modules/` | Dependencies — reinstalled via `npm install` |
|
|
211
|
+
| `~/.pi/agent/loop-police-detections.jsonl` | Detection logs — regenerated |
|
|
212
|
+
| `~/.pi/agent/.pi-hermes-locks.sqlite*` | Lock files — recreated |
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Backup Location
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
~/projects/personal/pi-utils/backups/
|
|
220
|
+
├── pi-backup-20260811-143000/
|
|
221
|
+
│ ├── MANIFEST.txt
|
|
222
|
+
│ ├── settings.json
|
|
223
|
+
│ ├── auth.json
|
|
224
|
+
│ ├── models.json
|
|
225
|
+
│ ├── skills/
|
|
226
|
+
│ ├── projects-memory/
|
|
227
|
+
│ ├── local-extensions/
|
|
228
|
+
│ ├── npm-manifests/
|
|
229
|
+
│ ├── pi-hermes-memory/
|
|
230
|
+
│ └── hermes-cleanup/
|
|
231
|
+
└── pi-backup-20260811-150000/
|
|
232
|
+
...
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Each backup is self-contained with a `MANIFEST.txt` listing all files.
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
## External Backup Directory Configuration
|
|
240
|
+
|
|
241
|
+
The external backup location is resolved in this priority order:
|
|
242
|
+
|
|
243
|
+
1. **`BACKUP_ROOT`** environment variable (highest priority)
|
|
244
|
+
2. **`externalBackupDir`** key in `~/.pi/agent/settings.json`
|
|
245
|
+
3. **Default**: `~/projects/personal/pi-utils/backups/`
|
|
246
|
+
|
|
247
|
+
```json
|
|
248
|
+
// ~/.pi/agent/settings.json
|
|
249
|
+
{
|
|
250
|
+
"externalBackupDir": "/custom/path/to/backups"
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### In the TUI
|
|
255
|
+
|
|
256
|
+
Use the **⚙️ Configure external backup directory** option in `/pi-backup` to set or reset the `settings.json` value:
|
|
257
|
+
|
|
258
|
+
- **📝 Change directory** — prompt to type a path, saved to `settings.json`
|
|
259
|
+
- **🔄 Reset to default** — removes the `settings.json` value, falls back to env var / default
|
|
260
|
+
|
|
261
|
+
### When the prompt appears
|
|
262
|
+
|
|
263
|
+
The configure prompt only appears when creating an external backup if **both** of these are true:
|
|
264
|
+
- No `BACKUP_ROOT` env var and no `externalBackupDir` in `settings.json`, AND
|
|
265
|
+
- The default external directory is empty or missing
|
|
266
|
+
|
|
267
|
+
If the default directory already contains backups, it is treated as configured and used directly with no prompt. Restore never prompts (it uses the selected backup's direct path).
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
## Environment Variables
|
|
272
|
+
|
|
273
|
+
| Variable | Default | Purpose |
|
|
274
|
+
|----------|---------|---------|
|
|
275
|
+
| `PI_ROOT` | `~/.pi` | Override Pi root directory |
|
|
276
|
+
| `BACKUP_ROOT` | `~/projects/personal/pi-utils/backups` | Override backup destination (highest priority for the TUI too) |
|
|
277
|
+
|
|
278
|
+
```bash
|
|
279
|
+
# Custom locations
|
|
280
|
+
PI_ROOT=/custom/.pi BACKUP_ROOT=/mnt/backups ~/projects/personal/pi-backup/backup-pi.sh
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## Automation (Cron)
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
# Daily backup at 3 AM
|
|
289
|
+
0 3 * * * ~/projects/personal/pi-backup/backup-pi.sh >> ~/pi-backup.log 2>&1
|
|
290
|
+
|
|
291
|
+
# Keep last 30 backups
|
|
292
|
+
0 4 * * * find ~/projects/personal/pi-utils/backups -maxdepth 1 -type d -name 'pi-backup-*' -mtime +30 -exec rm -rf {} \;
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
## Troubleshooting
|
|
298
|
+
|
|
299
|
+
| Issue | Fix |
|
|
300
|
+
|-------|-----|
|
|
301
|
+
| "Permission denied" | `chmod +x backup-pi.sh restore-pi.sh` |
|
|
302
|
+
| "npm install fails" | Check Node.js version (need ≥22), try `bun install` |
|
|
303
|
+
| "Skills not loading" | Verify `settings.json` has correct `packages` paths after restore |
|
|
304
|
+
| "Models not found" | Check `models.json` restored correctly, restart Pi |
|
|
305
|
+
| "Auth failed" | Verify `auth.json` restored, API keys valid |
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## Manual Verify Checklist After Restore
|
|
310
|
+
|
|
311
|
+
- [ ] `pi --version` shows correct version
|
|
312
|
+
- [ ] `/ctx-doctor` shows all checks pass
|
|
313
|
+
- [ ] `/loop-police-status` shows config loaded
|
|
314
|
+
- [ ] `/scoped-memory-status` shows enabled + last injection stats
|
|
315
|
+
- [ ] `/memory-search "test"` returns results
|
|
316
|
+
- [ ] `/context-stats` shows context breakdown
|
|
317
|
+
- [ ] `/nim-status` connects (if NVIDIA configured)
|
|
318
|
+
- [ ] Local extensions load (check `settings.json` paths)
|
|
319
|
+
- [ ] Run a simple task to verify end-to-end
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
## License
|
|
324
|
+
|
|
325
|
+
MIT License
|
|
326
|
+
|
|
327
|
+
Copyright (c) 2025 Jamie Futch
|
|
328
|
+
|
|
329
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
330
|
+
|
|
331
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
332
|
+
|
|
333
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
334
|
+
|
|
335
|
+
See the [`LICENSE`](LICENSE) file for the full license text.
|
package/backup-pi.ps1
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# =============================================================
|
|
2
|
+
# Pi Complete Backup Script (Windows PowerShell)
|
|
3
|
+
# Backs up ALL user configuration, skills, extensions, and state
|
|
4
|
+
# Run before reinstall; restore with restore-pi.ps1 after fresh install
|
|
5
|
+
#
|
|
6
|
+
# Usage:
|
|
7
|
+
# .\backup-pi.ps1 # default locations
|
|
8
|
+
# $env:PI_ROOT='...'; .\backup-pi.ps1 # custom Pi root
|
|
9
|
+
# $env:BACKUP_ROOT='...'; .\backup-pi.ps1 # custom backup root
|
|
10
|
+
# =============================================================
|
|
11
|
+
[CmdletBinding()]
|
|
12
|
+
param()
|
|
13
|
+
|
|
14
|
+
$ErrorActionPreference = 'Stop'
|
|
15
|
+
|
|
16
|
+
# ─── Config ────────────────────────────────────────────────
|
|
17
|
+
$PiRoot = if ($env:PI_ROOT) { $env:PI_ROOT } else { Join-Path $HOME '.pi' }
|
|
18
|
+
$BackupRoot = if ($env:BACKUP_ROOT) { $env:BACKUP_ROOT } else { Join-Path $HOME 'projects\personal\pi-utils\backups' }
|
|
19
|
+
$Timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
|
|
20
|
+
$ArchiveBase = "pi-backup-$Timestamp"
|
|
21
|
+
$ArchiveDir = Join-Path $BackupRoot $ArchiveBase
|
|
22
|
+
$KeepUncompressed = $env:KEEP_UNCOMPRESSED -eq '1'
|
|
23
|
+
|
|
24
|
+
function Log { Write-Host "[INFO] $args" -ForegroundColor Cyan }
|
|
25
|
+
function OK { Write-Host "[OK] $args" -ForegroundColor Green }
|
|
26
|
+
function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
|
|
27
|
+
function Err { Write-Host "[ERR] $args" -ForegroundColor Red; exit 1 }
|
|
28
|
+
|
|
29
|
+
function Copy-IfExists([string]$src, [string]$dst) {
|
|
30
|
+
if (Test-Path -LiteralPath $src) {
|
|
31
|
+
$dstDir = Split-Path $dst -Parent
|
|
32
|
+
if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
33
|
+
Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force
|
|
34
|
+
OK " $([System.IO.Path]::GetFileName($src))"
|
|
35
|
+
} else {
|
|
36
|
+
Warn " $([System.IO.Path]::GetFileName($src)) - not found, skipping"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function Copy-DirIfExists([string]$src, [string]$dst) {
|
|
41
|
+
if (Test-Path -LiteralPath $src) {
|
|
42
|
+
$dstDir = Split-Path $dst -Parent
|
|
43
|
+
if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
44
|
+
Copy-Item -LiteralPath $src -Destination $dst -Recurse -Force
|
|
45
|
+
OK " $([System.IO.Path]::GetFileName($src))/"
|
|
46
|
+
} else {
|
|
47
|
+
Warn " $([System.IO.Path]::GetFileName($src))/ - not found, skipping"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function Copy-Tree([string]$src, [string]$dst, [string[]]$excludeDirs = @(), [string[]]$excludeFiles = @()) {
|
|
52
|
+
if (!(Test-Path -LiteralPath $dst)) { New-Item -ItemType Directory -Path $dst -Force | Out-Null }
|
|
53
|
+
if (Get-Command robocopy -ErrorAction SilentlyContinue) {
|
|
54
|
+
$robocopyArgs = @($src, $dst, '/E')
|
|
55
|
+
foreach ($d in $excludeDirs) { $robocopyArgs += @('/XD', $d) }
|
|
56
|
+
foreach ($f in $excludeFiles) { $robocopyArgs += @('/XF', $f) }
|
|
57
|
+
$robocopyArgs += @('/NFL','/NDL','/NJH','/NJS')
|
|
58
|
+
& robocopy @robocopyArgs | Out-Null
|
|
59
|
+
} else {
|
|
60
|
+
# Fallback: manual recursive copy with exclusions
|
|
61
|
+
$dirs = if ($excludeDirs.Count) { @{ } } else { @{ } }
|
|
62
|
+
Get-ChildItem -LiteralPath $src | ForEach-Object {
|
|
63
|
+
$skip = $false
|
|
64
|
+
if ($_.PSIsContainer -and $_.Name -in $excludeDirs) { $skip = $true }
|
|
65
|
+
if (-not $_.PSIsContainer -and $_.Name -like $excludeFiles) { $skip = $true }
|
|
66
|
+
if (-not $skip) {
|
|
67
|
+
Copy-Item -LiteralPath $_.FullName -Destination (Join-Path $dst $_.Name) -Recurse -Force
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
New-Item -ItemType Directory -Path $ArchiveDir -Force | Out-Null
|
|
74
|
+
|
|
75
|
+
Write-Host ""
|
|
76
|
+
Log "================================================================"
|
|
77
|
+
Log " Pi Complete Backup"
|
|
78
|
+
Log "================================================================"
|
|
79
|
+
Log "Source: $PiRoot"
|
|
80
|
+
Log "Destination: $ArchiveDir"
|
|
81
|
+
Write-Host ""
|
|
82
|
+
|
|
83
|
+
# ── 1. Core Config Files ──────────────────────────────────
|
|
84
|
+
Log "1/8 Core configuration files..."
|
|
85
|
+
$agentDir = Join-Path $PiRoot 'agent'
|
|
86
|
+
Copy-IfExists (Join-Path $agentDir 'settings.json') (Join-Path $ArchiveDir 'settings.json')
|
|
87
|
+
Copy-IfExists (Join-Path $agentDir 'auth.json') (Join-Path $ArchiveDir 'auth.json')
|
|
88
|
+
Copy-IfExists (Join-Path $agentDir 'models.json') (Join-Path $ArchiveDir 'models.json')
|
|
89
|
+
Copy-IfExists (Join-Path $agentDir 'trust.json') (Join-Path $ArchiveDir 'trust.json')
|
|
90
|
+
Copy-IfExists (Join-Path $agentDir 'keybindings.json') (Join-Path $ArchiveDir 'keybindings.json')
|
|
91
|
+
Copy-IfExists (Join-Path $agentDir 'pix.json') (Join-Path $ArchiveDir 'pix.json')
|
|
92
|
+
Copy-IfExists (Join-Path $agentDir 'optimizer.json') (Join-Path $ArchiveDir 'optimizer.json')
|
|
93
|
+
Copy-IfExists (Join-Path $agentDir 'lmstudio.json.bak') (Join-Path $ArchiveDir 'lmstudio.json.bak')
|
|
94
|
+
Copy-IfExists (Join-Path $agentDir 'hermes-memory-config.json') (Join-Path $ArchiveDir 'hermes-memory-config.json')
|
|
95
|
+
Copy-IfExists (Join-Path $agentDir 'pi.code-workspace') (Join-Path $ArchiveDir 'pi.code-workspace')
|
|
96
|
+
|
|
97
|
+
# ── 2. Extension Configs ──────────────────────────────────
|
|
98
|
+
Log "2/8 Extension configurations..."
|
|
99
|
+
Copy-IfExists (Join-Path $agentDir 'pi-hermes-memory-cleanup\scoped-memory.json') (Join-Path $ArchiveDir 'pi-hermes-memory-cleanup\scoped-memory.json')
|
|
100
|
+
Copy-IfExists (Join-Path $agentDir 'loop-police.json') (Join-Path $ArchiveDir 'loop-police.json')
|
|
101
|
+
Copy-IfExists (Join-Path $agentDir 'pi-rtk\config.json') (Join-Path $ArchiveDir 'pi-rtk\config.json')
|
|
102
|
+
Copy-IfExists (Join-Path $agentDir 'context-mode\config.json') (Join-Path $ArchiveDir 'context-mode\config.json')
|
|
103
|
+
Copy-IfExists (Join-Path $agentDir 'pi-timeout\config.json') (Join-Path $ArchiveDir 'pi-timeout\config.json')
|
|
104
|
+
Copy-IfExists (Join-Path $agentDir 'pi-context-viewer\config.json') (Join-Path $ArchiveDir 'pi-context-viewer\config.json')
|
|
105
|
+
Copy-IfExists (Join-Path $agentDir 'pix-optimizer\config.json') (Join-Path $ArchiveDir 'pix-optimizer\config.json')
|
|
106
|
+
Copy-IfExists (Join-Path $agentDir 'pi-nvidia-nim\config.json') (Join-Path $ArchiveDir 'pi-nvidia-nim\config.json')
|
|
107
|
+
|
|
108
|
+
# ── 3. Global Skills ──────────────────────────────────────
|
|
109
|
+
Log "3/8 Global skills..."
|
|
110
|
+
Copy-DirIfExists (Join-Path $agentDir 'skills') (Join-Path $ArchiveDir 'skills')
|
|
111
|
+
|
|
112
|
+
# ── 4. Project Skills & Memory ────────────────────────────
|
|
113
|
+
Log "4/8 Project skills & memory..."
|
|
114
|
+
$projMem = Join-Path $agentDir 'projects-memory'
|
|
115
|
+
if (Test-Path -LiteralPath $projMem) {
|
|
116
|
+
$projDst = Join-Path $ArchiveDir 'projects-memory'
|
|
117
|
+
Copy-DirIfExists $projMem $projDst
|
|
118
|
+
$count = @(Get-ChildItem -LiteralPath $projDst -Directory -ErrorAction SilentlyContinue).Count
|
|
119
|
+
OK " projects-memory/ ($count projects)"
|
|
120
|
+
} else {
|
|
121
|
+
Warn " projects-memory/ - not found"
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# ── 5. Local Extensions (source code only, no node_modules) ─
|
|
125
|
+
Log "5/8 Local extension source code..."
|
|
126
|
+
$localExts = @('pi-timeout', 'pi-chunk', 'pi-hermes-memory-cleanup', 'pi-context-docs')
|
|
127
|
+
foreach ($ext in $localExts) {
|
|
128
|
+
$src = Join-Path $HOME "projects\personal\$ext"
|
|
129
|
+
if (Test-Path -LiteralPath $src) {
|
|
130
|
+
$dst = Join-Path $ArchiveDir "local-extensions\$ext"
|
|
131
|
+
$extDstDir = Join-Path $ArchiveDir 'local-extensions'
|
|
132
|
+
if (!(Test-Path -LiteralPath $extDstDir)) { New-Item -ItemType Directory -Path $extDstDir -Force | Out-Null }
|
|
133
|
+
# Copy source, exclude node_modules, dist, build, .git, coverage, logs
|
|
134
|
+
Copy-Tree $src $dst -excludeDirs @('node_modules','dist','build','coverage','.git') -excludeFiles @('*.log')
|
|
135
|
+
OK " $ext/ (source only)"
|
|
136
|
+
} else {
|
|
137
|
+
Warn " $ext - not found at ~/projects/personal/$ext"
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
# ── 6. npm Extension Manifests (package.json + lock) ─────
|
|
142
|
+
Log "6/8 npm extension manifests..."
|
|
143
|
+
$npmDir = Join-Path $agentDir 'npm'
|
|
144
|
+
if (Test-Path -LiteralPath $npmDir) {
|
|
145
|
+
# Only copy package.json and lockfiles, not node_modules
|
|
146
|
+
$manifests = Get-ChildItem -LiteralPath $npmDir -Recurse -Depth 3 -Include @('package.json','package-lock.json','npm-shrinkwrap.json','pnpm-lock.yaml') -ErrorAction SilentlyContinue
|
|
147
|
+
foreach ($m in $manifests) {
|
|
148
|
+
$rel = $m.FullName.Substring($npmDir.Length).TrimStart('\')
|
|
149
|
+
$dstDir = Join-Path $ArchiveDir "npm-manifests\$($rel | Split-Path -Parent)"
|
|
150
|
+
if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
151
|
+
Copy-Item -LiteralPath $m.FullName -Destination (Join-Path $dstDir $m.Name) -Force
|
|
152
|
+
}
|
|
153
|
+
OK " npm manifests (package.json, lockfiles)"
|
|
154
|
+
} else {
|
|
155
|
+
Warn " npm/ - not found"
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
# ── 7. Hermes Memory (active files only, not sessions DB) ─
|
|
159
|
+
Log "7/8 Hermes memory (active files)..."
|
|
160
|
+
$hermes = Join-Path $agentDir 'pi-hermes-memory'
|
|
161
|
+
if (Test-Path -LiteralPath $hermes) {
|
|
162
|
+
$hermesDst = Join-Path $ArchiveDir 'pi-hermes-memory'
|
|
163
|
+
New-Item -ItemType Directory -Path $hermesDst -Force | Out-Null
|
|
164
|
+
foreach ($f in @('MEMORY.md','USER.md','failures.md','retired-failures.md','retired-memory.md')) {
|
|
165
|
+
Copy-IfExists (Join-Path $hermes $f) (Join-Path $hermesDst $f)
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
Warn " pi-hermes-memory/ - not found"
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
# ── 8. Recent hermes-cleanup Backups (last 5) ─────────────
|
|
172
|
+
Log "8/8 Recent hermes-cleanup backups (last 5)..."
|
|
173
|
+
$cleanup = Join-Path $agentDir 'hermes-cleanup'
|
|
174
|
+
if (Test-Path -LiteralPath $cleanup) {
|
|
175
|
+
$recent = Get-ChildItem -LiteralPath $cleanup -Directory | Where-Object { $_.Name -match '^\d{4}' } | Sort-Object Name -Descending | Select-Object -First 5
|
|
176
|
+
foreach ($d in $recent) {
|
|
177
|
+
Copy-DirIfExists $d.FullName (Join-Path $ArchiveDir "hermes-cleanup\$($d.Name)")
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
Warn " hermes-cleanup/ - not found"
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
# ── Manifest ──────────────────────────────────────────────
|
|
184
|
+
Log "Creating manifest..."
|
|
185
|
+
$piVersion = (& pi --version 2>$null) -join ''
|
|
186
|
+
if (-not $piVersion) { $piVersion = 'unknown' }
|
|
187
|
+
$manifest = @"
|
|
188
|
+
Pi Complete Backup Manifest
|
|
189
|
+
===========================
|
|
190
|
+
Created: $(Get-Date)
|
|
191
|
+
Pi Root: $PiRoot
|
|
192
|
+
Pi Version: $piVersion
|
|
193
|
+
Host: $env:COMPUTERNAME
|
|
194
|
+
User: $env:USERNAME
|
|
195
|
+
|
|
196
|
+
Contents:
|
|
197
|
+
"@
|
|
198
|
+
$relFiles = Get-ChildItem -LiteralPath $ArchiveDir -Recurse -File | ForEach-Object { $_.FullName.Substring($ArchiveDir.Length).TrimStart('\') } | Sort-Object
|
|
199
|
+
$manifest += ($relFiles -join "`n") + "`n"
|
|
200
|
+
Set-Content -LiteralPath (Join-Path $ArchiveDir 'MANIFEST.txt') -Value $manifest -Encoding UTF8
|
|
201
|
+
|
|
202
|
+
# ── 9. Compress ───────────────────────────────────────────
|
|
203
|
+
Log "9/9 Compressing backup..."
|
|
204
|
+
$archivePath = Join-Path $BackupRoot ($ArchiveBase + '.zip')
|
|
205
|
+
try {
|
|
206
|
+
Compress-Archive -LiteralPath $ArchiveDir -DestinationPath $archivePath -CompressionLevel Optimal -Force
|
|
207
|
+
OK " $archivePath"
|
|
208
|
+
} catch {
|
|
209
|
+
Warn " Compress-Archive failed: $_.Exception.Message"
|
|
210
|
+
Warn " Keeping uncompressed staging dir: $ArchiveDir"
|
|
211
|
+
$archivePath = $null
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# ── 10. Clean up staging dir (unless told to keep it) ─────
|
|
215
|
+
if ($KeepUncompressed) {
|
|
216
|
+
Log "Keeping uncompressed staging dir: $ArchiveDir"
|
|
217
|
+
} elseif ($archivePath) {
|
|
218
|
+
Remove-Item -LiteralPath $ArchiveDir -Recurse -Force
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
# ── Summary ───────────────────────────────────────────────
|
|
222
|
+
Write-Host ""
|
|
223
|
+
Log "================================================================"
|
|
224
|
+
OK "Backup complete!"
|
|
225
|
+
if ($archivePath) {
|
|
226
|
+
Log "Archive: $archivePath"
|
|
227
|
+
Log "Size: $([math]::Round((Get-Item -LiteralPath $archivePath).Length / 1MB, 1)) MB"
|
|
228
|
+
}
|
|
229
|
+
if ($KeepUncompressed) { Log "Staging: $ArchiveDir" }
|
|
230
|
+
Write-Host ""
|
|
231
|
+
Log "To restore after reinstall:"
|
|
232
|
+
Log " ~/projects/personal/pi-backup/restore-pi.ps1 $archivePath"
|
|
233
|
+
Write-Host ""
|