Files
aurorabot/shared/scripts/db-backup.sh
syntaxbullet 942875e8d0
All checks were successful
Deploy to Production / test (push) Successful in 34s
fix: replace 'source .env' with safe env loader in all scripts
The raw 'source .env' pattern breaks when values contain special bash
characters like ) in passwords or database URLs. This caused deploy:remote
to fail with 'syntax error near unexpected token )'.

Changes:
- Created shared/scripts/lib/load-env.sh: reads .env line-by-line with
  export instead of source, safely handling special characters
- Updated db-backup.sh, db-restore.sh, deploy-remote.sh, remote.sh to
  use the shared loader
- Reordered deploy-remote.sh: git pull now runs first (step 1) so the
  remote always has the latest scripts before running backup (step 2)
2026-02-13 14:46:30 +01:00

62 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# =============================================================================
# Aurora Database Backup Script
# =============================================================================
# Creates a timestamped PostgreSQL backup and retains the last 10 backups.
#
# Usage: ./db-backup.sh
# =============================================================================
set -e
# Load environment variables safely
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/load-env.sh"
load_env
PROJECT_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)"
BACKUP_DIR="$PROJECT_DIR/shared/db/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/backup_$TIMESTAMP.sql"
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${YELLOW}💾 Starting database backup...${NC}"
mkdir -p "$BACKUP_DIR"
if docker ps | grep -q aurora_db; then
# Try to dump the database
if docker exec aurora_db pg_dump -U "${DB_USER:-auroradev}" "${DB_NAME:-auroradev}" > "$BACKUP_FILE"; then
# Check if backup file is not empty
if [ -s "$BACKUP_FILE" ]; then
echo -e " ${GREEN}${NC} Backup successful!"
echo -e " 📂 File: $BACKUP_FILE"
echo -e " 📏 Size: $(du -h "$BACKUP_FILE" | cut -f1)"
# Keep only last 10 backups (cross-platform: works on macOS and Linux)
cd "$BACKUP_DIR"
OLD_BACKUPS=$(ls -t backup_*.sql 2>/dev/null | tail -n +11)
if [ -n "$OLD_BACKUPS" ]; then
echo "$OLD_BACKUPS" | xargs rm --
echo -e " ${GREEN}${NC} Pruned old backups"
fi
else
echo -e " ${RED}${NC} Backup created but empty. Something went wrong."
rm -f "$BACKUP_FILE"
exit 1
fi
else
echo -e " ${RED}${NC} pg_dump failed."
rm -f "$BACKUP_FILE"
exit 1
fi
else
echo -e " ${RED}${NC} Database container (aurora_db) is not running!"
exit 1
fi