zscripts-token-savers/bash/zsync
kellymichels 09f86c1cba
feat(bash): native bash port of all z-scripts for Linux/macOS/WSL (#5)
* 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).
2026-07-22 16:35:05 -05:00

98 lines
3.2 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.
#
# zsync — copy new backup files offsite; or build + mirror a vite project's dist.
#
# Usage:
# zsync # new files: backups folder -> paths.oneDriveBackups
# zsync <viteproject> --dest /mnt/mirror/dist
#
# The no-args mode copies only files that don't exist at the destination yet
# (never overwrites, never deletes). The project mode runs npm run build, then
# mirrors dist/ to --dest (or $ZSYNC_DEST) with rsync --delete.
set -uo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/zhelpers.sh"
z_track_start "$@"
z_need_config
projects=(); dest="${ZSYNC_DEST:-}"
while [ $# -gt 0 ]; do
case "$1" in
--dest) dest="${2:-}"; shift 2 ;;
-*) err "Unknown option: $1"; exit 1 ;;
*) projects+=("$1"); shift ;;
esac
done
# ---- no-args mode: backups -> offsite, new files only ------------------------
if [ "${#projects[@]}" -eq 0 ]; then
BACKUPS_ROOT="$(dirname "$(zq '.paths.backupsLocal')")"
OFFSITE_ROOT="$(zq '.paths.oneDriveBackups')"
printf '\n'; info "=== zsync (backups -> offsite) ==="
dim " Source: $BACKUPS_ROOT"
dim " Destination: $OFFSITE_ROOT"
dim " Mode: new files and folders only"
printf '\n'
if [ -z "$OFFSITE_ROOT" ]; then
warn " paths.oneDriveBackups is not set in zconfig.json - nothing to do."
exit 0
fi
if [ ! -d "$BACKUPS_ROOT" ]; then
warn " Source not found (skipped): $BACKUPS_ROOT"
exit 0
fi
mkdir -p "$OFFSITE_ROOT"
copied=0; skipped=0
while IFS= read -r -d '' f; do
rel="${f#"$BACKUPS_ROOT"/}"
target="$OFFSITE_ROOT/$rel"
if [ -e "$target" ]; then skipped=$((skipped + 1)); continue; fi
mkdir -p "$(dirname "$target")"
cp -p "$f" "$target"
ok " Copied: $rel ($(z_size_mb "$f") MB)"
copied=$((copied + 1))
done < <(find "$BACKUPS_ROOT" -type f -print0 2>/dev/null)
printf '\n'; info " Done: $copied copied, $skipped already present"
printf '\n'
exit 0
fi
# ---- project mode: build + mirror a vite project's dist ----------------------
z_require rsync
for key in "${projects[@]}"; do
zproj_require "$key"
kind="$(zproj "$key" .kind)"
if [ "$kind" != "vite" ]; then
printf '\n'; warn "zsync project mode only supports vite kind (builds and mirrors dist/). '$key' is kind '$kind'."
exit 1
fi
root="$(zproj "$key" .localRoot)"
if [ -z "$dest" ]; then
printf '\n'; info "=== zsync ($(zproj "$key" .label)) ==="
warn "No destination set."
dim "Set ZSYNC_DEST or run: zsync $key --dest /path/to/folder"
dim "This runs npm run build, then mirrors dist -> destination (rsync --delete)."
exit 1
fi
printf '\n'; info "=== zsync ($(zproj "$key" .label)) ==="
info "Build + mirror dist -> $dest"
printf '\n'
(cd "$root" && npm run build) || { err "npm run build failed"; exit 1; }
[ -d "$root/dist" ] || { err "dist/ missing after build."; exit 1; }
mkdir -p "$dest"
rsync -a --delete "$root/dist/" "$dest/" || { err "rsync failed"; exit 1; }
printf '\n'; ok "Sync complete."
done