zscripts-token-savers/bash/zbackup_and_sync
kellymichels 845e89a9b3
fix(bash): zbackup — real pg_dump error reporting + require an explicit target (all) (#9)
* fix(bash): zbackup reports the real pg_dump failure, not "No DATABASE_URL"

local_pg_dump returned 1 for four different cases (no .env, no
DATABASE_URL, unparseable URL, and an actual dump failure), and the
caller printed "No local DATABASE_URL - source-only backup" for every
one. So a project that HAS a DATABASE_URL whose dump failed was
mislabeled as having none — the two messages contradicted each other.

- Distinguish the cases with exit codes: 0 dumped, 1 attempted-but-failed,
  2 no local database. The caller now prints the right line for each.
- Stop swallowing pg_dump's stderr (2>/dev/null); on failure, surface the
  actual error (version mismatch, unreachable host, auth) plus the
  host:port/db it tried, so failures are diagnosable.

* fix(bash): zbackup treats a not-running local DB as a calm skip

Fold all pg_dump messaging into local_pg_dump (caller just captures
success) and special-case an unreachable database. "connection refused"
/ "could not connect" / DNS / timeout now print a quiet
"Local database not running at host:port - source-only backup." instead
of a red multi-line error - a stopped dev DB is a normal state. Real
failures (version mismatch, auth, missing db) still print the full
pg_dump error so they're diagnosable.

* fix(bash): zbackup/zbackup_and_sync require an explicit target, matching PowerShell

Bare `zbackup` quietly backed up every project - inconsistent with the
PowerShell version (and with zdeploy), and an easy way to kick off a
huge unintended backup. Now a bare invocation prints usage and lists the
projects; `all` does what bare used to (every project + the scripts
folder). Same for `zbackup_and_sync`, and setup_backup_schedule's cron
line now passes `all` so the scheduled job still backs everything up.
2026-07-25 15:17:12 -05:00

57 lines
1.7 KiB
Bash

#!/usr/bin/env bash
# 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 — run backups, then sync the backups folder offsite.
#
# Usage:
# zbackup_and_sync <project> [<project> ...]
# zbackup_and_sync all # backup everything + sync
#
# Cron example (see setup_backup_schedule):
# 0 2 * * * /path/to/zscripts/bash/zbackup_and_sync all >> ~/zbackup.log 2>&1
set -uo pipefail
_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$_HERE/zhelpers.sh"
z_track_start "$@"
z_need_config
# Require an explicit target (matching zbackup/zdeploy); 'all' backs up everything.
if [ $# -eq 0 ]; then
printf '\n'; warn "Usage: zbackup_and_sync <project> [<project> ...] | all"
dim " Projects in zconfig.json: $(zproj_csv)"
dim " 'all' backs up every project plus the scripts folder, then syncs offsite."
exit 1
fi
printf '\n'
info "============================================"
info " Backup & Sync - $(date '+%Y-%m-%d %H:%M:%S')"
info "============================================"
printf '\n'
warn "[1/2] Running backups..."
"$_HERE/zbackup" "$@"
backup_rc=$?
if [ $backup_rc -ne 0 ]; then
err "Backup failed (exit code: $backup_rc)"
exit $backup_rc
fi
printf '\n'; warn "[2/2] Syncing offsite..."
"$_HERE/zsync"
sync_rc=$?
printf '\n'
info "============================================"
if [ $sync_rc -eq 0 ]; then
ok " Backup & Sync Complete [OK]"
else
warn " Sync had issues (exit code: $sync_rc)"
fi
info "============================================"
printf '\n'
exit $sync_rc