diff --git a/package.json b/package.json index 6b53462..daaef33 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,9 @@ "db:push:local": "drizzle-kit push", "dev": "bun --watch bot/index.ts", "db:studio": "drizzle-kit studio --port 4983 --host 0.0.0.0", - "studio:remote": "bash shared/scripts/remote-studio.sh", - "dashboard:remote": "bash shared/scripts/remote-dashboard.sh", "remote": "bash shared/scripts/remote.sh", + "logs": "bash shared/scripts/logs.sh", + "db:backup": "bash shared/scripts/db-backup.sh", "test": "bun test", "docker:cleanup": "bash shared/scripts/docker-cleanup.sh" }, diff --git a/shared/scripts/db-backup.sh b/shared/scripts/db-backup.sh new file mode 100755 index 0000000..9468ebb --- /dev/null +++ b/shared/scripts/db-backup.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# ============================================================================= +# Aurora Database Backup Script +# ============================================================================= + +set -e + +# Load environment variables +if [ -f .env ]; then + set -a + source .env + set +a +fi + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +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 + cd "$BACKUP_DIR" + ls -t backup_*.sql | tail -n +11 | xargs -r rm -- + 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 diff --git a/shared/scripts/db-restore.sh b/shared/scripts/db-restore.sh new file mode 100755 index 0000000..1032cff --- /dev/null +++ b/shared/scripts/db-restore.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# ============================================================================= +# Aurora Database Restore Script +# ============================================================================= +# Usage: ./db-restore.sh [path/to/backup.sql] +# ============================================================================= + +set -e + +# Load environment variables +if [ -f .env ]; then + set -a + source .env + set +a +fi + +# Colors +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' + +if [ -z "$1" ]; then + echo -e "${RED}Error: Please specify the backup file to restore.${NC}" + echo "Usage: ./db-restore.sh " + echo "Available backups:" + ls -lh shared/db/backups/*.sql 2>/dev/null || echo " (No backups found in shared/db/backups)" + exit 1 +fi + +BACKUP_FILE="$1" + +if [ ! -f "$BACKUP_FILE" ]; then + echo -e "${RED}Error: File not found: $BACKUP_FILE${NC}" + exit 1 +fi + +echo -e "${YELLOW}⚠️ WARNING: This will OVERWRITE the current database!${NC}" +echo -e "Target Database: ${DB_NAME:-auroradev}" +echo -e "Backup File: $BACKUP_FILE" +echo "" +read -p "Are you sure you want to proceed? (y/N): " -n 1 -r +echo "" + +if [[ $REPLY =~ ^[Yy]$ ]]; then + echo -e "${YELLOW}♻️ Restoring database...${NC}" + + if docker ps | grep -q aurora_db; then + # Drop and recreate public schema to ensure clean slate, then restore + # Note: dependent on how the dump was created. Standard pg_dump usually includes CREATE commands if configured, + # but often it's data only or structure+data. + # For safety, we'll just pipe the file to psql. + + cat "$BACKUP_FILE" | docker exec -i aurora_db psql -U "${DB_USER:-auroradev}" -d "${DB_NAME:-auroradev}" + + echo -e " ${GREEN}✓${NC} Restore complete!" + else + echo -e "${RED}Error: Database container (aurora_db) is not running!${NC}" + exit 1 + fi +else + echo "Operation cancelled." + exit 0 +fi diff --git a/shared/scripts/logs.sh b/shared/scripts/logs.sh new file mode 100755 index 0000000..52aa141 --- /dev/null +++ b/shared/scripts/logs.sh @@ -0,0 +1,38 @@ +#!/bin/bash +# ============================================================================= +# Aurora Log Viewer +# ============================================================================= +# Usage: ./logs.sh [app|db|all] [-f] +# Default: app container, follow mode +# ============================================================================= + +SERVICE=${1:-app} +FOLLOW="-f" + +if [[ "$1" == "-f" ]]; then + SERVICE="app" + FOLLOW="-f" +elif [[ "$2" == "-f" ]]; then + FOLLOW="-f" +elif [[ "$2" == "--no-follow" ]]; then + FOLLOW="" +fi + +echo "📋 Fetching logs for service: $SERVICE..." + +case $SERVICE in + app) + docker compose logs $FOLLOW app + ;; + db) + docker compose logs $FOLLOW db + ;; + all) + docker compose logs $FOLLOW + ;; + *) + echo "Unknown service: $SERVICE" + echo "Usage: ./logs.sh [app|db|all] [-f]" + exit 1 + ;; +esac diff --git a/shared/scripts/remote-dashboard.sh b/shared/scripts/remote-dashboard.sh deleted file mode 100755 index 0cc6ad2..0000000 --- a/shared/scripts/remote-dashboard.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -# Load environment variables -if [ -f .env ]; then - set -a - source .env - set +a -fi - -if [ -z "$VPS_HOST" ] || [ -z "$VPS_USER" ]; then - echo "Error: VPS_HOST and VPS_USER must be set in .env" - echo "Please add them to your .env file:" - echo "VPS_USER=your-username" - echo "VPS_HOST=your-ip-address" - exit 1 -fi - -DASHBOARD_PORT=${DASHBOARD_PORT:-3000} - -echo "🌐 Establishing secure tunnel to Aurora Dashboard..." -echo "📊 Dashboard will be accessible at: http://localhost:$DASHBOARD_PORT" -echo "Press Ctrl+C to stop the connection." -echo "" - -# Function to open browser (cross-platform) -open_browser() { - sleep 2 - if command -v open &> /dev/null; then - open "http://localhost:$DASHBOARD_PORT" - elif command -v xdg-open &> /dev/null; then - xdg-open "http://localhost:$DASHBOARD_PORT" - fi -} - -# Check if autossh is available -if command -v autossh &> /dev/null; then - SSH_CMD="autossh -M 0 -o ServerAliveInterval=30 -o ServerAliveCountMax=3" -else - SSH_CMD="ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=3" -fi - -open_browser & -$SSH_CMD -N -L $DASHBOARD_PORT:127.0.0.1:$DASHBOARD_PORT $VPS_USER@$VPS_HOST diff --git a/shared/scripts/remote-studio.sh b/shared/scripts/remote-studio.sh deleted file mode 100755 index 6ab9123..0000000 --- a/shared/scripts/remote-studio.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -# Load environment variables -if [ -f .env ]; then - # export $(grep -v '^#' .env | xargs) # Use a safer way if possible, but for simple .env this often works. - # Better way to source .env without exporting everything to shell if we just want to use them in script: - set -a - source .env - set +a -fi - -if [ -z "$VPS_HOST" ] || [ -z "$VPS_USER" ]; then - echo "Error: VPS_HOST and VPS_USER must be set in .env" - echo "Please add them to your .env file:" - echo "VPS_USER=your-username" - echo "VPS_HOST=your-ip-address" - exit 1 -fi - -echo "🔮 Establishing secure tunnel to Drizzle Studio..." -echo "" -echo "📚 Open this URL in your browser:" -echo " https://local.drizzle.studio?host=127.0.0.1&port=4983" -echo "" -echo "💡 Note: Drizzle Studio works via their proxy service, not direct localhost." -echo "Press Ctrl+C to stop the connection." - -# -N means "Do not execute a remote command". -L is for local port forwarding. -ssh -N -L 4983:127.0.0.1:4983 $VPS_USER@$VPS_HOST