清单格式:两种写法(软件名 / 手写目录)都支持 :+ 追加与 :- 排除;名录改用对象数组并逐条介绍目录

需求
- 支持两种条目写法:1) 直接写软件名录里的软件名;2) 用户手写目录。
- 两种写法都必须支持追加(:+)与排除(:-)。
- 软件名录要改进:scoop 合并成"一个软件 + 一个目录数组"。
- 运行时要把"分别是哪些目录、每个目录是干什么的、排除/追加的理由"讲清楚。

实现
- 名录(SoftwareCatalog.psd1 / Get-SoftwareCatalog)
  * 一个软件挂多个目录时写成**对象数组**:@{ Path = '...'; Description = '...' };
    也接受纯字符串数组与旧的 @{ Dirs = ... } / @{ Variants = ... }。
  * 目录说明(Description)一路带到运行日志里。
  * "声明了但当前不存在"的目录不再被丢掉:备份跳过,恢复仍然知道它该回到哪个位置。
  * scoop 合并成一个数组条目(%UserProfile%\scoop\persist + %UserProfile%\.config\scoop);
    ScoopApps-persist 保持独立条目 —— 它和前者末级名同为 persist,并进同一个归档会在包里撞名。
- 解析与解析结果(ConvertFrom-BackupListLine / Resolve-BackupEntry)
  * `:+` 以前只对"软件名且能解析出目录"的写法生效,**手写目录的 :+ 会被整段丢掉**;
    现在统一生效,且 :+ 后面写软件名会按名录展开。
  * 行尾 `# 说明` 解析成 Comment,运行时打印。
- 归档与恢复
  * 同一条目里两个同名目录:打包前明确报错(退出码 1),不再静默混成一棵树。
    (7z 命令行没有"入库改名"的能力,归档内顶层名只能是文件系统上的那个名字。)
  * 恢复时每个源只解出**它自己那棵子树**,不会再往别的父目录里复制兄弟目录。
- 可解释性
  * 新增 Write-BackupEntryPlan:打包前打印条目的目录(含来源与介绍)以及排除/追加的出处;
    Restore.ps1 同样打印"哪棵子树还原到哪、会新建还是覆盖"。
- 孤儿归档审计修正:判据只看当前清单,不再把 manifest 的历史记录当成"已知"。
  否则"条目被合并/改名后留下的旧归档"会被历史记录遮住,永远不会报警。

验证
- Pester 84 项、零依赖单元 49 项、端到端 23 项,全部通过。
- 真实机器:合并后的 scoop.7z 233 MB / 29478 项 / 7z t 通过,manifest.roots=[persist|scoop];
  恢复演练 26982/26982 逐字节一致(.ssh、legendary、Aria 同批通过)。
- 迁移提醒:scoop-config.7z 与 scoop-persist.7z 已无清单条目指向,会出现在孤儿审计里;
  确认 scoop.7z 无误后可以自行删除。
This commit is contained in:
Shuery committed 2026-09-22 08:18:16 +08:00
1 parent e114cae8c8
commit 43fa4e52dd
10 files changed
+1083 -339

No files matched your search

