zscripts-token-savers/setup_backup_schedule.ps1
kellymichels 91b638ac31
feat: checksums, toolkit versioning (v{major}.{rc}.{beta}.{alpha}.{build}), and downloadable release zips (#35)
* feat(zchecksums): SHA-256 manifest so a download can be verified before it's run

CHECKSUMS.txt lists a SHA-256 for every top-level .ps1 and .cmd - the files a
user actually executes. zchecksums verifies them; zchecksums -Update
regenerates after an intentional edit.

The manifest is sha256sum format, so 'sha256sum -c CHECKSUMS.txt' works on
Linux/macOS/WSL as well as the PowerShell path on Windows. Hashes are identical
on every platform because .gitattributes pins .ps1/.cmd to CRLF everywhere -
that pin is now load-bearing, so it is commented as such.

Beyond changed and missing files it also reports a script that is on disk but
NOT in the manifest, so something added outside a commit still gets noticed.
Exits non-zero on any of the three.

Honest about its limits, in the header and the README: the manifest lives in
the same repo as the code, so it is an integrity check rather than a signature.
It catches a truncated clone, a forgotten local edit, or an unlisted file - not
a compromised repo.

CHECKSUMS.txt is pinned to LF: sha256sum treats a trailing CR as part of the
filename and would report every entry as missing on Linux.

tests/Checksums.Tests.ps1 keeps it from rotting - a stale manifest is worse
than none, since it either cries wolf until people ignore it or quietly stops
covering a new script. The tests assert the format, LF endings, sort order,
full coverage of on-disk scripts, current hashes, and that zchecksums itself
exits 1 on a tampered file (proved by appending a byte and restoring it).

* feat(zversion, zrelease): toolkit versioning + downloadable release zips

Implements the versioning rule (SmartPlant's 5-segment scheme, now the global
standard; currently only sp and zscripts are on it at v1.x):

    v{major}.{rc}.{beta}.{alpha}.{build}

zversion: get / bump / bump-stage / set. A stage bump zeroes every lower
segment including build. 'bump' is one per PR and one per defect fix, not per
file. Any write rewrites three things together, because they are only useful
when they agree: build-version.json (source of truth), a '# Version:' line in
all 42 script headers (a lone copied script still says which release it came
from), and CHECKSUMS.txt (stamping changes every file).

zrelease: packages the current version as releases/zscripts-<version>.zip with
a sibling .sha256, for people who want the toolkit without cloning. One hash
verifies the download; the bundled CHECKSUMS.txt verifies the extracted
contents. Refuses to overwrite an existing version's zip (released = immutable;
bump instead), and refuses to package when zchecksums fails. tests/ excluded
from the zip; releases/ never packages itself.

First release included: releases/zscripts-v1.0.0.0.0.zip (42 scripts + 7
support files) and its .sha256.

.gitattributes: releases/*.sha256 pinned LF (sha256sum treats a trailing CR as
part of the filename), releases/*.zip marked binary.

Verified end-to-end as a downloader would experience it, in WSL: sha256sum -c
on the zip passes, unzip, sha256sum -c CHECKSUMS.txt inside gives 42 OK / 0
FAILED, and the extracted zdeploy.ps1 header and build-version.json both read
v1.0.0.0.0. Double-release guard and -Verify mode exercised. Full Pester suite
219/219 (the checksum tests absorb the new files automatically).
2026-07-28 13:47:25 -05:00

80 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.
# Version: v1.0.0.0.0
# 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 ""