Files
hub/scripts/backup-database.sh
Jon Leopard c8987b1c95 feat: comprehensive test data setup and account type analysis
- Add complete test data seeders with business relationships
- Create BusinessFactory for generating test businesses
- Add foundation migrations for user_type and business tables
- Fix User model with name accessor for Filament compatibility
- Switch test suite from SQLite to PostgreSQL for production parity
- Document account type recommendations based on LeafLink model
- Create migration best practices and automation scripts
- Update seeders to use updateOrCreate for idempotent runs

Test accounts now available:
- nobusiness@example.com (no business attached)
- onebusiness@example.com (single business)
- multibusiness@example.com (multiple businesses)
- dispensary@example.com (retailer user)
- brand@example.com (brand user)

All test accounts use 'password' for authentication.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-21 09:10:28 -07:00

109 lines
3.2 KiB
Bash

#!/bin/bash
# backup-database.sh
# Database backup script for Cannabrands
# Usage: ./scripts/backup-database.sh [type] [environment]
# Types: daily, pre-migration, manual
set -e
BACKUP_TYPE=${1:-manual}
ENV=${2:-local}
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DATE=$(date +%Y%m%d)
# Configuration
DB_NAME="${DB_DATABASE:-cannabrands_app}"
DB_HOST="${DB_HOST:-127.0.0.1}"
DB_USER="${DB_USERNAME:-root}"
# Backup directories
BACKUP_ROOT="backups"
DAILY_DIR="$BACKUP_ROOT/daily"
MIGRATION_DIR="$BACKUP_ROOT/pre_migration"
MANUAL_DIR="$BACKUP_ROOT/manual"
# Create directories
mkdir -p "$DAILY_DIR" "$MIGRATION_DIR" "$MANUAL_DIR"
echo "📦 Database Backup - Type: $BACKUP_TYPE, Environment: $ENV"
echo "=================================================="
# Determine backup path and filename
case $BACKUP_TYPE in
"daily")
BACKUP_FILE="$DAILY_DIR/daily_${DATE}.sql"
RETENTION_DAYS=7
;;
"pre-migration")
GIT_HASH=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
BACKUP_FILE="$MIGRATION_DIR/pre_migration_${GIT_HASH}_${TIMESTAMP}.sql"
RETENTION_DAYS=30
;;
"manual")
BACKUP_FILE="$MANUAL_DIR/manual_${TIMESTAMP}.sql"
RETENTION_DAYS=30
;;
*)
echo "❌ Error: Invalid backup type. Use: daily, pre-migration, or manual"
exit 1
;;
esac
echo "🗄️ Database: $DB_NAME"
echo "📁 Backup file: $BACKUP_FILE"
# Check if database exists and is accessible
if ! command -v pg_dump &> /dev/null; then
echo "❌ Error: pg_dump not found. Please install PostgreSQL client tools."
exit 1
fi
# Create backup
echo "⏳ Creating backup..."
if pg_dump -h "$DB_HOST" -U "$DB_USER" "$DB_NAME" > "$BACKUP_FILE" 2>/dev/null; then
echo "✅ Backup created successfully"
else
echo "❌ Backup failed!"
exit 1
fi
# Verify backup
if [ -f "$BACKUP_FILE" ] && [ -s "$BACKUP_FILE" ]; then
BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
echo "✅ Backup verified: $BACKUP_SIZE"
else
echo "❌ Backup verification failed!"
exit 1
fi
# Cleanup old backups based on retention policy
if [ "$BACKUP_TYPE" = "daily" ]; then
echo "🧹 Cleaning up old daily backups (keeping last $RETENTION_DAYS days)..."
find "$DAILY_DIR" -name "daily_*.sql" -mtime +$RETENTION_DAYS -delete 2>/dev/null || true
REMAINING=$(find "$DAILY_DIR" -name "daily_*.sql" | wc -l)
echo " Remaining daily backups: $REMAINING"
elif [ "$BACKUP_TYPE" = "pre-migration" ]; then
echo "🧹 Cleaning up old pre-migration backups (keeping last $RETENTION_DAYS days)..."
find "$MIGRATION_DIR" -name "pre_migration_*.sql" -mtime +$RETENTION_DAYS -delete 2>/dev/null || true
REMAINING=$(find "$MIGRATION_DIR" -name "pre_migration_*.sql" | wc -l)
echo " Remaining pre-migration backups: $REMAINING"
fi
# Backup summary
echo ""
echo "📊 Backup Summary:"
echo " File: $BACKUP_FILE"
echo " Size: $BACKUP_SIZE"
echo " Type: $BACKUP_TYPE"
echo " Created: $(date)"
echo ""
echo "🔧 Restore command (if needed):"
echo " psql -h $DB_HOST -U $DB_USER $DB_NAME < $BACKUP_FILE"
# Environment-specific notes
if [ "$ENV" = "production" ]; then
echo ""
echo "⚠️ Production backup complete. Verify before proceeding with changes!"
fi