#!/usr/bin/env bash
# Nightly backup: Waybill config and the Ledger journal, kept on /var/backups.
set -euo pipefail
root="$(pwd)"
cap=$((100 * 1024 * 1024))
used="$(find "$root/var" -type f -printf '%s\n' | awk '{s+=$1} END {print s+0}')"
if (( used > cap * 90 / 100 )); then
  printf 'backup: refusing to run, /var is %s%% full\n' "$((used * 100 / cap))" >&2
  exit 1
fi
out="$root/var/backups/waybill-$(date -u +%Y%m%dT%H%M%S).tar.gz"
tar -czf "$out" -C "$root" etc/waybill var/ledger
printf 'backup: wrote %s\n' "$out"
