Engineering Field guide

Automated game server backups: systemd timers and zstd

Automated game server world backups on Linux: parallel zstd compression, systemd timer scheduling, and retention policy pruning.

Automated game server backups: systemd timers and zstd

Catastrophic save loss resulting from hardware failure, corrupt mods, or accidental deletion is an administrator's worst disaster. Manual backups prove unreliable: operators inevitably forget recurring exports. Concurrently, unoptimized backup scripts often trigger severe server lag from single-threaded compression and fill 100% of host storage with unpruned archives.

This guide details the implementation of a high-performance automated backup pipeline: leveraging parallel zstd compression, strict retention pruning, and resilient scheduling via systemd timer.

Archive scripting with multithreaded Zstandard compression#

Traditional gzip compression relies on a single CPU thread and creates heavy I/O contention, causing noticeable tick drops on active game servers. Modern Zstandard (zstd) compression runs exponentially faster, utilizing available multi-core architectures with minimal impact on real-time game simulations.

Create the operational directories and backup automation script:

bash
sudo mkdir -p /opt/scripts /opt/backups/gameserver
cat << 'EOF' | sudo tee /opt/scripts/backup-gameserver.sh
#!/usr/bin/env bash
set -euo pipefail

BACKUP_DIR="/opt/backups/gameserver"
SOURCE_DIR="/opt/gameserver/saved"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
ARCHIVE_NAME="${BACKUP_DIR}/world_backup_${TIMESTAMP}.tar.zst"

mkdir -p "$BACKUP_DIR"

# Create compressed archive with multithreaded zstd
tar -I 'zstd -3 -T0' -cf "$ARCHIVE_NAME" -C "$SOURCE_DIR" .

# Verify archive integrity
zstd -t "$ARCHIVE_NAME"

# Prune archives older than 14 days
find "$BACKUP_DIR" -type f -name "world_backup_*.tar.zst" -mtime +14 -delete
EOF
sudo chmod +x /opt/scripts/backup-gameserver.sh

Compression level -3 delivers the ideal balance between processing throughput and archive density, while -T0 auto-detects all available logical processor threads.

Enforcing retention policies and pruning stale archives#

Without automated retention pruning, recurring backups inevitably trigger a No space left on device outage.

The pruning command utilizes find:

  • -type f limits actions strictly to regular files.
  • -name "world_backup_*.tar.zst" ensures unrelated files in the backup directory are preserved.
  • -mtime +14 matches archives older than 14 days.
  • -delete executes atomic filesystem unlinking.

Validate the retention query in inspection mode:

bash
find /opt/backups/gameserver -type f -name "world_backup_*.tar.zst" -mtime +14

Configuring a systemd service unit and timer#

For scheduling tasks on modern Linux systems, systemd timer units provide substantial advantages over legacy cron. Timers integrate directly with journalctl, enforce unit dependencies, and support Persistent=true (ensuring missed executions during host reboots trigger immediately upon startup).

Define the oneshot service unit:

bash
cat << 'EOF' | sudo tee /etc/systemd/system/gameserver-backup.service
[Unit]
Description=Automated Game Server World Backup
After=network.target

[Service]
Type=oneshot
ExecStart=/opt/scripts/backup-gameserver.sh
User=root
EOF

Define the recurring timer unit:

bash
cat << 'EOF' | sudo tee /etc/systemd/system/gameserver-backup.timer
[Unit]
Description=Daily Game Server Backup Timer

[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true

[Install]
WantedBy=timers.target
EOF

Activating the timer and executing an end-to-end verification#

Reload systemd configurations and enable the timer:

bash
sudo systemctl daemon-reload
sudo systemctl enable --now gameserver-backup.timer
systemctl list-timers gameserver-backup.timer

Execute a manual trigger of the backup service to confirm script permissions and archive generation:

bash
sudo systemctl start gameserver-backup.service
sudo journalctl -u gameserver-backup.service --no-pager

Verify that the target directory contains a valid, non-empty archive:

bash
ls -lh /opt/backups/gameserver/

To offload backups to geographically redundant S3 object storage and automate health telemetry, operators rely on a comprehensive server management platform, where snapshot scheduling, replication, and disaster recovery drills are managed centrally.

Sources#