Amblem
Furkan Baytekin

Smart Game Server Backups with Bash: Hourly, Weekly, and Monthly Rotation

Create automated hourly game server backups with this simple Bash script

Smart Game Server Backups with Bash: Hourly, Weekly, and Monthly Rotation
56
4 minutes

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:

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?

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

Each backup is a compressed .tar.gz of the server directory with a unique timestamp.


Setup Instructions

  1. Save the Script Save it as backup_game_server.sh, and replace paths in SOURCE_DIR and BASE_BACKUP_DIR.
  2. Make it Executable
bash
chmod +x /path/to/backup_game_server.sh
  1. Run Manually to Test
bash
/path/to/backup_game_server.sh
  1. Set Up Cron Edit crontab:
bash
crontab -e

Add this line to run it every hour:

bash
0 * * * * /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

bash
tar -xzf /backups/game_server/hourly/game_backup_20250604_010000.tar.gz -C /tmp/restore_test
bash
chmod 700 /backups/game_server chown root:root /backups/game_server -R
bash
rsync -az /backups/game_server user@remote:/remote/location

Troubleshooting

bash
sudo 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:

Suggested Blog Posts