Game servers store precious player progress and in-game assets. A crash or file corruption can ruin that data—and your community’s trust. Backups are the insurance policy you can’t skip. But storing every backup forever isn’t practical. That’s where a rotating backup system shines—keeping only the most recent hourly, weekly, and monthly snapshots.
This guide walks you through building a simple Bash-based backup system that stores:
- The last 5 hourly backups
- The last 3 weekly backups
- The last 2 monthly backups
Designed for game developers and server admins, this guide offers a real working script and a step-by-step setup. Clean, SEO-friendly, and practical.
Why Rotate Hourly, Weekly, and Monthly?
- Hourly backups help restore recent player progress quickly.
- Weekly snapshots are useful for detecting bugs or data drift.
- Monthly archives let you go back further in time if needed.
- Rotating copies keep disk usage under control—no cleanup headaches.
Let’s dive into the Bash script.
The Bash Script
Here’s a Bash script that handles hourly, weekly, and monthly backups with rotation logic.
bash#!/bin/bash
# === CONFIGURATION ===
SOURCE_DIR="/path/to/game_server"
BASE_BACKUP_DIR="/backups/game_server"
MAX_HOURLY=5
MAX_WEEKLY=3
MAX_MONTHLY=2
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DAY_OF_WEEK=$(date +%u) # 1 (Monday) to 7 (Sunday)
DAY_OF_MONTH=$(date +%d) # 01 to 31
# === HOURLY BACKUP ===
HOURLY_DIR="$BASE_BACKUP_DIR/hourly"
mkdir -p "$HOURLY_DIR"
HOURLY_FILE="$HOURLY_DIR/game_backup_$TIMESTAMP.tar.gz"
tar -czf "$HOURLY_FILE" "$SOURCE_DIR"
cd "$HOURLY_DIR" && ls -tp | grep -v '/$' | tail -n +$((MAX_HOURLY + 1)) | xargs -I {} rm -- {}
# === WEEKLY BACKUP (runs on Sundays) ===
if [ "$DAY_OF_WEEK" -eq 7 ]; then
WEEKLY_DIR="$BASE_BACKUP_DIR/weekly"
mkdir -p "$WEEKLY_DIR"
WEEKLY_FILE="$WEEKLY_DIR/game_backup_$TIMESTAMP.tar.gz"
tar -czf "$WEEKLY_FILE" "$SOURCE_DIR"
cd "$WEEKLY_DIR" && ls -tp | grep -v '/$' | tail -n +$((MAX_WEEKLY + 1)) | xargs -I {} rm -- {}
fi
# === MONTHLY BACKUP (runs on 1st) ===
if [ "$DAY_OF_MONTH" -eq 01 ]; then
MONTHLY_DIR="$BASE_BACKUP_DIR/monthly"
mkdir -p "$MONTHLY_DIR"
MONTHLY_FILE="$MONTHLY_DIR/game_backup_$TIMESTAMP.tar.gz"
tar -czf "$MONTHLY_FILE" "$SOURCE_DIR"
cd "$MONTHLY_DIR" && ls -tp | grep -v '/$' | tail -n +$((MAX_MONTHLY + 1)) | xargs -I {} rm -- {}
fi
How It Works
- Hourly: Always runs. Keeps the latest 5 backups.
- Weekly: Runs on Sundays. Keeps 3 most recent weeks.
- Monthly: Runs on the 1st of each month. Keeps 2 most recent months.
Each backup is a compressed .tar.gz
of the server directory with a unique timestamp.
Setup Instructions
-
Save the Script
Save it as
backup_game_server.sh
, and replace paths inSOURCE_DIR
andBASE_BACKUP_DIR
. - Make it Executable
bashchmod +x /path/to/backup_game_server.sh
- Run Manually to Test
bash/path/to/backup_game_server.sh
- Set Up Cron Edit crontab:
bashcrontab -e
Add this line to run it every hour:
bash0 * * * * /path/to/backup_game_server.sh
Example Directory Structure
Your /backups/game_server/
folder will look like this:
/backups/game_server/
├── hourly/
│ ├── game_backup_20250604_010000.tar.gz
│ ├── ...
├── weekly/
│ ├── game_backup_20250602_000000.tar.gz
│ ├── ...
├── monthly/
│ ├── game_backup_20250601_000000.tar.gz
│ ├── ...
Older files beyond the retention limit are automatically deleted.
Pro Tips
-
Check Disk Usage: Run
du -sh /backups/game_server/*
to monitor space. - Restore Easily:
bashtar -xzf /backups/game_server/hourly/game_backup_20250604_010000.tar.gz -C /tmp/restore_test
- Secure Your Backups:
bashchmod 700 /backups/game_server
chown root:root /backups/game_server -R
- Offsite Sync:
bashrsync -az /backups/game_server user@remote:/remote/location
Troubleshooting
-
Cron Not Running?
Check logs:
/var/log/syslog
(Debian/Ubuntu)/var/log/cron
(RHEL/CentOS)
- Permission Errors? Make sure the user running the script can access all relevant directories.
-
Missing
tar
? Install it:
bashsudo apt install tar
Final Words
This Bash script gives you a clean, automatic, and rotating backup system for your game server. It’s easy to integrate with cron and doesn’t clutter your storage. You can sleep better knowing you’ve got hourly, weekly, and monthly fallbacks—without lifting a finger.
Album of the day: