Upload backup to Gitea as a release asset

After the archive is produced, tag the current HEAD as backup-<stamp>,
create a Gitea release, and attach the tarball. Token is read from
$GITEA_TOKEN or ~/.gitea_token; upload is skipped if neither is set.
This commit is contained in:
2026-04-21 20:45:36 +02:00
parent 429bcdb1bf
commit b4c26f39f0

View File

@@ -1,8 +1,13 @@
#!/usr/bin/env bash
# Usage: ./backup.sh [label]
# Creates backups/world-YYYY-MM-DD-HHMM[-label].tar.gz
# Creates backups/world-YYYY-MM-DD-HHMM[-label].tar.gz and uploads it
# as a Gitea release asset on the `origin` remote.
#
# If the container is running, flushes world via rcon and archives via
# docker exec. If not, archives data/world from the host directly.
# docker exec. Otherwise archives data/world from the host directly.
#
# Token is read from $GITEA_TOKEN or ~/.gitea_token. If neither is set,
# the local archive is still created and upload is skipped.
set -euo pipefail
cd "$(dirname "$0")"
@@ -27,5 +32,60 @@ else
tar -czf "${OUT}" -C data world
fi
echo "Done."
ls -lh "${OUT}"
echo "Archive done: $(ls -lh "${OUT}" | awk '{print $5, $9}')"
# --- Upload to Gitea as a release asset ---
TOKEN="${GITEA_TOKEN:-}"
if [[ -z "$TOKEN" && -r "$HOME/.gitea_token" ]]; then
TOKEN="$(cat "$HOME/.gitea_token")"
fi
if [[ -z "$TOKEN" ]]; then
echo "No Gitea token (set GITEA_TOKEN or ~/.gitea_token) — skipping upload."
exit 0
fi
URL="$(git config --get remote.origin.url)"
SCHEME="${URL%%://*}"
REST="${URL#*://}"; REST="${REST#*@}" # strip any embedded userinfo
HOSTPORT="${REST%%/*}"
REPO_PATH="${REST#*/}"; REPO_PATH="${REPO_PATH%.git}"
OWNER="${REPO_PATH%/*}"
REPO="${REPO_PATH#*/}"
API="${SCHEME}://${HOSTPORT}/api/v1"
TAG="backup-${STAMP}${SUFFIX}"
NAME="World backup ${STAMP}${LABEL:+ ($LABEL)}"
SHA="$(git rev-parse HEAD)"
echo "Pushing main so the release target exists on remote..."
git push origin main >/dev/null
echo "Creating release '${TAG}' on ${OWNER}/${REPO}..."
PAYLOAD=$(printf '{"tag_name":"%s","target_commitish":"%s","name":"%s","body":"Automated world backup"}' \
"$TAG" "$SHA" "$NAME")
RESPONSE=$(curl -fsS -X POST \
-H "Authorization: token ${TOKEN}" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"${API}/repos/${OWNER}/${REPO}/releases")
RELEASE_ID="${RESPONSE#*\"id\":}"
RELEASE_ID="${RELEASE_ID%%,*}"
if ! [[ "$RELEASE_ID" =~ ^[0-9]+$ ]]; then
echo "Could not parse release id from response:" >&2
echo "$RESPONSE" >&2
exit 1
fi
echo "Uploading asset to release ${RELEASE_ID}..."
curl -fsS -X POST \
-H "Authorization: token ${TOKEN}" \
-F "attachment=@${OUT}" \
"${API}/repos/${OWNER}/${REPO}/releases/${RELEASE_ID}/assets?name=$(basename "${OUT}")" \
>/dev/null
echo "Uploaded: ${SCHEME}://${HOSTPORT}/${OWNER}/${REPO}/releases/tag/${TAG}"