railyard/apps/ritgram/scripts/restore-database.sh
caelum0x f80b04ae0f feat(ritgram): move GitGram app into railyard as Ritgram, next to Rithub
Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram).
AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan.
Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 04:18:46 +03:00

85 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
# Database Restore Script
# This script restores a database backup
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
BACKUP_DIR="${BACKUP_DIR:-/var/backups/gitgram}"
# Load environment variables
if [ -f .env.production ]; then
export $(cat .env.production | grep -v '^#' | xargs)
fi
# Check if DATABASE_URL is set
if [ -z "$DATABASE_URL" ]; then
echo -e "${RED}❌ ERROR: DATABASE_URL is not set${NC}"
exit 1
fi
# Check if backup file is provided
if [ -z "$1" ]; then
echo -e "${YELLOW}Available backups:${NC}"
ls -lh "$BACKUP_DIR"/gitgram_backup_*.sql.gz 2>/dev/null || echo "No backups found"
echo ""
echo "Usage: $0 <backup_file>"
echo "Example: $0 gitgram_backup_20250101_120000.sql.gz"
exit 1
fi
BACKUP_FILE="$1"
# Check if backup file exists
if [ ! -f "$BACKUP_DIR/$BACKUP_FILE" ]; then
echo -e "${RED}❌ ERROR: Backup file not found: $BACKUP_DIR/$BACKUP_FILE${NC}"
exit 1
fi
# Confirmation prompt
echo -e "${YELLOW}⚠️ WARNING: This will restore the database from backup${NC}"
echo "Backup file: $BACKUP_FILE"
echo "Database: $DATABASE_URL"
echo ""
read -p "Are you sure you want to continue? (yes/no): " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
echo -e "${YELLOW}Restore cancelled${NC}"
exit 0
fi
# Create a backup of current database before restore
echo -e "${YELLOW}📦 Creating safety backup of current database...${NC}"
SAFETY_BACKUP="gitgram_pre_restore_$(date +%Y%m%d_%H%M%S).sql.gz"
pg_dump "$DATABASE_URL" | gzip > "$BACKUP_DIR/$SAFETY_BACKUP"
echo -e "${GREEN}✅ Safety backup created: $SAFETY_BACKUP${NC}"
# Restore database
echo -e "${YELLOW}🔄 Restoring database...${NC}"
gunzip -c "$BACKUP_DIR/$BACKUP_FILE" | psql "$DATABASE_URL"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ Database restored successfully${NC}"
else
echo -e "${RED}❌ Restore failed${NC}"
echo -e "${YELLOW}You can restore from safety backup: $SAFETY_BACKUP${NC}"
exit 1
fi
# Run migrations to ensure schema is up to date
echo -e "${YELLOW}🔄 Running migrations...${NC}"
cd "$(dirname "$0")/../backend"
npx prisma migrate deploy
echo ""
echo -e "${GREEN}✅ Restore process completed successfully!${NC}"
exit 0