@jamiefutch/pi-backup 1.1.11 → 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/README.md +69 -18
- package/backup-manifest.txt +61 -0
- package/backup-pi.ps1 +169 -116
- package/backup-pi.sh +164 -134
- package/extensions/pi-backup-manager.ts +232 -80
- package/package.json +6 -4
- package/pi-clean-reinstall.ps1 +79 -17
- package/pi-clean-reinstall.sh +81 -26
- package/restore-pi.ps1 +139 -101
- package/restore-pi.sh +98 -104
package/pi-clean-reinstall.ps1
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
# =============================================================
|
|
2
2
|
# Pi Clean Reinstall Script (Windows PowerShell)
|
|
3
|
-
#
|
|
4
|
-
# then reinstalls Pi and creates a fresh ~/.pi structure.
|
|
3
|
+
# Removes ALL user config, skills, extensions, and state, then reinstalls Pi.
|
|
5
4
|
# Run backup-pi.ps1 FIRST if you want to restore afterward.
|
|
6
5
|
#
|
|
6
|
+
# Safety: ~/.pi is moved aside first and only deleted after the reinstall
|
|
7
|
+
# succeeds. If uninstall/install/verify fails, the original is moved back.
|
|
8
|
+
#
|
|
7
9
|
# Usage:
|
|
8
10
|
# .\pi-clean-reinstall.ps1 # default (bun)
|
|
9
11
|
# $env:PKG_MANAGER='npm'; .\pi-clean-reinstall.ps1
|
|
12
|
+
# $env:KEEP_OLD='1'; .\pi-clean-reinstall.ps1 # keep the moved-aside copy
|
|
10
13
|
# =============================================================
|
|
11
14
|
[CmdletBinding()]
|
|
12
15
|
param()
|
|
@@ -15,14 +18,31 @@ $ErrorActionPreference = 'Stop'
|
|
|
15
18
|
|
|
16
19
|
# ─── Config ────────────────────────────────────────────────
|
|
17
20
|
$PiRoot = if ($env:PI_ROOT) { $env:PI_ROOT } else { Join-Path $HOME '.pi' }
|
|
21
|
+
$BackupRoot = if ($env:BACKUP_ROOT) { $env:BACKUP_ROOT } else { Join-Path $HOME 'pi-backups' }
|
|
18
22
|
$Package = '@earendil-works/pi-coding-agent'
|
|
19
23
|
$PkgManager = if ($env:PKG_MANAGER) { $env:PKG_MANAGER } else { 'bun' }
|
|
24
|
+
$KeepOld = $env:KEEP_OLD -eq '1'
|
|
25
|
+
$OldRoot = "$PiRoot.pre-reinstall-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
|
|
26
|
+
$Moved = $false
|
|
27
|
+
$Finished = $false
|
|
20
28
|
|
|
21
29
|
function Log { Write-Host "[INFO] $args" -ForegroundColor Cyan }
|
|
22
30
|
function OK { Write-Host "[OK] $args" -ForegroundColor Green }
|
|
23
31
|
function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
|
|
24
32
|
function Err { Write-Host "[ERR] $args" -ForegroundColor Red; exit 1 }
|
|
25
33
|
|
|
34
|
+
# Read one confirmation line; exits (without changing anything) when stdin cannot be read.
|
|
35
|
+
function Read-Confirmation([string]$prompt) {
|
|
36
|
+
$reply = $null
|
|
37
|
+
try { $reply = Read-Host $prompt } catch { $reply = $null }
|
|
38
|
+
if ($null -eq $reply) {
|
|
39
|
+
Write-Host "[ERR] Cannot read a confirmation (stdin is not interactive). Aborted; nothing was changed." -ForegroundColor Red
|
|
40
|
+
exit 1
|
|
41
|
+
}
|
|
42
|
+
return $reply
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
try {
|
|
26
46
|
# ─── Preflight ─────────────────────────────────────────────
|
|
27
47
|
Write-Host ""
|
|
28
48
|
Log "================================================================"
|
|
@@ -31,49 +51,91 @@ Log "================================================================"
|
|
|
31
51
|
Log "Target: $PiRoot"
|
|
32
52
|
Log "Package: $Package"
|
|
33
53
|
Log "Manager: $PkgManager"
|
|
54
|
+
Log "Backups in: $BackupRoot"
|
|
34
55
|
Write-Host ""
|
|
35
56
|
|
|
36
|
-
if (-not (Test-Path -LiteralPath $PiRoot
|
|
37
|
-
Warn "$PiRoot does not exist -
|
|
57
|
+
if (-not (Test-Path -LiteralPath $PiRoot)) {
|
|
58
|
+
Warn "$PiRoot does not exist - nothing to move aside."
|
|
38
59
|
}
|
|
39
60
|
|
|
40
|
-
Write-Host ""
|
|
41
61
|
Warn "This will DELETE:"
|
|
42
62
|
Warn " $PiRoot (ALL config, skills, extensions, memory, sessions)"
|
|
43
63
|
Warn " global install of $Package"
|
|
64
|
+
Warn "($PiRoot is only deleted after the reinstall succeeds.)"
|
|
44
65
|
Write-Host ""
|
|
45
|
-
|
|
66
|
+
|
|
67
|
+
$hasBackup = $false
|
|
68
|
+
if (Test-Path -LiteralPath $BackupRoot) {
|
|
69
|
+
$hasBackup = [bool](Get-ChildItem -LiteralPath $BackupRoot -Filter 'pi-backup-*' -ErrorAction SilentlyContinue | Select-Object -First 1)
|
|
70
|
+
}
|
|
71
|
+
if (-not $hasBackup) {
|
|
72
|
+
Warn "No backup found in $BackupRoot (looked for pi-backup-*)."
|
|
73
|
+
Warn "Run .\backup-pi.ps1 first unless you really want to lose your config."
|
|
74
|
+
$reply = Read-Confirmation "Type 'no backup' to continue WITHOUT a backup"
|
|
75
|
+
if ($reply -ne 'no backup') { Err "Aborted." }
|
|
76
|
+
Write-Host ""
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
$confirm = Read-Confirmation "Type 'yes' to continue"
|
|
46
80
|
if ($confirm -ne 'yes') { Err "Aborted." }
|
|
47
81
|
Write-Host ""
|
|
48
82
|
|
|
49
|
-
# ── 1.
|
|
50
|
-
Log "1/
|
|
51
|
-
if (Test-Path -LiteralPath $PiRoot) {
|
|
52
|
-
|
|
83
|
+
# ── 1. Move $PiRoot aside ─────────────────────────────────
|
|
84
|
+
Log "1/5 Moving $PiRoot aside ..."
|
|
85
|
+
if (Test-Path -LiteralPath $PiRoot) {
|
|
86
|
+
Move-Item -LiteralPath $PiRoot -Destination $OldRoot
|
|
87
|
+
$Moved = $true
|
|
88
|
+
OK " moved to $OldRoot"
|
|
89
|
+
} else {
|
|
90
|
+
OK " nothing to move"
|
|
91
|
+
}
|
|
53
92
|
|
|
54
93
|
# ── 2. Uninstall Pi ───────────────────────────────────────
|
|
55
|
-
Log "2/
|
|
94
|
+
Log "2/5 Uninstalling $Package via $PkgManager ..."
|
|
56
95
|
& $PkgManager remove -g $Package 2>$null
|
|
57
96
|
if ($LASTEXITCODE -ne 0) { Warn " uninstall reported an issue (may already be removed)" }
|
|
58
|
-
OK "
|
|
97
|
+
OK " uninstall step done"
|
|
59
98
|
|
|
60
99
|
# ── 3. Reinstall Pi ───────────────────────────────────────
|
|
61
|
-
Log "3/
|
|
100
|
+
Log "3/5 Installing $Package via $PkgManager ..."
|
|
62
101
|
& $PkgManager add -g $Package
|
|
63
102
|
if ($LASTEXITCODE -ne 0) { Err "Failed to install $Package via $PkgManager" }
|
|
64
103
|
OK " installed"
|
|
65
104
|
|
|
66
|
-
# ── 4.
|
|
67
|
-
Log "4/
|
|
105
|
+
# ── 4. Verify ─────────────────────────────────────────────
|
|
106
|
+
Log "4/5 Verifying the install (pi --version) ..."
|
|
68
107
|
& pi --version
|
|
69
|
-
|
|
108
|
+
if ($LASTEXITCODE -ne 0) { Err "'pi --version' failed after install" }
|
|
109
|
+
OK " pi runs (it creates a fresh $PiRoot on first start)"
|
|
110
|
+
|
|
111
|
+
# ── 5. Remove the moved-aside copy ────────────────────────
|
|
112
|
+
$Finished = $true
|
|
113
|
+
if ($Moved) {
|
|
114
|
+
if ($KeepOld) {
|
|
115
|
+
Log "5/5 Keeping the old config at $OldRoot (KEEP_OLD=1)"
|
|
116
|
+
} else {
|
|
117
|
+
Log "5/5 Deleting the old config at $OldRoot ..."
|
|
118
|
+
Remove-Item -LiteralPath $OldRoot -Recurse -Force
|
|
119
|
+
OK " deleted"
|
|
120
|
+
}
|
|
121
|
+
} else {
|
|
122
|
+
Log "5/5 Nothing to clean up"
|
|
123
|
+
}
|
|
70
124
|
|
|
71
125
|
Write-Host ""
|
|
72
126
|
Log "================================================================"
|
|
73
127
|
OK "Clean reinstall complete!"
|
|
74
128
|
Write-Host ""
|
|
75
129
|
Log "Next steps:"
|
|
76
|
-
Log " 1. Restore: ./restore-pi.ps1 <backup-dir>"
|
|
130
|
+
Log " 1. Restore: ./restore-pi.ps1 <backup-dir-or-archive>"
|
|
77
131
|
Log " 2. Reinstall npm deps: cd ~/.pi/agent && npm install"
|
|
78
132
|
Log " 3. Verify: pi --version, /ctx-doctor, /loop-police-status, /scoped-memory-status"
|
|
79
133
|
Write-Host ""
|
|
134
|
+
} finally {
|
|
135
|
+
if ($Moved -and -not $Finished -and (Test-Path -LiteralPath $OldRoot)) {
|
|
136
|
+
Write-Host "[ERR] Reinstall failed. Restoring the original $PiRoot ..." -ForegroundColor Red
|
|
137
|
+
if (Test-Path -LiteralPath $PiRoot) { Remove-Item -LiteralPath $PiRoot -Recurse -Force -ErrorAction SilentlyContinue }
|
|
138
|
+
Move-Item -LiteralPath $OldRoot -Destination $PiRoot
|
|
139
|
+
Write-Host "[WARN] Original $PiRoot restored. Pi may need to be reinstalled manually: $PkgManager add -g $Package" -ForegroundColor Yellow
|
|
140
|
+
}
|
|
141
|
+
}
|
package/pi-clean-reinstall.sh
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
#!/bin/bash
|
|
2
2
|
# Pi Clean Reinstall Script
|
|
3
|
-
#
|
|
4
|
-
# then reinstalls Pi and creates a fresh ~/.pi structure.
|
|
3
|
+
# Removes ALL user config, skills, extensions, and state, then reinstalls Pi.
|
|
5
4
|
# Run backup-pi.sh FIRST if you want to restore afterward.
|
|
5
|
+
#
|
|
6
|
+
# Safety: ~/.pi is moved aside first, and only deleted after the reinstall succeeds.
|
|
7
|
+
# If the uninstall/install/verify steps fail, the original ~/.pi is moved back.
|
|
6
8
|
|
|
7
9
|
set -euo pipefail
|
|
8
10
|
|
|
9
11
|
# ─── Config ──────────────────────────────────────────────────────
|
|
10
12
|
PI_ROOT="${PI_ROOT:-$HOME/.pi}"
|
|
13
|
+
BACKUP_ROOT="${BACKUP_ROOT:-$HOME/pi-backups}"
|
|
11
14
|
PACKAGE="@earendil-works/pi-coding-agent"
|
|
12
15
|
PKG_MANAGER="${PKG_MANAGER:-bun}"
|
|
16
|
+
KEEP_OLD="${KEEP_OLD:-0}" # set to 1 to keep the moved-aside copy after a successful reinstall
|
|
17
|
+
OLD_ROOT="$PI_ROOT.pre-reinstall-$(date +%Y%m%d-%H%M%S)"
|
|
18
|
+
MOVED=0
|
|
19
|
+
FINISHED=0
|
|
13
20
|
|
|
14
21
|
# ─── Colors ──────────────────────────────────────────────────────
|
|
15
22
|
RED='\033[0;31m'
|
|
@@ -23,7 +30,34 @@ ok() { echo -e "${GREEN}[OK]${NC} $*"; }
|
|
|
23
30
|
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
|
|
24
31
|
err() { echo -e "${RED}[ERR]${NC} $*"; }
|
|
25
32
|
|
|
33
|
+
# ─── Rollback ────────────────────────────────────────────────────
|
|
34
|
+
# Runs on any non-zero exit after the original ~/.pi was moved aside.
|
|
35
|
+
on_exit() {
|
|
36
|
+
local rc=$?
|
|
37
|
+
if [[ $rc -ne 0 && "$FINISHED" != "1" && "$MOVED" == "1" && -d "$OLD_ROOT" ]]; then
|
|
38
|
+
err "Reinstall failed (exit $rc). Restoring the original $PI_ROOT ..."
|
|
39
|
+
rm -rf "$PI_ROOT"
|
|
40
|
+
mv "$OLD_ROOT" "$PI_ROOT"
|
|
41
|
+
warn "Original $PI_ROOT restored. Pi may need to be reinstalled manually: $PKG_MANAGER add -g $PACKAGE"
|
|
42
|
+
fi
|
|
43
|
+
return $rc
|
|
44
|
+
}
|
|
45
|
+
trap on_exit EXIT
|
|
46
|
+
|
|
26
47
|
# ─── Preflight ───────────────────────────────────────────────────
|
|
48
|
+
confirm() { # confirm <expected-text>; fails (exit 1) when stdin cannot be read
|
|
49
|
+
local reply=""
|
|
50
|
+
if ! read -r reply; then
|
|
51
|
+
err "Cannot read a confirmation (stdin is not interactive). Aborted; nothing was changed."
|
|
52
|
+
exit 1
|
|
53
|
+
fi
|
|
54
|
+
[[ "$reply" == "$1" ]]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
has_backup() {
|
|
58
|
+
compgen -G "$BACKUP_ROOT/pi-backup-*" >/dev/null 2>&1
|
|
59
|
+
}
|
|
60
|
+
|
|
27
61
|
preflight() {
|
|
28
62
|
echo
|
|
29
63
|
log "════════════════════════════════════════════════════════════"
|
|
@@ -32,27 +66,29 @@ preflight() {
|
|
|
32
66
|
log "Target: $PI_ROOT"
|
|
33
67
|
log "Package: $PACKAGE"
|
|
34
68
|
log "Manager: $PKG_MANAGER"
|
|
69
|
+
log "Backups in: $BACKUP_ROOT"
|
|
35
70
|
echo
|
|
36
71
|
|
|
37
|
-
if [[ -z "${BUN_HOME:-}" ]] && [[ "$PKG_MANAGER" == "bun" ]]; then
|
|
38
|
-
BUN_HOME="$HOME/.bun"
|
|
39
|
-
fi
|
|
40
|
-
|
|
41
72
|
if [[ ! -d "$PI_ROOT" ]]; then
|
|
42
|
-
warn "$PI_ROOT does not exist —
|
|
73
|
+
warn "$PI_ROOT does not exist — nothing to move aside."
|
|
43
74
|
fi
|
|
44
75
|
|
|
45
|
-
echo
|
|
46
76
|
warn "This will DELETE:"
|
|
47
77
|
warn " $PI_ROOT (ALL config, skills, extensions, memory, sessions)"
|
|
48
78
|
warn " global install of $PACKAGE"
|
|
79
|
+
warn "($PI_ROOT is only deleted after the reinstall succeeds.)"
|
|
49
80
|
echo
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
81
|
+
|
|
82
|
+
if ! has_backup; then
|
|
83
|
+
warn "No backup found in $BACKUP_ROOT (looked for pi-backup-*)."
|
|
84
|
+
warn "Run ./backup-pi.sh first unless you really want to lose your config."
|
|
85
|
+
warn "Type 'no backup' to continue WITHOUT a backup: "
|
|
86
|
+
if ! confirm "no backup"; then err "Aborted."; exit 1; fi
|
|
87
|
+
echo
|
|
55
88
|
fi
|
|
89
|
+
|
|
90
|
+
warn "Type 'yes' to continue: "
|
|
91
|
+
if ! confirm "yes"; then err "Aborted."; exit 1; fi
|
|
56
92
|
echo
|
|
57
93
|
}
|
|
58
94
|
|
|
@@ -60,32 +96,51 @@ preflight() {
|
|
|
60
96
|
main() {
|
|
61
97
|
preflight
|
|
62
98
|
|
|
63
|
-
# ── 1.
|
|
64
|
-
log "1/
|
|
65
|
-
|
|
66
|
-
|
|
99
|
+
# ── 1. Move ~/.pi aside ──────────────────────────────────────
|
|
100
|
+
log "1/5 Moving $PI_ROOT aside ..."
|
|
101
|
+
if [[ -e "$PI_ROOT" ]]; then
|
|
102
|
+
mv "$PI_ROOT" "$OLD_ROOT"
|
|
103
|
+
MOVED=1
|
|
104
|
+
ok " moved to $OLD_ROOT"
|
|
105
|
+
else
|
|
106
|
+
ok " nothing to move"
|
|
107
|
+
fi
|
|
67
108
|
|
|
68
109
|
# ── 2. Uninstall Pi ──────────────────────────────────────────
|
|
69
|
-
log "2/
|
|
110
|
+
log "2/5 Uninstalling $PACKAGE via $PKG_MANAGER ..."
|
|
70
111
|
"$PKG_MANAGER" remove -g "$PACKAGE" || warn " uninstall reported an issue (may already be removed)"
|
|
71
|
-
ok "
|
|
112
|
+
ok " uninstall step done"
|
|
72
113
|
|
|
73
114
|
# ── 3. Reinstall Pi ──────────────────────────────────────────
|
|
74
|
-
log "3/
|
|
75
|
-
"$PKG_MANAGER" add -g "$PACKAGE"
|
|
115
|
+
log "3/5 Installing $PACKAGE via $PKG_MANAGER ..."
|
|
116
|
+
"$PKG_MANAGER" add -g "$PACKAGE" || { err " install failed"; exit 1; }
|
|
76
117
|
ok " installed"
|
|
77
118
|
|
|
78
|
-
# ── 4.
|
|
79
|
-
log "4/
|
|
80
|
-
pi --version
|
|
81
|
-
ok " fresh
|
|
119
|
+
# ── 4. Verify ────────────────────────────────────────────────
|
|
120
|
+
log "4/5 Verifying the install (pi --version) ..."
|
|
121
|
+
pi --version || { err " 'pi --version' failed after install"; exit 1; }
|
|
122
|
+
ok " pi runs (it creates a fresh $PI_ROOT on first start)"
|
|
123
|
+
|
|
124
|
+
# ── 5. Remove the moved-aside copy ───────────────────────────
|
|
125
|
+
FINISHED=1
|
|
126
|
+
if [[ "$MOVED" == "1" ]]; then
|
|
127
|
+
if [[ "$KEEP_OLD" == "1" ]]; then
|
|
128
|
+
log "5/5 Keeping the old config at $OLD_ROOT (KEEP_OLD=1)"
|
|
129
|
+
else
|
|
130
|
+
log "5/5 Deleting the old config at $OLD_ROOT ..."
|
|
131
|
+
rm -rf "$OLD_ROOT"
|
|
132
|
+
ok " deleted"
|
|
133
|
+
fi
|
|
134
|
+
else
|
|
135
|
+
log "5/5 Nothing to clean up"
|
|
136
|
+
fi
|
|
82
137
|
|
|
83
138
|
echo
|
|
84
139
|
log "════════════════════════════════════════════════════════════"
|
|
85
140
|
ok "Clean reinstall complete!"
|
|
86
141
|
echo
|
|
87
142
|
log "Next steps:"
|
|
88
|
-
log " 1. Restore: ./restore-pi.sh <backup-dir>"
|
|
143
|
+
log " 1. Restore: ./restore-pi.sh <backup-dir-or-archive>"
|
|
89
144
|
log " 2. Reinstall npm deps: cd ~/.pi/agent && npm install"
|
|
90
145
|
log " 3. Verify: pi --version, /ctx-doctor, /loop-police-status, /scoped-memory-status"
|
|
91
146
|
echo
|
package/restore-pi.ps1
CHANGED
|
@@ -24,6 +24,34 @@ function OK { Write-Host "[OK] $args" -ForegroundColor Green }
|
|
|
24
24
|
function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow }
|
|
25
25
|
function Err { Write-Host "[ERR] $args" -ForegroundColor Red }
|
|
26
26
|
|
|
27
|
+
# ─── Manifest ──────────────────────────────────────────────
|
|
28
|
+
# What gets backed up / restored is defined once, in backup-manifest.txt (shared with the bash scripts).
|
|
29
|
+
$ManifestFile = Join-Path $PSScriptRoot 'backup-manifest.txt'
|
|
30
|
+
if (-not (Test-Path -LiteralPath $ManifestFile)) {
|
|
31
|
+
Write-Host "[ERR] backup-manifest.txt not found next to this script" -ForegroundColor Red
|
|
32
|
+
exit 1
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function Read-Manifest([string]$path) {
|
|
36
|
+
foreach ($line in [System.IO.File]::ReadAllLines($path)) {
|
|
37
|
+
$l = $line.TrimEnd("`r")
|
|
38
|
+
if ($l -eq '' -or $l.StartsWith('#')) { continue }
|
|
39
|
+
$p = $l.Split('|')
|
|
40
|
+
[pscustomobject]@{
|
|
41
|
+
Kind = $p[0]
|
|
42
|
+
A = if ($p.Count -gt 1) { $p[1] } else { '' }
|
|
43
|
+
B = if ($p.Count -gt 2) { $p[2] } else { '' }
|
|
44
|
+
C = if ($p.Count -gt 3) { $p[3] } else { '' }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
# Manifest paths use "/"; join them under a root with the platform separator.
|
|
50
|
+
function P([string]$root, [string]$rel) {
|
|
51
|
+
Join-Path $root ($rel.Replace('/', [System.IO.Path]::DirectorySeparatorChar))
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
27
55
|
function Show-Usage {
|
|
28
56
|
Write-Host @"
|
|
29
57
|
Usage: $($MyInvocation.MyCommand.Name) <backup-directory-or-archive>
|
|
@@ -46,8 +74,30 @@ Prerequisites:
|
|
|
46
74
|
|
|
47
75
|
if (-not $BackupArg) { Show-Usage }
|
|
48
76
|
|
|
49
|
-
|
|
77
|
+
function Test-PiRunning {
|
|
78
|
+
# Best-effort only: process names are unreliable, so callers warn rather than block.
|
|
79
|
+
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
|
|
80
|
+
if (Get-Process -Name 'pi' -ErrorAction SilentlyContinue) { return $true }
|
|
81
|
+
try {
|
|
82
|
+
$procs = Get-CimInstance Win32_Process -Filter "Name='node.exe' OR Name='bun.exe'" -ErrorAction Stop
|
|
83
|
+
if ($procs | Where-Object { $_.CommandLine -like '*pi-coding-agent*' }) { return $true }
|
|
84
|
+
} catch {}
|
|
85
|
+
return $false
|
|
86
|
+
}
|
|
87
|
+
if (Get-Command pgrep -ErrorAction SilentlyContinue) {
|
|
88
|
+
& pgrep -x pi *> $null
|
|
89
|
+
if ($LASTEXITCODE -eq 0) { return $true }
|
|
90
|
+
& pgrep -f 'pi-coding-agent' *> $null
|
|
91
|
+
return ($LASTEXITCODE -eq 0)
|
|
92
|
+
}
|
|
93
|
+
return $false
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
# The extraction dir is removed in `finally`, so every exit path (abort, error,
|
|
97
|
+
# success) cleans up. `exit` inside `try` still runs `finally`.
|
|
50
98
|
$STAGE = $null
|
|
99
|
+
try {
|
|
100
|
+
# ─── Archive handling ─────────────────────────────────────
|
|
51
101
|
$BackupDir = $null
|
|
52
102
|
$SevenZip = @('7zz', '7z', '7za') | ForEach-Object { Get-Command $_ -ErrorAction SilentlyContinue } | Select-Object -First 1
|
|
53
103
|
|
|
@@ -65,7 +115,7 @@ if (Test-Path -LiteralPath $BackupArg -PathType Container) {
|
|
|
65
115
|
Err "7zip is required to extract .7z backups. Install 7-Zip and ensure it is on PATH."
|
|
66
116
|
exit 1
|
|
67
117
|
}
|
|
68
|
-
$STAGE = Join-Path
|
|
118
|
+
$STAGE = Join-Path ([System.IO.Path]::GetTempPath()) ("pi-restore-" + [guid]::NewGuid().ToString('N'))
|
|
69
119
|
New-Item -ItemType Directory -Path $STAGE -Force | Out-Null
|
|
70
120
|
Log "Extracting archive: $BackupArg"
|
|
71
121
|
try {
|
|
@@ -77,12 +127,11 @@ if (Test-Path -LiteralPath $BackupArg -PathType Container) {
|
|
|
77
127
|
}
|
|
78
128
|
} catch {
|
|
79
129
|
Err "Failed to extract archive: $($_.Exception.Message)"
|
|
80
|
-
Remove-Item -LiteralPath $STAGE -Recurse -Force -ErrorAction SilentlyContinue
|
|
81
130
|
exit 1
|
|
82
131
|
}
|
|
83
132
|
# The archive holds one top-level pi-backup-* dir
|
|
84
133
|
$top = Get-ChildItem -LiteralPath $STAGE -Directory | Select-Object -First 1
|
|
85
|
-
if (-not $top) { Err "No backup directory found inside archive.";
|
|
134
|
+
if (-not $top) { Err "No backup directory found inside archive."; exit 1 }
|
|
86
135
|
$BackupDir = $top.FullName
|
|
87
136
|
Log "Extracted to: $BackupDir"
|
|
88
137
|
} else {
|
|
@@ -104,19 +153,38 @@ Warn "This will OVERWRITE all current Pi configuration."
|
|
|
104
153
|
Warn "Backup source: $BackupDir"
|
|
105
154
|
Warn "Target: $PiRoot"
|
|
106
155
|
Warn "Pi must NOT be running."
|
|
156
|
+
if (Test-PiRunning) {
|
|
157
|
+
Warn "A Pi process appears to be running. Exit Pi first: files restored under a"
|
|
158
|
+
Warn "running Pi can be overwritten or corrupted when it exits."
|
|
159
|
+
}
|
|
107
160
|
Write-Host ""
|
|
108
|
-
$
|
|
109
|
-
|
|
161
|
+
if ($env:PI_BACKUP_ASSUME_YES -eq '1') {
|
|
162
|
+
Log "PI_BACKUP_ASSUME_YES=1 set; skipping confirmation."
|
|
163
|
+
} else {
|
|
164
|
+
$confirm = $null
|
|
165
|
+
try { $confirm = Read-Host "Continue? [y/N] " } catch { $confirm = $null }
|
|
166
|
+
if ($null -eq $confirm) {
|
|
167
|
+
Err "Cannot read a confirmation (stdin is not interactive)."
|
|
168
|
+
Err "Run this in a terminal, or set PI_BACKUP_ASSUME_YES=1 to skip the prompt."
|
|
169
|
+
exit 1
|
|
170
|
+
}
|
|
171
|
+
if ($confirm -notmatch '^[Yy]$') { Log "Aborted."; exit 0 }
|
|
172
|
+
}
|
|
110
173
|
|
|
111
174
|
# ─── Helpers ──────────────────────────────────────────────
|
|
175
|
+
function Get-RelativeLabel([string]$path) {
|
|
176
|
+
if ($BackupDir -and $path.StartsWith($BackupDir)) { return $path.Substring($BackupDir.Length).TrimStart('\','/').Replace('\','/') }
|
|
177
|
+
return [System.IO.Path]::GetFileName($path)
|
|
178
|
+
}
|
|
179
|
+
|
|
112
180
|
function Restore-File([string]$src, [string]$dst) {
|
|
113
181
|
if (Test-Path -LiteralPath $src -PathType Leaf) {
|
|
114
182
|
$dstDir = Split-Path $dst -Parent
|
|
115
183
|
if (!(Test-Path -LiteralPath $dstDir)) { New-Item -ItemType Directory -Path $dstDir -Force | Out-Null }
|
|
116
184
|
Copy-Item -LiteralPath $src -Destination $dst -Force
|
|
117
|
-
OK " $(
|
|
185
|
+
OK " $(Get-RelativeLabel $src)"
|
|
118
186
|
} else {
|
|
119
|
-
Warn " $(
|
|
187
|
+
Warn " $(Get-RelativeLabel $src) - not in backup, skipping"
|
|
120
188
|
}
|
|
121
189
|
}
|
|
122
190
|
|
|
@@ -141,12 +209,17 @@ function Copy-Tree([string]$src, [string]$dst, [string[]]$excludeDirs = @(), [st
|
|
|
141
209
|
$robocopyArgs += @('/NFL','/NDL','/NJH','/NJS')
|
|
142
210
|
& robocopy @robocopyArgs | Out-Null
|
|
143
211
|
} else {
|
|
144
|
-
|
|
212
|
+
# Fallback without robocopy: recurse so existing destination dirs are merged,
|
|
213
|
+
# not nested (Copy-Item of a dir onto an existing dir would create dst/dir/dir).
|
|
214
|
+
Get-ChildItem -LiteralPath $src -Force | ForEach-Object {
|
|
215
|
+
$item = $_
|
|
145
216
|
$skip = $false
|
|
146
|
-
if ($
|
|
147
|
-
if (-not $
|
|
217
|
+
if ($item.PSIsContainer -and $item.Name -in $excludeDirs) { $skip = $true }
|
|
218
|
+
if (-not $item.PSIsContainer -and ($excludeFiles | Where-Object { $item.Name -like $_ })) { $skip = $true }
|
|
148
219
|
if (-not $skip) {
|
|
149
|
-
|
|
220
|
+
$target = Join-Path $dst $item.Name
|
|
221
|
+
if ($item.PSIsContainer) { Copy-Tree $item.FullName $target $excludeDirs $excludeFiles }
|
|
222
|
+
else { Copy-Item -LiteralPath $item.FullName -Destination $target -Force }
|
|
150
223
|
}
|
|
151
224
|
}
|
|
152
225
|
}
|
|
@@ -159,93 +232,58 @@ Log " Restoring Pi from backup"
|
|
|
159
232
|
Log "================================================================"
|
|
160
233
|
Write-Host ""
|
|
161
234
|
|
|
162
|
-
# ── 1. Core Config Files ──────────────────────────────────
|
|
163
|
-
Log "1/8 Core configuration files..."
|
|
164
235
|
$agentDir = Join-Path $PiRoot 'agent'
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
Restore-File
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
Restore-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
Restore-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
$
|
|
194
|
-
if (Test-Path -LiteralPath $
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
# ── 6. npm Extension Manifests ────────────────────────────
|
|
216
|
-
Log "6/8 npm extension manifests..."
|
|
217
|
-
$npmSrc = Join-Path $BackupDir 'npm-manifests'
|
|
218
|
-
if (Test-Path -LiteralPath $npmSrc -PathType Container) {
|
|
219
|
-
$npmDst = Join-Path $agentDir 'npm'
|
|
220
|
-
New-Item -ItemType Directory -Path $npmDst -Force | Out-Null
|
|
221
|
-
Copy-Tree $npmSrc $npmDst
|
|
222
|
-
OK " npm manifests restored (run 'npm install' to reinstall deps)"
|
|
223
|
-
} else {
|
|
224
|
-
Warn " No npm manifests in backup"
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
# ── 7. Hermes Memory ──────────────────────────────────────
|
|
228
|
-
Log "7/8 Hermes memory (active files)..."
|
|
229
|
-
$hermesSrc = Join-Path $BackupDir 'pi-hermes-memory'
|
|
230
|
-
if (Test-Path -LiteralPath $hermesSrc -PathType Container) {
|
|
231
|
-
$hermesDst = Join-Path $agentDir 'pi-hermes-memory'
|
|
232
|
-
New-Item -ItemType Directory -Path $hermesDst -Force | Out-Null
|
|
233
|
-
foreach ($f in @('MEMORY.md','USER.md','failures.md','retired-failures.md','retired-memory.md')) {
|
|
234
|
-
Restore-File (Join-Path $hermesSrc $f) (Join-Path $hermesDst $f)
|
|
235
|
-
}
|
|
236
|
-
} else {
|
|
237
|
-
Warn " No Hermes memory in backup"
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
# ── 8. Hermes-cleanup Backups ─────────────────────────────
|
|
241
|
-
Log "8/8 Hermes-cleanup backups..."
|
|
242
|
-
$cleanupSrc = Join-Path $BackupDir 'hermes-cleanup'
|
|
243
|
-
if (Test-Path -LiteralPath $cleanupSrc -PathType Container) {
|
|
244
|
-
foreach ($d in Get-ChildItem -LiteralPath $cleanupSrc -Directory) {
|
|
245
|
-
Restore-Dir $d.FullName (Join-Path $agentDir "hermes-cleanup\$($d.Name)")
|
|
236
|
+
$entries = @(Read-Manifest $ManifestFile)
|
|
237
|
+
$total = @($entries | Where-Object { $_.Kind -eq 'section' }).Count
|
|
238
|
+
$step = 0
|
|
239
|
+
foreach ($e in $entries) {
|
|
240
|
+
switch ($e.Kind) {
|
|
241
|
+
'section' { $step++; Log "$step/$total $($e.A)..." }
|
|
242
|
+
'file' {
|
|
243
|
+
$dst = P $PiRoot $e.A
|
|
244
|
+
$src = P $BackupDir $e.B
|
|
245
|
+
Restore-File $src $dst
|
|
246
|
+
# Credentials: keep a restored auth.json private to this user (Unix)
|
|
247
|
+
if ($e.B -eq 'auth.json' -and (Test-Path -LiteralPath $src) -and (Test-Path -LiteralPath $dst) -and -not ($IsWindows -or $env:OS -eq 'Windows_NT')) {
|
|
248
|
+
try { [System.IO.File]::SetUnixFileMode($dst, [System.IO.UnixFileMode]'UserRead,UserWrite') } catch { Warn " could not restrict permissions on auth.json: $($_.Exception.Message)" }
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
'dir' { Restore-Dir (P $BackupDir $e.B) (P $PiRoot $e.A) }
|
|
252
|
+
{ $_ -in 'dir-each','recent' } {
|
|
253
|
+
$src = P $BackupDir $e.B
|
|
254
|
+
if (Test-Path -LiteralPath $src -PathType Container) {
|
|
255
|
+
foreach ($sub in Get-ChildItem -LiteralPath $src -Directory) {
|
|
256
|
+
Restore-Dir $sub.FullName (Join-Path (P $PiRoot $e.A) $sub.Name)
|
|
257
|
+
}
|
|
258
|
+
OK " $($e.B)/ restored"
|
|
259
|
+
} else {
|
|
260
|
+
Warn " No $($e.B) in backup"
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
'local-ext' {
|
|
264
|
+
$src = P $BackupDir "local-extensions/$($e.A)"
|
|
265
|
+
if (Test-Path -LiteralPath $src -PathType Container) {
|
|
266
|
+
$dst = Join-Path $LocalExtRoot $e.A
|
|
267
|
+
New-Item -ItemType Directory -Path $dst -Force | Out-Null
|
|
268
|
+
Copy-Tree $src $dst
|
|
269
|
+
OK " $($e.A)/ restored"
|
|
270
|
+
} else {
|
|
271
|
+
Warn " $($e.A) - not in backup, skipping"
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
'npm' {
|
|
275
|
+
$src = P $BackupDir $e.B
|
|
276
|
+
if (Test-Path -LiteralPath $src -PathType Container) {
|
|
277
|
+
$dst = P $PiRoot $e.A
|
|
278
|
+
New-Item -ItemType Directory -Path $dst -Force | Out-Null
|
|
279
|
+
Copy-Tree $src $dst
|
|
280
|
+
OK " npm manifests restored (run 'npm install' to reinstall deps)"
|
|
281
|
+
} else {
|
|
282
|
+
Warn " No npm manifests in backup"
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
default { Warn "Unknown manifest entry type: $($e.Kind)" }
|
|
246
286
|
}
|
|
247
|
-
} else {
|
|
248
|
-
Warn " No cleanup backups in backup"
|
|
249
287
|
}
|
|
250
288
|
|
|
251
289
|
# ── Verify ────────────────────────────────────────────────
|
|
@@ -264,8 +302,8 @@ Log " /scoped-memory-status"
|
|
|
264
302
|
Log " 3. Restart Pi and verify everything works"
|
|
265
303
|
Write-Host ""
|
|
266
304
|
|
|
267
|
-
|
|
268
|
-
if ($STAGE -and (Test-Path -LiteralPath $STAGE)) {
|
|
269
|
-
|
|
270
|
-
|
|
305
|
+
} finally {
|
|
306
|
+
if ($STAGE -and (Test-Path -LiteralPath $STAGE)) {
|
|
307
|
+
Remove-Item -LiteralPath $STAGE -Recurse -Force -ErrorAction SilentlyContinue
|
|
308
|
+
}
|
|
271
309
|
}
|