Server host hardware failures and malicious script injections happen. The only difference between a minor 10-minute restore and losing months of player progress is an automated, offsite backup pipeline.
1. Automated Nightly Backup Script (Bash)
#!/usr/bin/env bash
set -e
BACKUP_DIR="/home/fivem/backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
DB_NAME="fivem_server"
mkdir -p "${BACKUP_DIR}"
# 1. Export MySQL Dump with routine and triggers
mysqldump --single-transaction --quick --routines --triggers -u fivem_user -p'YourPassword' ${DB_NAME} | gzip > "${BACKUP_DIR}/db_${TIMESTAMP}.sql.gz"
# 2. Archive custom resources
tar -czf "${BACKUP_DIR}/resources_${TIMESTAMP}.tar.gz" /home/fivem/fivem-server/server-data/resources
# 3. Prune local archives older than 7 days
find "${BACKUP_DIR}" -type f -name "*.gz" -mtime +7 -delete
echo "Backup completed successfully at ${TIMESTAMP}"