Skip to main content
Losing data is a nightmare. With this simple bash script, you can create compressed archives of your important files and keep them organized.

The Script

Create a file called backup.sh: nano backup.sh Paste the following code:
#!/bin/bash

# --- CONFIGURATION ---
# Folder to save (e.g., the Minecraft server folder)
SOURCE_DIR="/home/minecraft/server"

# Folder where to save backups
BACKUP_DIR="/home/minecraft/backups"

# Backup name (with date and time)
DATE=$(date +%Y-%m-%d_%H-%M)
BACKUP_NAME="backup_$DATE.tar.gz"

# Days after which to delete old backups
RETENTION_DAYS=7
# ----------------------

# Create backup folder if it doesn't exist
mkdir -p $BACKUP_DIR

echo "Starting backup of $SOURCE_DIR..."

# Create compressed archive
tar -czf "$BACKUP_DIR/$BACKUP_NAME" "$SOURCE_DIR"

echo "Backup completed: $BACKUP_DIR/$BACKUP_NAME"

# Remove backups older than X days
echo "Removing backups older than $RETENTION_DAYS days..."
find "$BACKUP_DIR" -type f -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete

echo "Operation completed!"

How to Use It

  1. Save and Close: Press CTRL+O, Enter, then CTRL+X.
  2. Make the script executable: chmod +x backup.sh
  3. Run it manually: ./backup.sh

Automating with Cron (Scheduling)

You can have this script run automatically every day (e.g., at 4:00 AM).
  1. Open crontab: crontab -e
  2. Add this line to the end of the file: 0 4 * * * /path/to/your/backup.sh (Make sure to use the full path, e.g., /home/user/backup.sh)
  3. Save and close.
Now your server will automatically backup every night at 04:00 and automatically delete backups older than 7 days to avoid filling up the disk!
Guide created by HighMark - All information and contacts on my official website: Highmark.it