zscripts-token-savers/bash/zbackup_ec2
kellymichels 3fcc3546b7
fix(bash): translate Windows config paths to WSL/Unix form so the bash port works on WSL (#7)
* fix(bash): translate Windows config paths to WSL/Unix form

A shared zconfig.json (one file used from a Windows checkout and from
WSL via the symlink) holds Windows paths like "F:\evomedia.net\app".
The bash port passed those to local file ops verbatim, so every
path-using command failed on WSL (e.g. zbackup: "Root not found:
F:\evomedia.net\evomedia-docs").

Add a z_path helper that converts drive-letter paths to the host's
native form (wslpath, with a /mnt fallback) and is a no-op for Unix
paths and empty strings — so a bash-native config is unaffected. Route
every LOCAL path through it: localRoot (new zproj_root accessor),
paths.* (temp, backupsLocal, backupsEc2, scriptsRoot, oneDriveBackups),
ec2.pemKey (zec2_pem), and ztokens.dataDir. Server-side paths
(remote.path, composeDir, stackRoot, certsSource) are left verbatim.

Verified on WSL: all 11 project roots, all paths.*, and the C:/G: pem
and OneDrive paths resolve to existing dirs; zbackup evodocs (the
failing case) now runs clean end-to-end.

* fix(bash): keep z_path fallback bash-3.2 clean (tr, not ${x,,})

The wslpath-absent fallback lowercased the drive letter with ${drive,,},
a bash-4.0 construct that breaks on macOS's stock bash 3.2. Use tr
instead so the whole helper stays 3.2-compatible.
2026-07-22 18:40:52 -05:00

101 lines
4.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_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
mapfile -t projects < <(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"