scripts(backup-notifications): unify push + email format across VDS/RUVDS/windows-host

3 host-pipelines (VDS bash, RUVDS+windows-host ps1) had drifted formats:
ntfy title `VDS backup OK $D` vs `RUVDS backup OK ($D)`, tags
`white_check_mark` vs `green_circle`, email subject `[VDS] backup OK` vs
`RUVDS backup -- SUCCESS`. Phone-side фильтрация и desktop reading
ломались за счёт inconsistency.

Unified to:
- ntfy push: title `<HOST> backup OK <date>`, body
  `<duration_human>, size=<>, snapshots=<>, dest=kreknin:<>`,
  tags `green_circle` (OK) / `red_circle` (FAILED).
- email: subject `[<HOST>] backup <STATUS> <date>` (STATUS=OK|FAILED),
  body — structured Date/Duration/Size/Snapshots/Source/Dest/Components/Log.
  Failure body extends with `Tail (last 40 lines)`.

Also imports VDS `run.sh` into repo as `scripts/vds-backup-rsync-kreknin/`
— closes drift из общего `scripts/<slug>/` pattern (RUVDS+windows-host
уже жили там; VDS жил только на /opt/stacks/backup/scripts/).

Deploy status:
- VDS: deployed via scp + sudo install, sha256=27b09ca272bb, smoke ntfy+email ✓
- RUVDS: deployed via scp + Move-Item, sha256=f3bb57a86af5, smoke ntfy+email ✓
- windows-host: deploy.ps1 + smoke-notify.ps1 готовы в scripts/, **pending
  elevated PS у user'а** (ACL=SYSTEM+Administrators, не пишется без UAC).

Spec + decisions + completed: .tasks/unify-backup-notifications.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 07:19:15 +03:00
parent 29724d410f
commit 73ad6dd06a
9 changed files with 610 additions and 35 deletions

View File

