Run world archive via docker exec

Stream tar from inside the container to the host, so all server
interaction (rcon + archive) happens through docker exec. Requires
the container to be running; fails fast otherwise.
This commit is contained in:
2026-04-21 20:36:27 +02:00
parent f67c8c8012
commit 90697cdb62

View File

@@ -1,7 +1,7 @@
#!/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.
# Requires the minecraft container to be running.
set -euo pipefail
cd "$(dirname "$0")"
@@ -13,17 +13,18 @@ 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..."
if ! docker ps --format '{{.Names}}' | grep -qx minecraft; then
echo "Error: minecraft container is not running." >&2
exit 1
fi
echo "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 "Archiving /data/world -> ${OUT}"
docker exec minecraft tar -czf - -C /data world > "${OUT}"
echo "Done."
ls -lh "${OUT}"