Skip to content
Go back

Backing Up a Live Game Server Safely

By KingPin 15 min read
Backing Up a Live Game Server Safely
Contents

The world that loaded with holes in it

You restore last night’s backup. The server boots, you walk to spawn, and the terrain ends in a clean vertical cliff where a chunk should be. Your base is there. The chest room next to it is void. The log complains about region files.

You did back it up. You ran cp -r world /mnt/backup/ while four people were mining. The copy took 40 seconds, the server wrote to the region files during those 40 seconds, and you got a save that never existed at any single moment. Half old, half new, wholly unreliable.

The fix is two techniques used together. First, ask the game to stop writing and flush what it has, using its own console over RCON. Second, take a filesystem snapshot inside that quiet window, so the pause lasts only as long as the flush plus a snapshot, and then copy the snapshot to your backup repo at your leisure. Games that have no flush command (Valheim is the example) need a different plan, and we get to that too.

Full example: Clone the working files at github.com/KingPin/sumguy-examples/gaming/backing-up-a-live-game-server-safely

Why a plain copy tears the world

A running Minecraft server keeps chunks in memory and writes them to region files (.mca) on an autosave schedule and on demand. A copy walks the directory file by file. If the server rewrites a region file after your copy read the first half of it, you hold a file that matches neither the old state nor the new one.

Two files can also disagree with each other: level.dat from minute 10, a region file from minute 11.

A filesystem snapshot alone fixes the cross-file problem, because it freezes every file at one instant. It does not fix the in-memory problem: the snapshot contains whatever was on disk, and chunks the server has not yet written are not on disk. That is why you need both halves. The game flushes its memory to disk, and the filesystem freezes the result.

Technique 1: Make the game quiet over RCON

Minecraft Java Edition ships three commands for this job:

The Minecraft wiki is blunt about the intent: save-off exists so you can copy the level files for a backup. It also says the setting resets on restart, so a crash during backup does not leave saving off forever.

One sharp edge: save-off disables writing to the level files, except player, advancement, and stat files. Those keep updating. They are small, and a snapshot catches each one whole, but a player’s inventory file may be a few seconds newer than the world around them.

Turning RCON on

RCON is off by default in vanilla. These are the server.properties keys:

enable-rcon=true
rcon.port=25575
rcon.password=pick-something-long-and-random
broadcast-rcon-to-ops=false

rcon.port defaults to 25575. If rcon.password is blank and RCON is enabled, the server refuses to start RCON as a safeguard. broadcast-rcon-to-ops defaults to true and sends command output to every online operator. Set it to false or your players’ ops will see “Saved the game” spam every night.

The protocol is not encrypted, and the wiki says to connect from localhost where you can. Never publish port 25575 to the internet.

With the itzg Docker image

If you run itzg/minecraft-server, you already have RCON. The image documentation says RCON is enabled by default and used for graceful shutdown and for coordinating save state during backups. The image ships an rcon-cli binary that picks up its connection settings inside the container, so you run it through docker exec:

Terminal window
docker exec mc rcon-cli save-off
docker exec mc rcon-cli save-all flush
# ...copy or snapshot here...
docker exec mc rcon-cli save-on

The container is named mc. One-shot commands need no -i. Use docker exec -i mc rcon-cli when you want an interactive console. Set your own password with RCON_PASSWORD (or RCON_PASSWORD_FILE with Docker secrets, which the image docs recommend).

With a bare-metal server

Use mcrcon. Its flags are -H (host, default localhost), -P (port, default 25575), and -p (password). It also reads MCRCON_HOST, MCRCON_PORT, and MCRCON_PASS, which keeps the password out of your process list:

Terminal window
export MCRCON_PASS='pick-something-long-and-random'
mcrcon -s "save-off" "save-all flush"
# ...snapshot...
mcrcon -s "save-on"

The -s flag is silent mode. Several quoted commands run in order.

Stop here and you have a manual recipe: save-off, save-all flush, cp -a, save-on. The copy still takes as long as your world is big, and saving stays off the whole time. The snapshot fixes that.

Technique 2: Snapshot inside the quiet window

A ZFS snapshot takes effectively no time and no space at creation. You pay only for blocks that change afterward. So the window looks like this:

  1. save-off
  2. save-all flush
  3. zfs snapshot (milliseconds)
  4. save-on

The server runs frozen for the flush and the snapshot, then carries on. Everything slow, meaning reading the world and shipping it to a backup repo, happens afterward from the snapshot while the server runs normally.

Assume the world lives on a dataset called tank/games/minecraft, mounted at /srv/minecraft. By default every ZFS dataset exposes its snapshots read-only under a hidden .zfs/snapshot/ directory at the mount point, so a snapshot named restic-tmp appears at /srv/minecraft/.zfs/snapshot/restic-tmp/. You can browse it, ls it, and point a backup tool at it.

