zscripts-token-savers/zbackup_and_sync.ps1
KellyMichels 84523c8e4d fix(ps): zbackup explicit target + robust DATABASE_URL parsing; ssh-stderr deploy fix
Restores parked, previously-uncommitted PowerShell improvements:

- zbackup / zbackup_and_sync require an explicit target: bare invocation
  now prints usage instead of quietly backing up everything; 'all' does
  what bare used to (matching zdeploy). setup_backup_schedule.ps1 passes
  'all' to the scheduled task; both tolerate switch-style args.
- zbackup parses more DATABASE_URL styles: strips surrounding quotes
  (Prisma convention), accepts postgres:// and postgresql+driver://
  schemes, and treats the port as optional (defaults to 5432).
- Invoke-Ec2Step survives ssh stderr warnings: under ErrorActionPreference
  'Stop', PS 5.1 turns any native stderr line (e.g. Docker's COMPOSE_BAKE
  deprecation notice) into a terminating NativeCommandError, aborting a
  deploy that actually succeeded. Drop to Continue locally and flatten
  stderr so only the real exit code decides success.
2026-07-23 00:15:19 -05:00

69 lines
2.6 KiB
PowerShell

# Evomedia.net Token Savers — https://github.com/kellymichels/zscripts-token-savers
# Created by Kelly Michels · dev@evomedia.net
# Licensed under the MIT License. See LICENSE.
# zbackup_and_sync.ps1 — run backups, then sync the backups folder offsite.
#
# Usage:
# zbackup_and_sync.ps1 <project> [<project> ...]
# zbackup_and_sync.ps1 all # backup everything + sync
#
# Scheduled Task example (see setup_backup_schedule.ps1):
# powershell -ExecutionPolicy Bypass -NoProfile -File "<scriptsRoot>\zbackup_and_sync.ps1" all
param(
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$Projects = @()
)
$ErrorActionPreference = "Stop"
$ScriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Definition
. (Join-Path $ScriptRoot "ZHelpers.ps1")
Start-ZTracking
# Tolerate switch-style args from muscle memory; require an explicit target
# ('all' included) — same convention as zbackup/zdeploy.
$Projects = @($Projects | ForEach-Object { $_.TrimStart('-') })
if ($Projects.Count -eq 0) {
$keys = (Get-ZProjectKeys) -join ', '
Write-Host ""
Write-Host "Usage: zbackup_and_sync <project> [<project> ...] | all" -ForegroundColor Yellow
Write-Host " Projects in zconfig.json: $keys" -ForegroundColor Gray
Write-Host " 'all' backs up every project plus the scripts folder, then syncs offsite." -ForegroundColor Gray
Stop-ZTracking; exit 1
}
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
Write-Host " Backup & Sync - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "[1/2] Running backups..." -ForegroundColor Yellow
$backupPath = Join-Path $ScriptRoot "zbackup.ps1"
& powershell -NoProfile -File $backupPath @Projects
$backupExitCode = $LASTEXITCODE
if ($backupExitCode -ne 0) {
Write-Host "Backup failed (exit code: $backupExitCode)" -ForegroundColor Red
Stop-ZTracking; exit $backupExitCode
}
Write-Host ""
Write-Host "[2/2] Syncing offsite..." -ForegroundColor Yellow
$syncPath = Join-Path $ScriptRoot "zsync.ps1"
& powershell -NoProfile -File $syncPath
$syncExitCode = $LASTEXITCODE
Write-Host ""
Write-Host "============================================" -ForegroundColor Cyan
if ($syncExitCode -eq 0) {
Write-Host " Backup & Sync Complete [OK]" -ForegroundColor Green
} else {
Write-Host " Sync had issues (exit code: $syncExitCode)" -ForegroundColor Yellow
}
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Stop-ZTracking; exit $syncExitCode