zscripts-token-savers/setup_backup_schedule.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

79 lines
3.0 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.
# setup_backup_schedule.ps1 — create a scheduled task for daily backups + OneDrive sync
#
# Usage:
# Right-click PowerShell -> "Run as Administrator"
# Then run: .\setup_backup_schedule.ps1
#
# This creates a task that runs zbackup_and_sync.ps1 every day at 2:00 AM
#Requires -RunAsAdministrator
$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "ZHelpers.ps1")
$cfg = Get-ZConfig
$ScriptPath = Join-Path $cfg.paths.scriptsRoot "zbackup_and_sync.ps1"
$TaskName = "ZScripts-Backup-And-Sync"
Write-Host ""
Write-Host "Setting up scheduled backup task..." -ForegroundColor Cyan
Write-Host ""
if (-not (Test-Path -LiteralPath $ScriptPath)) {
Write-Host "ERROR: Script not found: $ScriptPath" -ForegroundColor Red
Write-Host " Check paths.scriptsRoot in zconfig.json" -ForegroundColor DarkGray
exit 1
}
$existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
if ($existingTask) {
Write-Host "Task already exists: $TaskName" -ForegroundColor Yellow
Write-Host ""
$deleteChoice = Read-Host "Delete and recreate? (y/n)"
if ($deleteChoice -eq "y") {
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
Write-Host "Deleted existing task." -ForegroundColor Green
} else {
Write-Host "Keeping existing task. Exiting." -ForegroundColor Yellow
exit 0
}
}
$trigger = New-ScheduledTaskTrigger -Daily -At "02:00"
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -NoProfile -File `"$ScriptPath`" all"
$settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-Compatibility Win8
Write-Host "Creating scheduled task: $TaskName" -ForegroundColor Yellow
Register-ScheduledTask `
-TaskName $TaskName `
-Trigger $trigger `
-Action $action `
-Settings $settings `
-Description "Daily backup of all projects + OneDrive sync. Runs at 2:00 AM daily." `
-Force
Write-Host ""
Write-Host "Success! Scheduled task created." -ForegroundColor Green
Write-Host ""
Write-Host "Task Details:" -ForegroundColor Cyan
Write-Host " Name: $TaskName" -ForegroundColor Gray
Write-Host " Schedule: Daily at 2:00 AM" -ForegroundColor Gray
Write-Host " Action: $ScriptPath" -ForegroundColor Gray
Write-Host ""
Write-Host "To manage the task:" -ForegroundColor Gray
Write-Host " View: Get-ScheduledTask -TaskName '$TaskName'" -ForegroundColor DarkGray
Write-Host " Run now: Start-ScheduledTask -TaskName '$TaskName'" -ForegroundColor DarkGray
Write-Host " Disable: Disable-ScheduledTask -TaskName '$TaskName'" -ForegroundColor DarkGray
Write-Host " Delete: Unregister-ScheduledTask -TaskName '$TaskName'" -ForegroundColor DarkGray
Write-Host ""