Terminal window
zfs snapshot tank/games/minecraft@restic-tmp
ls /srv/minecraft/.zfs/snapshot/restic-tmp/
zfs destroy tank/games/minecraft@restic-tmp

Btrfs works too

On Btrfs the same idea is a read-only subvolume snapshot: btrfs subvolume snapshot -r /srv/minecraft /srv/snap/minecraft-tmp, then btrfs subvolume delete it afterward. The world directory must be its own subvolume. Our Btrfs send/receive post covers the mechanics. The rest of this post uses ZFS because the .zfs/snapshot path keeps the script short.

The script

This script does the whole dance and refuses to leave saving off. The trap is the important part: if zfs snapshot fails (dataset renamed, pool full of snapshots, a typo), the script still sends save-on on its way out. Without that, one failed night leaves the server not saving until the next restart, and a crash then loses everything since.

game-backup.sh
#!/usr/bin/env bash
set -euo pipefail
CONTAINER=mc
DATASET=tank/games/minecraft
MOUNT=/srv/minecraft
SNAP=restic-tmp
export RESTIC_REPOSITORY=/mnt/backup/mc-restic
export RESTIC_PASSWORD_FILE=/etc/restic/password
rcon() { docker exec "$CONTAINER" rcon-cli "$@"; }
saves_off=0
cleanup() {
# Always turn saving back on, even if the snapshot failed.
if [ "$saves_off" -eq 1 ]; then
rcon save-on || echo "WARNING: save-on failed, fix this now" >&2
fi
zfs destroy "$DATASET@$SNAP" 2>/dev/null || true
}
trap cleanup EXIT
# Leftover snapshot from a crashed run would make zfs snapshot fail.
zfs destroy "$DATASET@$SNAP" 2>/dev/null || true
# Quiet window starts here.
saves_off=1
rcon save-off
rcon save-all flush
zfs snapshot "$DATASET@$SNAP"
rcon save-on
saves_off=0
# Quiet window ends here. The server is writing again.
restic backup "$MOUNT/.zfs/snapshot/$SNAP" \
--tag minecraft --host mc-server
restic forget --tag minecraft --host mc-server \
--keep-hourly 24 --keep-daily 7 --keep-weekly 4

A few notes on the choices:

Run it from a systemd timer or plain cron. Hourly is reasonable, because after the first run restic reads only changed region files:

0 * * * * /usr/local/bin/game-backup.sh >> /var/log/game-backup.log 2>&1

Test the trap once on purpose: set DATASET to a typo, run the script, and confirm the server re-enables saving.

Gotchas worth knowing

The itzg backup sidecar, if you prefer containers

The same author publishes itzg/mc-backup, a companion container. Its README says it coordinates backups by using RCON to flush data, pause writes, and resume afterward. The documented order is save-off, save-all, then your pre-backup script, the backup, and finally save-on.

It supports tar (the default), rsync, rclone, and restic as BACKUP_METHOD values. The variables you will touch first:

docker-compose.yml (excerpt)
backups:
image: itzg/mc-backup
hostname: mc-backups
environment:
RCON_HOST: mc
RCON_PASSWORD_FILE: /run/secrets/rcon_pass
BACKUP_INTERVAL: "2h"
BACKUP_METHOD: restic
RESTIC_REPOSITORY: /backups/restic
RESTIC_PASSWORD_FILE: /run/secrets/restic_pass
PRUNE_RESTIC_RETENTION: "--keep-daily 7 --keep-weekly 4"
volumes:
- ./data:/data:ro
- ./backups:/backups

Its defaults are SRC_DIR=/data, RCON_PORT=25575, RCON_PASSWORD=minecraft, and BACKUP_INTERVAL=24h. The default RCON password is a real default, so set your own. The README also warns to give the container a fixed hostname when using restic, or each restart gets a new random hostname and old backups never rotate.

The tradeoff: the sidecar copies the live directory, so saves stay off for the whole read. That is fine for a modest world on ext4. On a large one, use the snapshot script.

Games with no flush command: the Valheim problem

Valheim has no save-off and no scriptable console. You cannot ask the server to pause writing, so it does its own thing.

It saves on a timer and keeps rotating backups of its own. These are start parameters on the server launch line, with defaults quoted from Iron Gate’s manual:

FlagDefaultMeaning
-saveinterval1800Seconds between world saves (30 minutes)
-backups4Automatic backups to keep
-backupshort7200Seconds before the first backup interval (2 hours)
-backuplong43200Seconds between the later backups (12 hours)

With defaults you get one backup about 2 hours old and three more roughly 12 hours apart. Those files sit next to the world, so they die with the disk, and they only reach back about a day and a half.

