133 lines
6.3 KiB
PowerShell
133 lines
6.3 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Backup D:\Oculusquest\Projekt -> \\MYCLOUD-NESYFV\Michael\Projekte, in two steps:
|
|
1. check: which files are new/changed (missing or outdated in the backup)
|
|
2. copy exactly those (after asking, unless -Auto)
|
|
|
|
Nothing is ever deleted in the backup. Files that exist only in the backup
|
|
(deleted or renamed locally) are listed and kept.
|
|
|
|
Not backed up (regenerable): node_modules, __pycache__, .gradle, .venv, venv, .venv-tts
|
|
|
|
.EXAMPLE
|
|
.\backup-projekte.ps1 check, ask, copy
|
|
.\backup-projekte.ps1 -Test only check
|
|
.\backup-projekte.ps1 -Auto check and copy without asking (task scheduler)
|
|
#>
|
|
param(
|
|
[string]$Source,
|
|
[string]$Target,
|
|
[switch]$Test,
|
|
[switch]$Auto
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
# Same settings as the backup in the project dashboard: backup-config.json
|
|
$cfg = Get-Content (Join-Path $PSScriptRoot 'backup-config.json') -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
if (-not $Source) { $Source = $cfg.source }
|
|
if (-not $Target) { $Target = $cfg.target }
|
|
$logDir = Join-Path $PSScriptRoot 'logs'
|
|
New-Item -ItemType Directory -Force $logDir | Out-Null
|
|
$stamp = Get-Date -Format 'yyyy-MM-dd_HH-mm'
|
|
$checkLog = Join-Path $logDir "pruefung-$stamp.log"
|
|
$copyLog = Join-Path $logDir "backup-$stamp.log"
|
|
|
|
$common = @('/E', '/FFT', '/XJ', '/R:2', '/W:5', '/COPY:DAT', '/DCOPY:T', '/XD') + @($cfg.excludeDirs) + @('/XF') + @($cfg.excludeFiles)
|
|
# /FFT: NAS timestamps have 2 s precision - without it every file would look changed
|
|
|
|
function Format-Size([double]$b) {
|
|
if ($b -ge 1GB) { return ('{0:N1} GB' -f ($b / 1GB)) }
|
|
if ($b -ge 1MB) { return ('{0:N0} MB' -f ($b / 1MB)) }
|
|
return ('{0:N0} KB' -f ($b / 1KB))
|
|
}
|
|
|
|
function Stop-Here([int]$code) {
|
|
if (-not $Auto) { Write-Host ''; Read-Host 'Enter zum Schliessen' | Out-Null }
|
|
exit $code
|
|
}
|
|
|
|
Write-Host ''
|
|
Write-Host " Backup: $Source" -ForegroundColor Cyan
|
|
Write-Host " nach: $Target" -ForegroundColor Cyan
|
|
Write-Host ''
|
|
|
|
if (-not (Test-Path -LiteralPath $Source)) { Write-Host " FEHLER: Quelle $Source fehlt." -ForegroundColor Red; Stop-Here 1 }
|
|
if (-not (Test-Path -LiteralPath $Target)) {
|
|
$share = Split-Path $Target -Parent
|
|
if (-not (Test-Path -LiteralPath $share)) {
|
|
Write-Host " FEHLER: MyCloud nicht erreichbar ($share). Eingeschaltet und im Netzwerk?" -ForegroundColor Red
|
|
Stop-Here 1
|
|
}
|
|
New-Item -ItemType Directory $Target | Out-Null
|
|
}
|
|
|
|
# ── 1. check ───────────────────────────────────────────────────────────────
|
|
Write-Host ' 1/2 Pruefe, was neu ist und im Backup fehlt ...' -ForegroundColor Yellow
|
|
# /L = list only, /X = also report files that exist only in the backup
|
|
& robocopy $Source $Target @common /L /X /NJH /NJS /NDL /NP /FP /BYTES /UNILOG:$checkLog | Out-Null
|
|
if ($LASTEXITCODE -ge 8) { Write-Host " FEHLER bei der Pruefung (robocopy $LASTEXITCODE). Details: $checkLog" -ForegroundColor Red; Stop-Here 1 }
|
|
|
|
$copy = New-Object System.Collections.Generic.List[object]
|
|
$extra = New-Object System.Collections.Generic.List[object]
|
|
foreach ($line in [IO.File]::ReadAllLines($checkLog)) {
|
|
$parts = $line.Split("`t") | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' }
|
|
if ($parts.Count -lt 3) { continue }
|
|
$path = $parts[-1]; $size = 0
|
|
if (-not [int64]::TryParse($parts[-2], [ref]$size)) { continue }
|
|
$isExtra = $parts[0] -like '*EXTRA*'
|
|
$base = if ($isExtra) { $Target } else { $Source }
|
|
if (-not $path.StartsWith($base, [StringComparison]::OrdinalIgnoreCase)) { continue }
|
|
$rel = $path.Substring($base.Length).TrimStart('\')
|
|
# robocopy's class column: "Neue Datei"/"New File" = missing in the backup, otherwise newer/changed
|
|
$item = [pscustomobject]@{ Project = ($rel -split '\\')[0]; Rel = $rel; Size = $size; New = $parts[0] -match '^(Neue Datei|New File)$' }
|
|
if ($isExtra) { $extra.Add($item) } else { $copy.Add($item) }
|
|
}
|
|
|
|
$newFiles = @($copy | Where-Object New)
|
|
$changed = @($copy | Where-Object { -not $_.New })
|
|
$total = ($copy | Measure-Object Size -Sum).Sum
|
|
|
|
Write-Host ''
|
|
if ($copy.Count -eq 0) {
|
|
Write-Host ' Alles aktuell - im Backup fehlt nichts.' -ForegroundColor Green
|
|
} else {
|
|
Write-Host (" Zu kopieren: {0:N0} Dateien, {1}" -f $copy.Count, (Format-Size $total)) -ForegroundColor White
|
|
Write-Host (" neu (fehlen im Backup): {0,8:N0} {1}" -f $newFiles.Count, (Format-Size (($newFiles | Measure-Object Size -Sum).Sum)))
|
|
Write-Host (" geaendert: {0,8:N0} {1}" -f $changed.Count, (Format-Size (($changed | Measure-Object Size -Sum).Sum)))
|
|
Write-Host ''
|
|
Write-Host ' Nach Projekt:'
|
|
$copy | Group-Object Project | ForEach-Object {
|
|
[pscustomobject]@{ Name = $_.Name; Files = $_.Count; Bytes = ($_.Group | Measure-Object Size -Sum).Sum }
|
|
} | Sort-Object Bytes -Descending | Select-Object -First 15 | ForEach-Object {
|
|
Write-Host (" {0,-28} {1,8:N0} Dateien {2,10}" -f $_.Name, $_.Files, (Format-Size $_.Bytes))
|
|
}
|
|
}
|
|
if ($extra.Count) {
|
|
Write-Host ''
|
|
Write-Host (" Nur noch im Backup (hier geloescht/umbenannt): {0:N0} Dateien, {1} - bleiben erhalten." -f $extra.Count, (Format-Size (($extra | Measure-Object Size -Sum).Sum))) -ForegroundColor DarkGray
|
|
}
|
|
Write-Host ''
|
|
Write-Host " Vollstaendige Liste: $checkLog" -ForegroundColor DarkGray
|
|
|
|
if ($copy.Count -eq 0) { Stop-Here 0 }
|
|
if ($Test) { Write-Host ''; Write-Host ' Testlauf - es wurde nichts kopiert.' -ForegroundColor Yellow; Stop-Here 0 }
|
|
|
|
if (-not $Auto) {
|
|
Write-Host ''
|
|
$answer = Read-Host ' Jetzt kopieren? (J/N)'
|
|
if ($answer -notmatch '^(j|ja|y|yes)$') { Write-Host ' Abgebrochen, nichts kopiert.'; Stop-Here 0 }
|
|
}
|
|
|
|
# ── 2. copy ────────────────────────────────────────────────────────────────
|
|
Write-Host ''
|
|
Write-Host ' 2/2 Kopiere neue und geaenderte Dateien ...' -ForegroundColor Yellow
|
|
& robocopy $Source $Target @common /MT:16 /NDL /NP /TEE /UNILOG:$copyLog
|
|
$rc = $LASTEXITCODE
|
|
Write-Host ''
|
|
if ($rc -ge 8) {
|
|
Write-Host " FEHLER: Backup unvollstaendig (robocopy $rc). Details: $copyLog" -ForegroundColor Red
|
|
Stop-Here 1
|
|
}
|
|
Write-Host (" Backup fertig: {0:N0} Dateien, {1}. Protokoll: {2}" -f $copy.Count, (Format-Size $total), $copyLog) -ForegroundColor Green
|
|
Stop-Here 0
|