mirror of
https://github.com/kellymichels/zscripts-token-savers
synced 2026-10-07 07:18:18 +00:00
* feat(bash): native bash port of all z-scripts for Linux/macOS/WSL Full port: zhelpers.sh library (jq config, ssh, http/tcp, archive builder, build-version, motd, port-kill), all commands (zstart/zkill/zrestart/zstop, zdeploy with 5 kind handlers, zec2/zec2online/zrepair, zbackup/zbackup_ec2/ zsync/zbackup_and_sync, zstart_docker, zsetup_mail, setup_backup_schedule via cron), Unix-path zconfig.example.json, and a bash/README.md. Verified: bash -n clean on all scripts; archive exclusions, config semantics, and zec2 tested against the real config and live server from Git Bash. Needs a Linux/macOS/WSL shakedown for lsof/rsync/nohup paths before merging. * chore: pin line endings (.gitattributes) - bash LF, powershell CRLF * docs(readme): point Linux/macOS/WSL users at the bash port * fix(bash): don't run the Windows venv python.exe on WSL/Linux/macOS zstart's venv detection fell back to .venv/Scripts/python.exe (a Windows binary) whenever it existed. On a Windows-built project accessed from WSL that file sits on the mount and looks executable, so it got picked and failed with 'exec format error' instead of falling through to python3. Guard that branch to Windows-family shells (msys/cygwin), where a .exe can actually run. Verified on WSL: zstart --detached now backgrounds a stdlib app via python3, serves HTTP 200, logs to /tmp/zstart-<key>.log, and zkill terminates it and frees the port. * feat(bash): add token-usage tracking to the bash port (#6) Adds z_track_start/z_track_stop + z_record to zhelpers.sh and wires z_track_start into every command script. Each run now captures its own output volume (FIFO+tee, ANSI stripped), prints the '--- N lines / N chars / ~N tokens est. (Claude Code) ---' footer, and appends one JSONL row per top-level run in the same shape as the PowerShell tokens.jsonl. A nested-run guard keeps zrestart from double-counting its zkill/zstart children. Data dir precedence: $ZTOKENS_DATA, config ztokens.dataDir, sibling ../../ztokens/data, else ~/.ztokens/data. Docs + example config updated. Verified on WSL (isolated data dir): single run records correctly; nested zrestart produces one combined record, not three; est = round(chars/3.5).
101 lines
4.0 KiB
Bash
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="$(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"
|