Enhance backup scripts with error handling and directory checks

This commit is contained in:
2026-03-04 14:55:43 -05:00
parent c93dcb5daf
commit cbcb6f02a5
5 changed files with 113 additions and 19 deletions

View File

@@ -1,5 +1,16 @@
#!/usr/bin/env bash
set -euo pipefail
set -Eeuo pipefail
trap 'echo "Error on line $LINENO during mirror export" >&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
}
GITEA_URL="${GITEA_URL:-https://git.sketchferret.com}"
TOKEN_FILE="${TOKEN_FILE:-/srv/secrets/gitea_token}"
@@ -12,7 +23,7 @@ if [[ ! -f "$TOKEN_FILE" ]]; then
fi
TOKEN="$(cat "$TOKEN_FILE")"
mkdir -p "$OUT/$OWNER"
ensure_writable_dir "$OUT/$OWNER"
repos_json="$(curl -fsSL -H "Authorization: token $TOKEN" "$GITEA_URL/api/v1/users/$OWNER/repos?limit=1000")"
@@ -23,6 +34,11 @@ for repo in json.loads(sys.argv[1]):
PY
)
if [[ "${#urls[@]}" -eq 0 ]]; then
echo "No repositories returned for owner '$OWNER' from $GITEA_URL" >&2
exit 1
fi
for url in "${urls[@]}"; do
name="$(basename "$url" .git)"
target="$OUT/$OWNER/$name.git"