add backup scripts and improve docs and .env.example

This commit is contained in:
jwinterm
2025-12-22 16:29:32 -05:00
parent 7f91d83308
commit 7d701fe01b
14 changed files with 1335 additions and 702 deletions
+51
View File
@@ -0,0 +1,51 @@
#!/bin/bash
# PostgreSQL backup script for Wownerogue
# Reads configuration from environment variables (set in .env or systemd service)
set -e
# Configuration with defaults
DB_NAME="${DB_NAME:-wownerogue}"
DB_USER="${DB_USER:-postgres}"
DB_HOST="${DB_HOST:-localhost}"
DB_PORT="${DB_PORT:-5432}"
BACKUP_DIR="${BACKUP_DIR:-/var/backups/wownerogue}"
RETENTION_DAYS="${RETENTION_DAYS:-30}"
# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"
# Timestamp for filename
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/${DB_NAME}_${TIMESTAMP}.sql.gz"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}
log "Starting backup of database '$DB_NAME' on $DB_HOST:$DB_PORT..."
# Perform backup with gzip compression
if pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" | gzip > "$BACKUP_FILE"; then
BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
log "Backup completed successfully: $BACKUP_FILE ($BACKUP_SIZE)"
else
log "ERROR: Backup failed!"
rm -f "$BACKUP_FILE" # Remove partial backup
exit 1
fi
# Create a symlink to latest backup for easy access
ln -sf "$BACKUP_FILE" "$BACKUP_DIR/${DB_NAME}_LATEST.sql.gz"
# Delete backups older than retention period
log "Cleaning up backups older than $RETENTION_DAYS days..."
DELETED_COUNT=$(find "$BACKUP_DIR" -name "${DB_NAME}_[0-9]*.sql.gz" -mtime +$RETENTION_DAYS -delete -print | wc -l)
log "Deleted $DELETED_COUNT old backup(s)"
# Show disk usage
TOTAL_SIZE=$(du -sh "$BACKUP_DIR" | cut -f1)
BACKUP_COUNT=$(find "$BACKUP_DIR" -name "${DB_NAME}_[0-9]*.sql.gz" | wc -l)
log "Backup directory: $BACKUP_DIR ($TOTAL_SIZE, $BACKUP_COUNT backups)"
log "Backup process complete."
+26
View File
@@ -0,0 +1,26 @@
[Unit]
Description=Wownerogue PostgreSQL Backup
After=postgresql.service
[Service]
Type=oneshot
# Run as the user who owns the application (adjust as needed)
# User=wownerogue
# Load environment from the application's .env file
# Update this path to match your installation
EnvironmentFile=/var/www/wownerogue/src/.env
# Override backup-specific settings if needed
Environment="BACKUP_DIR=/var/backups/wownerogue"
Environment="RETENTION_DAYS=30"
# Run the backup script
# Update this path to match your installation
ExecStart=/var/www/wownerogue/scripts/backup_db.sh
# Working directory
WorkingDirectory=/var/www/wownerogue/src
[Install]
WantedBy=multi-user.target
+15
View File
@@ -0,0 +1,15 @@
[Unit]
Description=Daily Wownerogue database backup
[Timer]
# Run daily at 3 AM
OnCalendar=*-*-* 03:00:00
# Run immediately if a scheduled run was missed (e.g., system was powered off)
Persistent=true
# Add random delay up to 15 minutes to avoid exact timing
RandomizedDelaySec=900
[Install]
WantedBy=timers.target