mirror of
https://github.com/kellymichels/zscripts-token-savers
synced 2026-10-07 07:18:18 +00:00
These three defaulted to "every project" when run with no arguments - inconsistent with zbackup/zdeploy/zstart/etc. (which show usage), and a surprising amount of work to kick off by accident: zbackup_ec2 pulls a server backup of every project, and zec2online deep-checks everything AND auto-starts any downed stacks. Now a bare invocation prints usage and lists the projects; 'all' does what bare used to. Applied to both the bash and PowerShell versions.
109 lines
4.4 KiB
Bash
109 lines
4.4 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 <project> [<project> ...]
|
|
# zbackup_ec2 all # every project with a remote.path
|
|
#
|
|
# 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=("$@")
|
|
# Require an explicit target; 'all' pulls every project with a remote.path.
|
|
if [ "${#projects[@]}" -eq 0 ]; then
|
|
printf '\n'; warn "Usage: zbackup_ec2 <project> [<project> ...] | all"
|
|
dim " Projects in zconfig.json: $(zproj_csv)"
|
|
dim " 'all' pulls a server backup of every project with a remote.path."
|
|
exit 1
|
|
fi
|
|
if printf '%s\n' "${projects[@]}" | grep -qx 'all'; 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"
|