mirror of
https://github.com/kellymichels/zscripts-token-savers
synced 2026-10-07 07:18:18 +00:00
The README advertises macOS support, but the port used constructs that fail on the userland macOS actually ships: - `mapfile` (bash 4+) in 10 spots — macOS ships bash 3.2 as /usr/bin/bash, so a mac user following the README hit "mapfile: command not found" and silently got empty project lists. Replace each with a portable `while IFS= read -r` loop (identical arrays; set -u safe on empty input). - `_z_commafy` used the GNU-only `sed :a;...;ta` label/branch idiom, which errors on BSD/macOS sed (the token footer's thousands separators). Reimplement with awk (already a dependency). - README: correct the macOS line — bash 4+ does NOT ship with macOS; the stock 3.2 now works, and only jq needs brew. Also two correctness nits found in the same review: - zkill/zrestart `--kill-all` was parsed but silently ignored; now it prints a "not implemented in the bash port" notice instead of no-op. - zrepair's smoke-test line said "https://$domain" but probes http://$HOST with a Host header; label now matches what it does. Verified on WSL (bash 5): read-loops produce the same 11 project / 7 domain keys as mapfile; awk commafy matches across 0..1,234,567; empty producer yields a 0-length array; footer renders. 3.2-compat is by static analysis — no bash-4-only constructs remain.
133 lines
5.0 KiB
Bash
133 lines
5.0 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 — local backups: zip project sources (plus a Postgres dump when the
|
|
# project's .env has a DATABASE_URL) into the backups folder.
|
|
#
|
|
# Usage:
|
|
# zbackup # every project in zconfig.json + this scripts folder
|
|
# zbackup <project> [<project> ...]
|
|
# zbackup scripts # just this scripts folder ('scripts' is a reserved word)
|
|
# zbackup pyapp --tag "pre-migration"
|
|
#
|
|
# Output: <paths.backupsLocal>/<project>/<timestamp>_<project>[_tag].zip
|
|
|
|
set -uo pipefail
|
|
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/zhelpers.sh"
|
|
z_track_start "$@"
|
|
z_need_config
|
|
z_require zip
|
|
|
|
projects=(); tag=""
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--tag) tag="${2:-}"; shift 2 ;;
|
|
-*) err "Unknown option: $1"; exit 1 ;;
|
|
*) projects+=("$1"); shift ;;
|
|
esac
|
|
done
|
|
|
|
BACKUP_ROOT="$(zq '.paths.backupsLocal')"
|
|
include_scripts=0
|
|
if [ "${#projects[@]}" -eq 0 ]; then
|
|
projects=(); while IFS= read -r _zl || [ -n "$_zl" ]; do projects+=("$_zl"); done < <(zproj_keys)
|
|
include_scripts=1
|
|
else
|
|
filtered=()
|
|
for p in "${projects[@]}"; do
|
|
if [ "$p" = "scripts" ]; then include_scripts=1; else filtered+=("$p"); fi
|
|
done
|
|
projects=("${filtered[@]+"${filtered[@]}"}")
|
|
fi
|
|
|
|
ts() { date +%Y%m%d-%H%M%S; }
|
|
tag_suffix() { [ -n "$tag" ] && printf '_%s' "$(printf '%s' "$tag" | tr -s '[:space:]' '_')"; }
|
|
|
|
# Dump the project's Postgres database if its .env declares a DATABASE_URL.
|
|
# Checks <root>/.env, then <root>/backend/.env. Returns 0 and writes $2 on success.
|
|
local_pg_dump() { # <root> <out_path>
|
|
local root="$1" out="$2" env_file db_url
|
|
env_file="$root/.env"; [ -f "$env_file" ] || env_file="$root/backend/.env"
|
|
[ -f "$env_file" ] || return 1
|
|
db_url="$(grep -m1 '^DATABASE_URL=' "$env_file" | cut -d= -f2- | tr -d '[:space:]')"
|
|
[ -n "$db_url" ] || return 1
|
|
db_url="$(printf '%s' "$db_url" | sed -E 's|^postgresql\+[^:]+://|postgresql://|')"
|
|
if [[ "$db_url" =~ ^postgresql://([^:]+):([^@]+)@([^:]+):([0-9]+)/([^?]+) ]]; then
|
|
local user="${BASH_REMATCH[1]}" pass="${BASH_REMATCH[2]}" host="${BASH_REMATCH[3]}"
|
|
local port="${BASH_REMATCH[4]}" db="${BASH_REMATCH[5]}"
|
|
command -v pg_dump >/dev/null 2>&1 || { err ' pg_dump not installed - skipping PG backup'; return 1; }
|
|
if PGPASSWORD="$(z_urldecode "$pass")" pg_dump -h "$host" -p "$port" -U "$user" -d "$db" -F p -f "$out" 2>/dev/null \
|
|
&& [ -f "$out" ]; then
|
|
ok " PostgreSQL dump: $(awk -v b="$(wc -c < "$out")" 'BEGIN{printf "%.1f", b/1024}') KB"
|
|
return 0
|
|
fi
|
|
err " pg_dump failed"
|
|
return 1
|
|
fi
|
|
err ' Could not parse DATABASE_URL - skipping PG backup'
|
|
return 1
|
|
}
|
|
|
|
declare -a summary
|
|
exit_code=0
|
|
|
|
backup_project() { # <key>
|
|
local key="$1" root out_dir zip_path dump_dir db_dump extra="-"
|
|
zproj_require "$key"
|
|
root="$(zproj "$key" .localRoot)"
|
|
out_dir="$BACKUP_ROOT/$key"; mkdir -p "$out_dir"
|
|
zip_path="$out_dir/$(ts)_${key}$(tag_suffix).zip"
|
|
|
|
printf '\n'; info "=== [$(printf '%s' "$key" | tr '[:lower:]' '[:upper:]')] Local backup ($(zproj "$key" .label)) ==="
|
|
dim " Output: $zip_path"
|
|
[ -d "$root" ] || { err " Root not found: $root"; return 1; }
|
|
|
|
dump_dir="$(mktemp -d "${TMPDIR:-/tmp}/zbackup_${key}_XXXXXX")"
|
|
db_dump="$dump_dir/database_pg.sql"
|
|
warn " [1/3] Checking for a local database to dump..."
|
|
if local_pg_dump "$root" "$db_dump"; then extra="$db_dump"
|
|
else dim " No local DATABASE_URL - source-only backup."; fi
|
|
|
|
warn " [2/3] Archiving source..."
|
|
local excl=()
|
|
excl=(); while IFS= read -r _zl || [ -n "$_zl" ]; do excl+=("$_zl"); done < <(z_archive_excludes "$key" 1)
|
|
if ! z_archive "$root" "$zip_path" 0 "$extra" "${excl[@]}"; then
|
|
rm -rf "$dump_dir"; return 1
|
|
fi
|
|
rm -rf "$dump_dir"
|
|
warn " [3/3] Done."
|
|
summary+=("$key -> $zip_path ($(z_size_mb "$zip_path") MB)")
|
|
}
|
|
|
|
backup_scripts() {
|
|
local out_dir zip_path scripts_root
|
|
scripts_root="$(zq '.paths.scriptsRoot')"
|
|
out_dir="$BACKUP_ROOT/scripts"; mkdir -p "$out_dir"
|
|
zip_path="$out_dir/$(ts)_scripts$(tag_suffix).zip"
|
|
|
|
printf '\n'; info "=== [SCRIPTS] Local backup (this scripts folder) ==="
|
|
dim " Output: $zip_path"
|
|
[ -d "$scripts_root" ] || { err " Scripts root not found: $scripts_root"; return 1; }
|
|
|
|
warn " [1/2] Archiving source..."
|
|
z_archive "$scripts_root" "$zip_path" 1 "-" .git archive tmp nul || return 1
|
|
warn " [2/2] Done."
|
|
summary+=("scripts -> $zip_path ($(z_size_mb "$zip_path") MB)")
|
|
}
|
|
|
|
for key in "${projects[@]+"${projects[@]}"}"; do
|
|
backup_project "$key" || { summary+=("$key -> FAILED"); exit_code=1; }
|
|
done
|
|
if [ "$include_scripts" -eq 1 ]; then
|
|
backup_scripts || { summary+=("scripts -> FAILED"); exit_code=1; }
|
|
fi
|
|
|
|
printf '\n'; info "=== Backup summary ==="
|
|
for line in "${summary[@]+"${summary[@]}"}"; do
|
|
case "$line" in *FAILED*) err " $line" ;; *) ok " $line" ;; esac
|
|
done
|
|
printf '\n'
|
|
exit "$exit_code"
|