Add manual world backup script

Flushes world via rcon (save-off + save-all flush) before tarring
when the server is running; skips the flush and archives as-is when
the container is down. Outputs timestamped archives to backups/,
which is gitignored.
This commit is contained in:
2026-04-21 20:35:57 +02:00
parent ad654d145a
commit f67c8c8012
2 changed files with 30 additions and 0 deletions

29
backup.sh Executable file
View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
# Usage: ./backup.sh [label]
# Creates backups/world-YYYY-MM-DD-HHMM[-label].tar.gz
# If the minecraft container is running, flushes world via rcon first.
set -euo pipefail
cd "$(dirname "$0")"
LABEL="${1:-}"
STAMP="$(date +%Y-%m-%d-%H%M)"
SUFFIX="${LABEL:+-$LABEL}"
OUT="backups/world-${STAMP}${SUFFIX}.tar.gz"
mkdir -p backups
if docker ps --format '{{.Names}}' | grep -qx minecraft; then
echo "Server running — flushing world via rcon..."
docker exec minecraft rcon-cli save-off >/dev/null
docker exec minecraft rcon-cli save-all flush >/dev/null
trap 'docker exec minecraft rcon-cli save-on >/dev/null || true' EXIT
else
echo "Server not running — archiving world as-is."
fi
echo "Archiving data/world -> ${OUT}"
tar -czf "${OUT}" -C data world
echo "Done."
ls -lh "${OUT}"