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.
102 lines
4.1 KiB
Bash
102 lines
4.1 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_ec2 — pull backups down from the server: a Postgres dump for projects
|
|
# with a "db" config block, plus a zip of server-side data dirs (uploads/archive/dist).
|
|
#
|
|
# Usage:
|
|
# zbackup_ec2 # every project with a remote.path
|
|
# zbackup_ec2 <project> [<project> ...]
|
|
#
|
|
# Output:
|
|
# <paths.backupsEc2>/<project>/<timestamp>_<project>_db.sql (projects with a db block)
|
|
# <paths.backupsEc2>/<project>/<timestamp>_<project>_files.zip
|
|
|
|
set -uo pipefail
|
|
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/zhelpers.sh"
|
|
z_track_start "$@"
|
|
z_need_config
|
|
z_require ssh scp
|
|
|
|
projects=("$@")
|
|
if [ "${#projects[@]}" -eq 0 ]; then
|
|
projects=(); while IFS= read -r _zl || [ -n "$_zl" ]; do projects+=("$_zl"); done \
|
|
< <(jq -r '.projects | to_entries[] | select((.key | startswith("_") | not) and .value.remote.path != null) | .key' "$ZCONFIG")
|
|
fi
|
|
|
|
EC2_BACKUP_ROOT="$(z_path "$(zq '.paths.backupsEc2')")"
|
|
REMOTE_HOME="/home/$(zec2_user)"
|
|
|
|
scp_down() { # <remote_path> <local_path>
|
|
scp -o StrictHostKeyChecking=no -i "$(zec2_pem)" "$(zec2_target):$1" "$2" \
|
|
|| { err "SCP download failed: $1 -> $2"; return 1; }
|
|
}
|
|
|
|
declare -a summary
|
|
exit_code=0
|
|
|
|
ec2_backup() { # <key>
|
|
local key="$1" remote_path compose_dir ts out_dir remote_db remote_zip local_db local_zip
|
|
local db_user db_name has_db=0 line
|
|
zproj_require "$key"
|
|
remote_path="$(zproj "$key" .remote.path)"
|
|
compose_dir="$(zremote_compose_dir "$key")"
|
|
ts="$(date +%Y%m%d-%H%M%S)"
|
|
out_dir="$EC2_BACKUP_ROOT/$key"; mkdir -p "$out_dir"
|
|
remote_db="$REMOTE_HOME/ec2_${key}_db_$ts.sql"
|
|
remote_zip="$REMOTE_HOME/ec2_${key}_files_$ts.zip"
|
|
local_db="$out_dir/${ts}_${key}_db.sql"
|
|
local_zip="$out_dir/${ts}_${key}_files.zip"
|
|
db_user="$(zproj "$key" .db.user)"; db_name="$(zproj "$key" .db.name)"
|
|
[ -n "$db_user" ] && [ -n "$db_name" ] && has_db=1
|
|
|
|
printf '\n'; info "=== [$(printf '%s' "$key" | tr '[:lower:]' '[:upper:]')] Server backup ($(zproj "$key" .label)) ==="
|
|
|
|
if [ "$has_db" -eq 1 ]; then
|
|
warn " [1/4] PostgreSQL dump on the server..."
|
|
zec2_step "pg_dump $db_name" \
|
|
"cd $compose_dir && sudo docker compose exec -T db pg_dump -U $db_user -d $db_name > $remote_db" || return 1
|
|
else
|
|
dim " [1/4] No db config block - skipping database dump."
|
|
fi
|
|
|
|
warn " [2/4] Zipping server-side data (uploads/archive/dist if present)..."
|
|
local zip_cmd
|
|
zip_cmd="sudo apt-get install -y zip >/dev/null 2>&1"
|
|
zip_cmd+="; FILES=''"
|
|
zip_cmd+="; test -d $remote_path/uploads && FILES=\"\$FILES $remote_path/uploads\""
|
|
zip_cmd+="; test -d $remote_path/archive && FILES=\"\$FILES $remote_path/archive\""
|
|
zip_cmd+="; test -d $remote_path/dist && FILES=\"\$FILES $remote_path/dist\""
|
|
zip_cmd+="; test -f $remote_path/build-version.json && FILES=\"\$FILES $remote_path/build-version.json\""
|
|
zip_cmd+="; if [ -z \"\$FILES\" ]; then echo 'no data dirs found' | sudo zip $remote_zip - >/dev/null; else sudo zip -r $remote_zip \$FILES; fi"
|
|
if ! zec2_step "zip $key files" "$zip_cmd"; then
|
|
warn " WARNING: files zip failed - writing placeholder and continuing."
|
|
zec2_step "placeholder zip" "echo 'zip failed' | sudo zip $remote_zip - >/dev/null" || true
|
|
fi
|
|
|
|
warn " [3/4] Downloading to local..."
|
|
[ "$has_db" -eq 1 ] && { scp_down "$remote_db" "$local_db" || return 1; }
|
|
scp_down "$remote_zip" "$local_zip" || return 1
|
|
|
|
warn " [4/4] Cleaning up remote..."
|
|
zec2_step "remove remote temp files" "rm -f $remote_db $remote_zip" || true
|
|
|
|
line="$key ->"
|
|
[ "$has_db" -eq 1 ] && [ -f "$local_db" ] && line+=" $local_db ($(z_size_mb "$local_db") MB) |"
|
|
line+=" $local_zip ($(z_size_mb "$local_zip") MB)"
|
|
summary+=("$line")
|
|
}
|
|
|
|
for key in "${projects[@]+"${projects[@]}"}"; do
|
|
ec2_backup "$key" || { summary+=("$key -> FAILED"); exit_code=1; }
|
|
done
|
|
|
|
printf '\n'; info "=== Server backup summary ==="
|
|
for line in "${summary[@]+"${summary[@]}"}"; do
|
|
case "$line" in *FAILED*) err " $line" ;; *) ok " $line" ;; esac
|
|
done
|
|
printf '\n'
|
|
exit "$exit_code"
|