zscripts-token-savers/bash/zbackup_and_sync
kellymichels d945430807
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:34:50 -05:00

49 lines
1.3 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_and_sync — run backups, then sync the backups folder offsite.
#
# Usage:
# zbackup_and_sync # backup everything + sync
# zbackup_and_sync <project> [<project> ...]
#
# Cron example (see setup_backup_schedule):
# 0 2 * * * /path/to/zscripts/bash/zbackup_and_sync >> ~/zbackup.log 2>&1
set -uo pipefail
_HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$_HERE/zhelpers.sh"
z_track_start "$@"
z_need_config
printf '\n'
info "============================================"
info " Backup & Sync - $(date '+%Y-%m-%d %H:%M:%S')"
info "============================================"
printf '\n'
warn "[1/2] Running backups..."
"$_HERE/zbackup" "$@"
backup_rc=$?
if [ $backup_rc -ne 0 ]; then
err "Backup failed (exit code: $backup_rc)"
exit $backup_rc
fi
printf '\n'; warn "[2/2] Syncing offsite..."
"$_HERE/zsync"
sync_rc=$?
printf '\n'
info "============================================"
if [ $sync_rc -eq 0 ]; then
ok " Backup & Sync Complete [OK]"
else
warn " Sync had issues (exit code: $sync_rc)"
fi
info "============================================"
printf '\n'
exit $sync_rc