Fall back to host tar when container is down

When the minecraft container is not running, archive data/world
directly from the host instead of failing — the files are already
consistent on disk with no process writing to them.
This commit is contained in:
2026-04-21 20:37:26 +02:00
parent 90697cdb62
commit 429bcdb1bf

View File

@@ -1,7 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Usage: ./backup.sh [label] # Usage: ./backup.sh [label]
# Creates backups/world-YYYY-MM-DD-HHMM[-label].tar.gz # Creates backups/world-YYYY-MM-DD-HHMM[-label].tar.gz
# Requires the minecraft container to be running. # If the container is running, flushes world via rcon and archives via
# docker exec. If not, archives data/world from the host directly.
set -euo pipefail set -euo pipefail
cd "$(dirname "$0")" cd "$(dirname "$0")"
@@ -13,18 +14,18 @@ OUT="backups/world-${STAMP}${SUFFIX}.tar.gz"
mkdir -p backups mkdir -p backups
if ! docker ps --format '{{.Names}}' | grep -qx minecraft; then if docker ps --format '{{.Names}}' | grep -qx minecraft; then
echo "Error: minecraft container is not running." >&2 echo "Server running — flushing world via rcon..."
exit 1
fi
echo "Flushing world via rcon..."
docker exec minecraft rcon-cli save-off >/dev/null docker exec minecraft rcon-cli save-off >/dev/null
docker exec minecraft rcon-cli save-all flush >/dev/null docker exec minecraft rcon-cli save-all flush >/dev/null
trap 'docker exec minecraft rcon-cli save-on >/dev/null || true' EXIT trap 'docker exec minecraft rcon-cli save-on >/dev/null || true' EXIT
echo "Archiving /data/world -> ${OUT}" echo "Archiving /data/world -> ${OUT}"
docker exec minecraft tar -czf - -C /data world > "${OUT}" docker exec minecraft tar -czf - -C /data world > "${OUT}"
else
echo "Server not running — archiving data/world from host."
tar -czf "${OUT}" -C data world
fi
echo "Done." echo "Done."
ls -lh "${OUT}" ls -lh "${OUT}"