@@ -14,6 +14,14 @@ $rcloneCfg = "$base\rclone.conf"
$remoteBase = "kreknin:NetBackup/ruvds-iis"
$remoteToday = "$remoteBase/$today"
$HostLabel = 'RUVDS'
$SourceDisplay = "RUVDS ($env:COMPUTERNAME / 80.64.31.36)"
$DestDisplay = "kreknin:/volume1/NetBackup/ruvds-iis/$today/"
function Format-Duration([int]$sec) {
'{0}m{1:D2}s' -f ([int]($sec / 60)), ($sec % 60)
}
if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }
Start-Transcript -Path $logFile -Append -Force | Out-Null
@@ -134,40 +142,65 @@ try {
Remove-Item $certDir -Recurse -Force -ErrorAction SilentlyContinue
$duration = [int](New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
$durationSec = [int](New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
$durationHuman = Format-Duration $durationSec
$srcBytes = (Get-ChildItem 'C:\sites\snolla' -Recurse -File -EA SilentlyContinue | Measure-Object Length -Sum).Sum
$totalGB = '{0:N2}' -f ($srcBytes / 1GB)
$msg = "RUVDS daily backup $today OK | $duration sec | size $totalGB GB | $exported certs"
Notify-Ntfy "RUVDS backup OK ($today)" $msg 'default' 'green_circle'
$sizeStr = '{0:N2} GB' -f ($srcBytes / 1GB)
$snapshotCount = if ($existing) { @($existing).Count } else { 1 }
# ntfy push (single line)
$ntfyBody = "$durationHuman, size=$sizeStr, snapshots=$snapshotCount, dest=$DestDisplay, certs=$exported"
Notify-Ntfy "$HostLabel backup OK $today" $ntfyBody 'default' 'green_circle'
# email (structured)
$emailBody = @"
RUVDS daily backup completed successfully.
$HostLabel daily backup completed successfully.
Date: $today
Duration: $duration seconds
Size: $totalGB GB
Certs: $exported PFX
Source: RUVDS ($env:COMPUTERNAME / 80.64.31.36)
Dest: kreknin:/volume1/NetBackup/ruvds-iis/$today/
Date: $today
Duration: $durationHuman
Size: $sizeStr
Snapshots: $snapshotCount
Source: $SourceDisplay
Dest: $DestDisplay
Components synced:
Components:
- sites/snolla
- iis-config (applicationHost.config)
- iis-backup-webconfiguration
- certs (LE PFX exports)
- certs ($exported PFX exports)
- ssh-config
Log: $logFile
"@
Notify-Email "RUVDS backup $today -- SUCCESS" $emailBody
Write-Host "`n=== DONE in $duration sec ==="
Notify-Email "[$HostLabel] backup OK $today" $emailBody
Write-Host "`n=== DONE in $durationHuman ==="
} catch {
$err = $_.Exception.Message
Write-Host "`n=== FAILED: $err ===" -ForegroundColor Red
Write-Host $_.ScriptStackTrace
$duration = [int](New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
Notify-Ntfy "RUVDS backup FAILED ($today)" "After $duration sec: $err" 'high' 'red_circle'
Notify-Email "RUVDS backup $today -- FAILED" "After $duration sec: $err`n`nLog: $logFile"
$durationSec = [int](New-TimeSpan -Start $start -End (Get-Date)).TotalSeconds
$durationHuman = Format-Duration $durationSec
$tailLog = ''
try { $tailLog = (Get-Content $logFile -Tail 40 -EA SilentlyContinue) -join "`n" } catch {}
# ntfy push (single line)
Notify-Ntfy "$HostLabel backup FAILED $today" "After $durationHuman`: $err. See $logFile" 'high' 'red_circle'
# email (structured)
$emailBody = @"
$HostLabel daily backup FAILED.
Date: $today
Duration: $durationHuman
Error: $err
Source: $SourceDisplay
Log: $logFile
Tail (last 40 lines):
$tailLog
"@
Notify-Email "[$HostLabel] backup FAILED $today" $emailBody
Stop-Transcript | Out-Null
exit 1
} finally {

View File

@@ -0,0 +1,65 @@
# Smoke-test notify channels with NEW unified format.
# Sends one TEST-tagged ntfy push + one TEST-tagged email — no backup run.
# Used by unify-backup-notifications task to verify format/delivery without 80-min wait.
$ErrorActionPreference = 'Stop'
$base = 'C:\ProgramData\backup'
$cfg = @{}
Get-Content "$base\config.env" | Where-Object { $_ -match '^[A-Z_]+=' } | ForEach-Object {
$kv = $_ -split '=', 2
$cfg[$kv[0]] = $kv[1]
}
$today = Get-Date -Format 'yyyy-MM-dd'
$HostLabel = 'RUVDS'
$SourceDisplay = "RUVDS ($env:COMPUTERNAME / 80.64.31.36)"
$DestDisplay = "kreknin:/volume1/NetBackup/ruvds-iis/$today/"
$durationHuman = '1m02s'
$sizeStr = '8.66 GB'
$snapshotCount = 1
$exported = 25
$logFile = "C:\ProgramData\backup\logs\$today.log"
# ntfy
$pair = "$($cfg.NTFY_USER):$($cfg.NTFY_PASS)"
$auth = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($pair))
$ntfyBody = "$durationHuman, size=$sizeStr, snapshots=$snapshotCount, dest=$DestDisplay, certs=$exported"
try {
Invoke-RestMethod -Uri "$($cfg.NTFY_URL)/vds-backup" -Method POST `
-Headers @{ Authorization=$auth; Title="[TEST] $HostLabel backup OK $today"; Priority='default'; Tags='green_circle' } `
-Body $ntfyBody -ContentType 'text/plain' -ErrorAction Stop | Out-Null
Write-Host 'ntfy_ok'
} catch { Write-Host ('ntfy_fail: ' + $_.Exception.Message) }
# email
$emailBody = @"
$HostLabel daily backup completed successfully.
Date: $today
Duration: $durationHuman
Size: $sizeStr
Snapshots: $snapshotCount
Source: $SourceDisplay
Dest: $DestDisplay
Components:
- sites/snolla
- iis-config (applicationHost.config)
- iis-backup-webconfiguration
- certs ($exported PFX exports)
- ssh-config
Log: $logFile
[TEST RUN sent from unify-backup-notifications smoke]
"@
$secpass = ConvertTo-SecureString $cfg.SMTP_PASS -AsPlainText -Force
$mailCred = New-Object PSCredential($cfg.SMTP_USER, $secpass)
try {
Send-MailMessage -SmtpServer $cfg.SMTP_HOST -Port ([int]$cfg.SMTP_PORT) -UseSsl `
-Credential $mailCred -From $cfg.SMTP_FROM -To $cfg.OPS_NOTIFY_EMAIL `
-Subject "[TEST][$HostLabel] backup OK $today" -Body $emailBody -Encoding UTF8 `
-WarningAction SilentlyContinue -ErrorAction Stop
Write-Host 'email_ok'
} catch { Write-Host ('email_fail: ' + $_.Exception.Message) }