Regular backups protect your server from data loss.
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>&1Save 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"Storing backups only on the same server is risky. Use rclone to sync to S3, Google Drive, or Dropbox.
sudo apt install rclone (Linux) or download for Windows.rclone config (follow prompts for your provider).# Sync to Cloud
rclone copy "$BACKUP_DIR/$DATE" "remote:fivem-backups/$DATE" --transfers=4systemctl stop fivem or close the console.gunzip < db.sql.gz | mysql -u root -p fivem_prodtar -xzf resources.tar.gz -C /opt/fivem/server-data/cache/ folder to ensure no conflicts.