+57 -29
View File
@@ -211,29 +211,40 @@ foreach ($name in $Entries) {
$rows += [pscustomobject]@{ Entry = $name; Status = 'SKIP'; Detail = '名录解析不出源路径' }
continue
}
if ($sources.Count -gt 1) {
# 多目录条目恢复时会整包解压到每个位置,逐个对拍意义不大,默认不演练
$rows += [pscustomobject]@{ Entry = $name; Status = 'SKIP'; Detail = "多目录条目($($sources.Count) 个源),不在演练范围内" }
if (@($sources | Where-Object { Test-Path -LiteralPath $_.SourcePath }).Count -eq 0) {
$rows += [pscustomobject]@{ Entry = $name; Status = 'SKIP'; Detail = '所有源目录当前都不存在,无法对拍' }
continue
}
$liveSource = $sources[0].SourcePath
if (-not (Test-Path -LiteralPath $liveSource)) {
$rows += [pscustomobject]@{ Entry = $name; Status = 'SKIP'; Detail = "活源不存在,无法对拍:$liveSource" }
continue
$entryRoot = Join-Path (Join-Path $WorkRoot 'restore') $name
New-Item -ItemType Directory -Path $entryRoot -Force | Out-Null
# 每个源各自映射到一个临时目标:临时名录保持**同样的个数与顺序**,
# 于是 Restore 会把第 i 个源还原到第 i 个临时目录,再和第 i 个活源逐字节对拍。
# (一个条目可以挂多个目录:软件名录的数组写法、以及清单里的 :+ 追加。)
$scratchEntries = @()
$pairs = @()
for ($index = 0; $index -lt $sources.Count; $index++) {
$source = $sources[$index]
$leaf = @($source.RelativePaths)[0]
$scratchTarget = Join-Path (Join-Path $entryRoot $index) $leaf
$scratchEntries += $scratchTarget
$pairs += [pscustomobject]@{
Restored = $scratchTarget
Live = $source.SourcePath
Exists = (Test-Path -LiteralPath $source.SourcePath)
}
}
$leaf = Split-Path -Path $liveSource -Leaf
$restoreParent = Join-Path (Join-Path $WorkRoot 'restore') $name
$scratchTarget = Join-Path $restoreParent $leaf
New-Item -ItemType Directory -Path $restoreParent -Force | Out-Null
# 临时名录:把这个软件名指到临时目标,Restore 就会解到这里,碰不到真实目录
# 临时名录:把这些目录全指到临时目标,Restore 就解到这里,碰不到真实目录
$scratchCatalog = Join-Path $WorkRoot ("catalog-$name.psd1")
$scratchList = Join-Path $WorkRoot ("list-$name.txt")
$scratchConfig = Join-Path $WorkRoot ("config-$name.psd1")
[System.IO.File]::WriteAllText($scratchCatalog, "@{`n '$name' = '$scratchTarget'`n}`n", [System.Text.UTF8Encoding]::new($false))
$itemLines = @($scratchEntries | ForEach-Object { " @{ Path = '$_' }" }) -join "`n"
[System.IO.File]::WriteAllText($scratchCatalog,
"@{`n '$name' = @(`n$itemLines`n )`n}`n", [System.Text.UTF8Encoding]::new($false))
[System.IO.File]::WriteAllText($scratchList, "$name`n", [System.Text.UTF8Encoding]::new($false))
[System.IO.File]::WriteAllText($scratchConfig, @"
@{
@@ -245,7 +256,7 @@ foreach ($name in $Entries) {
}
"@, [System.Text.UTF8Encoding]::new($false))
Write-Host ("-- 演练 {0}(归档 {1}.7z)" -f $name, $resolved.BaseName) -ForegroundColor Gray
Write-Host ("-- 演练 {0}(归档 {1}.7z,{2} 个目录)" -f $name, $resolved.BaseName, $sources.Count) -ForegroundColor Gray
# 用**子进程**跑 Restore.ps1:它结尾会 exit,子进程既不会打断演练,
# 给出的也是真正的进程退出码(和 Pester 套件里的做法一致)。
@@ -270,37 +281,54 @@ foreach ($name in $Entries) {
}
$checked++
$report = Compare-RestoredTree -RestoredPath $scratchTarget -LivePath $liveSource -ArchiveTime $archiveTime
$matched = 0
$restoredCount = 0
$extra = @()
$stale = @()
$changed = @()
$notArchived = @()
$detail = "对拍 $($report.Matched)/$($report.Restored)"
foreach ($pair in $pairs) {
# 活源本来就没了的不对拍(归档里也不该有它)
if (-not $pair.Exists) { continue }
$one = Compare-RestoredTree -RestoredPath $pair.Restored -LivePath $pair.Live -ArchiveTime $archiveTime
$matched += $one.Matched
$restoredCount += $one.Restored
$extra += $one.Extra
$stale += $one.Stale
$changed += $one.Changed
$notArchived += $one.Missing
}
$detail = "对拍 $matched/$restoredCount"
$status = 'PASS'
if ($report.Extra.Count -gt 0) {
if ($extra.Count -gt 0) {
$status = 'FAIL'
$detail += ";归档里有源里没有的 $($report.Extra.Count) 个文件"
$failures += "$name :恢复出源里没有的文件(首例 $($report.Extra[0]))"
$detail += ";归档里有源里没有的 $($extra.Count) 个文件"
$failures += "$name :恢复出源里没有的文件(首例 $($extra[0]))"
}
if ($report.Stale.Count -gt 0) {
if ($stale.Count -gt 0) {
# 活源在归档之后被改过:源变了,不是归档坏了,只提示
$detail += ";源在备份后变过 $($report.Stale.Count) 个(不算失败)"
$detail += ";源在备份后变过 $($stale.Count) 个(不算失败)"
}
if ($report.Changed.Count -gt 0) {
if ($changed.Count -gt 0) {
if ($AllowChanged) {
$detail += ";与活源不一致 $($report.Changed.Count) 个(-AllowChanged,已容忍)"
$detail += ";与活源不一致 $($changed.Count) 个(-AllowChanged,已容忍)"
} else {
$status = 'FAIL'
$detail += ";与活源不一致 $($report.Changed.Count) 个(首例 $($report.Changed[0]))"
$failures += "$name :与活源不一致(首例 $($report.Changed[0]))"
$detail += ";与活源不一致 $($changed.Count) 个(首例 $($changed[0]))"
$failures += "$name :与活源不一致(首例 $($changed[0]))"
}
}
if (($report.Matched + $report.Stale.Count) -eq 0) {
if (($matched + $stale.Count) -eq 0) {
$status = 'FAIL'
$detail += ";没有任何文件能对上(多半是空归档)"
$failures += "$name :恢复出 0 个可对拍的文件"
}
if ($report.Missing.Count -gt 0) {
if ($notArchived.Count -gt 0) {
# 排除规则命中的文件、以及备份之后新增的文件都会落在这里,只是提示
$detail += ";活源另有 $($report.Missing.Count) 个文件不在归档里(排除规则/备份后新增)"
$detail += ";活源另有 $($notArchived.Count) 个文件不在归档里(排除规则/备份后新增)"
}
$rows += [pscustomobject]@{ Entry = $name; Status = $status; Detail = $detail }