Backups
Regular backups protect your server from data loss.
What to Backup
- Database: The most critical component. Contains player data, money, vehicles, etc.
- Resources folder: Contains your scripts and assets.
- server.cfg: Contains critical configuration.
- Log files: Useful for auditing but can be excluded if space is tight.
Automated Backups
Linux (Bash Script)
Save as backup.sh and make executable (chmod +x backup.sh).
#!/bin/bash
# Configuration
BACKUP_DIR="/opt/fivem/backups"
SERVER_DIR="/opt/fivem/server-data"
DB_USER="fivem"
DB_PASS="password"
DB_NAME="fivem_prod"
DATE=$(date +%Y%m%d_%H%M%S)
RETENTION_DAYS=7
# Create directory
mkdir -p "$BACKUP_DIR/$DATE"
# 1. Backup Database
echo "Backing up database..."
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME | gzip > "$BACKUP_DIR/$DATE/db.sql.gz"
# 2. Backup Resources (exclude cache/node_modules to save space)
echo "Backing up resources..."
tar --exclude='cache' --exclude='node_modules' --exclude='.git' -czf "$BACKUP_DIR/$DATE/resources.tar.gz" -C "$SERVER_DIR" resources server.cfg
# 3. Cleanup Old Backups
echo "Cleaning up old backups..."
find "$BACKUP_DIR" -type d -mtime +$RETENTION_DAYS -exec rm -rf {} +
echo "Backup complete: $DATE"Schedule with crontab -e:
0 4 * * * /opt/fivem/scripts/backup.sh > /var/log/fivem_backup.log 2>&1Windows (PowerShell Script)
Save as backup.ps1.
# Configuration
$BackupDir = "C:\FiveM\Backups"
$ServerDir = "C:\FiveM\ServerData"
$DbUser = "root"
$DbPass = "password"
$DbName = "fivem_prod"
$Date = Get-Date -Format "yyyyMMdd_HHmmss"
$RetentionDays = 7
# Create directory
$TargetDir = Join-Path $BackupDir $Date
New-Item -ItemType Directory -Force -Path $TargetDir | Out-Null
# 1. Backup Database
Write-Host "Backing up database..."
# Ensure mysqldump is in your PATH or provide full path
& "mysqldump.exe" -u $DbUser -p$DbPass $DbName > "$TargetDir\db.sql"
# 2. Backup Resources
Write-Host "Backing up resources..."
$Exclude = @("cache", "node_modules", ".git")
Compress-Archive -Path "$ServerDir\resources", "$ServerDir\server.cfg" -DestinationPath "$TargetDir\resources.zip"
# 3. Cleanup Old Backups
Write-Host "Cleaning up old backups..."
Get-ChildItem -Path $BackupDir -Directory | Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-$RetentionDays) } | Remove-Item -Recurse -Force
Write-Host "Backup complete: $Date"Off-Site Backups (Cloud)
Storing backups only on the same server is risky. Use rclone to sync to S3, Google Drive, or Dropbox.
- Install rclone:
sudo apt install rclone(Linux) or download for Windows. - Configure:
rclone config(follow prompts for your provider). - Add to Script:
# Sync to Cloud
rclone copy "$BACKUP_DIR/$DATE" "remote:fivem-backups/$DATE" --transfers=4Restore Procedures
- Stop Server:
systemctl stop fivemor close the console. - Restore Database:
gunzip < db.sql.gz | mysql -u root -p fivem_prod - Restore Resources:
tar -xzf resources.tar.gz -C /opt/fivem/server-data/ - Clear Cache: Delete the
cache/folder to ensure no conflicts. - Start Server: Start and check logs for errors.
Best Practices
- Test Restores: Once a month, try restoring your backup to a local dev server to ensure the files aren’t corrupt.
- Encryption: If backups contain sensitive data (keys, player IPs), encrypt them.
- 3-2-1 Rule: 3 copies of data, 2 different media types, 1 off-site.
Last updated on