On Linux the world lives under ~/.config/unity3d/IronGate/Valheim/worlds_local/ unless you pass -savedir. Older worlds are a pair of files, <name>.fwl (metadata) and <name>.db (the data). Valheim 1.0 stores each world as a directory of many files instead. Check your own worlds_local before you script anything.

That directory change makes torn copies worse. The widely used valheim-server-docker project notes that in the 1.0 layout a copy taken mid-save can capture an index that refers to files the server already replaced. It handles this by watching for _main.<n>.ok marker files the server writes after each committed save, and retrying the backup if a save landed during the copy.

What I would actually do

Be opinionated: do not copy the live world directory and hope.

  1. Best: snapshot, then copy the snapshot. A ZFS or Btrfs snapshot freezes all files at one instant, so the cross-file tearing problem disappears. Run it right after a known save (the server saves every -saveinterval seconds, 30 minutes by default). The snapshot may land mid-write, in which case you restore the previous automatic backup instead. Keep the game’s own backup files in the snapshot too so you have a fallback.
  2. Good: stop, copy, start. Valheim also saves on shutdown. Stop the server for a minute overnight, copy, start.
  3. Acceptable: back up the game’s own completed backup files. Copy them off the machine with restic, at a time well clear of the next save, and test-load one before you trust the routine. The cost is staleness: the newest one can be hours old.

Skip the bare rsync of the live directory every 10 minutes. That is the torn-world generator from the top of this post, on a timer.

The boring fallback: stop, tar, start

If your server can afford 30 to 90 seconds of downtime, stop the game, archive the directory, and start it again. Nothing is in flight, so nothing tears. No RCON, no snapshots, no trap.

LinuxGSM’s backup command is exactly this. Its docs describe it as creating .tar.gz archives of the game server, and the example output warns that the server will be stopped during the backup. LinuxGSM also has a stoponbackup setting if you want to change that behavior, which I would not for a game with no flush command. We cover the full setup in the LinuxGSM post:

Terminal window
./gameserver backup

Put it in a nightly timer and ship the archives with restic or rclone. For a small private server, this beats every clever option here on one metric: you will keep running it.

Test the restore or you have a guess

A backup you have never restored is a hypothesis. Do the restore test once now, then once a quarter.

Terminal window
restic snapshots --tag minecraft
restic restore latest --tag minecraft --target /tmp/mc-restore
find /tmp/mc-restore -maxdepth 6 -name level.dat

restic restores the original path under the target directory, so your world sits at something like /tmp/mc-restore/srv/minecraft/.zfs/snapshot/restic-tmp/. Start a throwaway container on a different port against that copy, with its own name and no access to the real data directory:

Terminal window
docker run --rm -d --name mc-restore-test \
-e EULA=TRUE -p 25566:25565 \
-v /tmp/mc-restore/srv/minecraft/.zfs/snapshot/restic-tmp:/data \
itzg/minecraft-server
docker logs -f mc-restore-test

Look for a clean boot and no region file errors, then walk to your base and its neighbors. Check that server.properties, plugin configs, and whitelist.json came back too.

Which one should you use?

A snapshot on the same pool is not a backup. It is an undo button. Get a second copy off the box.

Common Questions

Can I back up a Minecraft server while players are online?

Yes. Run save-off, then save-all flush, take your snapshot or copy, and run save-on. Players stay connected the whole time. The flush freezes the server briefly, so a large world gets one short hitch.

Does save-off work on Bedrock Edition?

No. The save-off and save-on commands are Java Edition only. Bedrock Dedicated Server uses save hold to prepare a backup, save query to check readiness, and save resume to turn writing back on. The same snapshot idea applies, but your script needs to poll save query until the files are ready before it takes the snapshot, which makes the quiet window slightly longer than on Java.

Do I need ZFS to back up a game server this way?

No. ZFS only makes the quiet window short. Btrfs subvolume snapshots and LVM snapshots do the same job. Without any of them, you can still use save-off, save-all flush, a plain cp or restic backup of the live directory, and save-on, but saving stays off for the whole copy, so large worlds hold more unsaved changes in RAM for longer.

How much disk space do snapshots use?

Almost none at first. A ZFS snapshot costs zero bytes when created and grows only as the live files change, since it keeps the old blocks. A snapshot that exists only while restic reads it stays small. Delete the snapshot right after the backup, as the script does.

What happens if the server crashes while saving is off?

The setting resets on restart, so saving comes back on after a reboot. The risk is lost progress: chunks changed since save-off were held in memory and are gone after a crash. That is why the script runs save-on through a trap and keeps the off window to the length of one flush, not minutes.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Next Post
Debian 13 + ZFS on a QNAP TS-451

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts