Enhance backup scripts with error handling and directory checks
This commit is contained in:
@@ -1,22 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
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
|
||||
|
||||
mkdir -p "$OUT_DIR"
|
||||
git -C "$OPS_REPO_PATH" bundle create "$OUT_DIR/ops.bundle" --all
|
||||
ensure_writable_dir "$OUT_DIR"
|
||||
git -C "$OPS_REPO_PATH" bundle create "$artifact" --all
|
||||
|
||||
mkdir -p "$LATEST_DIR"
|
||||
cp "$OUT_DIR/ops.bundle" "$LATEST_DIR/ops.bundle"
|
||||
ensure_writable_dir "$LATEST_DIR"
|
||||
cp "$artifact" "$latest_artifact"
|
||||
|
||||
echo "Wrote bundle: $OUT_DIR/ops.bundle"
|
||||
echo "Updated latest: $LATEST_DIR/ops.bundle"
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user