#!/usr/bin/env bash set -Eeuo pipefail trap 'echo "Error on line $LINENO while creating ops bundle" >&2' ERR ensure_writable_dir() { local dir="$1" mkdir -p "$dir" if [[ ! -d "$dir" || ! -w "$dir" ]]; then echo "Output directory is not writable: $dir" >&2 exit 1 fi } OPS_REPO_PATH="${OPS_REPO_PATH:-/srv/ops}" OUT_BASE="${OUT_BASE:-/srv/backups/ops}" TS="$(date +%F_%H%M%S)" OUT_DIR="$OUT_BASE/$TS" LATEST_DIR="$OUT_BASE/latest" artifact="$OUT_DIR/ops.bundle" latest_artifact="$LATEST_DIR/ops.bundle" if [[ ! -d "$OPS_REPO_PATH/.git" ]]; then echo "Missing git repo at $OPS_REPO_PATH" exit 1 fi ensure_writable_dir "$OUT_DIR" git -C "$OPS_REPO_PATH" bundle create "$artifact" --all ensure_writable_dir "$LATEST_DIR" cp "$artifact" "$latest_artifact" if [[ ! -s "$artifact" || ! -s "$latest_artifact" ]]; then echo "Bundle artifact missing or empty" >&2 exit 1 fi echo "Wrote bundle: $artifact" echo "Updated latest: $latest_artifact"