Files
project/ci/pull-backup.ps1
T

47 lines
1.5 KiB
PowerShell

# pull-backup.ps1
# Pulls the latest Gitea dump from the EX44 server to this computer.
# Run by Windows Task Scheduler. Requires the WireGuard tunnel to be up.
# ASCII only on purpose: PowerShell 5.1 reads .ps1 as ANSI and mangles UTF-8.
$ErrorActionPreference = 'Stop'
$Server = 'root@10.10.0.1'
$Dest = 'C:\Backups\recada'
$Keep = 14 # how many dumps to keep locally
New-Item -ItemType Directory -Force -Path $Dest | Out-Null
$log = Join-Path $Dest 'backup.log'
function Say([string]$msg) {
$line = "{0} {1}" -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $msg
Write-Host $line
Add-Content -Path $log -Value $line
}
try {
$latest = (ssh -o BatchMode=yes -o ConnectTimeout=10 $Server `
"ls -1t /srv/backup/gitea-*.zip 2>/dev/null | head -1")
if ($latest) { $latest = $latest.Trim() }
if (-not $latest) { throw 'no dumps found on server' }
$name = ($latest -split '/')[-1]
$target = Join-Path $Dest $name
if (Test-Path $target) {
Say "already here: $name"
} else {
scp -o BatchMode=yes "${Server}:$latest" $target
$size = [math]::Round((Get-Item $target).Length / 1MB, 1)
Say "pulled: $name ($size MB)"
}
Get-ChildItem $Dest -Filter 'gitea-*.zip' |
Sort-Object LastWriteTime -Descending |
Select-Object -Skip $Keep |
ForEach-Object { Remove-Item $_.FullName -Force; Say "pruned: $($_.Name)" }
}
catch {
Say "ERROR: $($_.Exception.Message)"
exit 1
}