@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/backup-pi.sh ADDED
@@ -0,0 +1,230 @@
1
+ #!/bin/bash
2
+ # Pi Complete Backup Script
3
+ # Backs up ALL user configuration, skills, extensions, and state
4
+ # Run before reinstall; restore with restore-pi.sh after fresh install
5
+
6
+ set -euo pipefail
7
+
8
+ # ─── Config ──────────────────────────────────────────────────────
9
+ PI_ROOT="${PI_ROOT:-$HOME/.pi}"
10
+ BACKUP_ROOT="${BACKUP_ROOT:-$HOME/projects/personal/pi-utils/backups}"
11
+ TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
12
+ BACKUP_DIR="$BACKUP_ROOT/pi-backup-$TIMESTAMP" # staging dir (compressed into archive below)
13
+ KEEP_UNCOMPRESSED="${KEEP_UNCOMPRESSED:-0}" # set to 1 to keep the staging dir after archiving
14
+
15
+ # ─── Compression tool detection ────────────────────────────────
16
+ # Prefer 7zip (best ratio) if present, fall back to zip.
17
+ ZIP_EXT="zip"
18
+ 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"
31
+ fi
32
+ [[ -n "$ZIP_TOOL" ]] || { echo -e "${RED}[ERR]${NC} Neither 7zip nor zip found. Install one for compressed backups."; exit 1; }
33
+ ARCHIVE="$BACKUP_ROOT/pi-backup-$TIMESTAMP.$ZIP_EXT"
34
+
35
+ # ─── Colors ──────────────────────────────────────────────────────
36
+ RED='\033[0;31m'
37
+ GREEN='\033[0;32m'
38
+ YELLOW='\033[1;33m'
39
+ BLUE='\033[0;34m'
40
+ NC='\033[0m'
41
+
42
+ log() { echo -e "${BLUE}[INFO]${NC} $*"; }
43
+ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
44
+ warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
45
+ err() { echo -e "${RED}[ERR]${NC} $*"; }
46
+
47
+ # ─── Helpers ─────────────────────────────────────────────────────
48
+ copy_if_exists() {
49
+ local src="$1" dst="$2"
50
+ if [[ -e "$src" ]]; then
51
+ mkdir -p "$(dirname "$dst")"
52
+ cp -a "$src" "$dst"
53
+ ok " $(basename "$src")"
54
+ else
55
+ warn " $(basename "$src") — not found, skipping"
56
+ fi
57
+ }
58
+
59
+ copy_dir_if_exists() {
60
+ local src="$1" dst="$2"
61
+ if [[ -d "$src" ]]; then
62
+ mkdir -p "$(dirname "$dst")"
63
+ cp -a "$src" "$dst"
64
+ ok " $(basename "$src")/"
65
+ else
66
+ warn " $(basename "$src")/ — not found, skipping"
67
+ fi
68
+ }
69
+
70
+ # ─── Main ────────────────────────────────────────────────────────
71
+ main() {
72
+ echo
73
+ log "════════════════════════════════════════════════════════════"
74
+ log " Pi Complete Backup"
75
+ log "════════════════════════════════════════════════════════════"
76
+ log "Source: $PI_ROOT"
77
+ log "Destination: $BACKUP_DIR"
78
+ echo
79
+
80
+ mkdir -p "$BACKUP_DIR"
81
+
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
184
+
185
+ # ── Manifest ──────────────────────────────────────────────────
186
+ log "Creating manifest..."
187
+ cat > "$BACKUP_DIR/MANIFEST.txt" <<EOF
188
+ Pi Complete Backup Manifest
189
+ ===========================
190
+ Created: $(date)
191
+ Pi Root: $PI_ROOT
192
+ Pi Version: $(pi --version 2>/dev/null || echo "unknown")
193
+ Host: $(hostname)
194
+ User: $(whoami)
195
+
196
+ Contents:
197
+ EOF
198
+ find "$BACKUP_DIR" -type f | sed "s|$BACKUP_DIR/||" | sort >> "$BACKUP_DIR/MANIFEST.txt"
199
+
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) ─────────
211
+ if [[ "$KEEP_UNCOMPRESSED" == "1" ]]; then
212
+ log "Keeping uncompressed staging dir: $BACKUP_DIR"
213
+ else
214
+ rm -rf "$BACKUP_DIR"
215
+ fi
216
+
217
+ # ── Summary ───────────────────────────────────────────────────
218
+ echo
219
+ log "════════════════════════════════════════════════════════════"
220
+ ok "Backup complete!"
221
+ log "Archive: $ARCHIVE"
222
+ log "Size: $(du -h "$ARCHIVE" | cut -f1)"
223
+ [[ "$KEEP_UNCOMPRESSED" == "1" ]] && log "Staging: $BACKUP_DIR"
224
+ echo
225
+ log "To restore after reinstall:"
226
+ log " ~/projects/personal/pi-backup/restore-pi.sh $ARCHIVE"
227
+ echo
228
+ }
229
+
230
+ main "$@"