db scripts

This commit is contained in:
Jose Henrique 2025-06-19 20:44:11 -03:00
parent e62713b377
commit 4b2e2a764b
2 changed files with 115 additions and 21 deletions

56
backup-db.sh Normal file
View File

@ -0,0 +1,56 @@
#!/bin/bash
# OpenCand Database Backup Script
# This script creates a backup of the PostgreSQL database (excluding materialized views)
set -e # Exit on any error
# Configuration
DB_CONTAINER="opencand_db"
DB_NAME="opencand"
DB_USER="root"
BACKUP_FILE="backup_$(date +%Y%m%d_%H%M%S).sql"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}💾 OpenCand Database Backup Script${NC}"
echo "=================================="
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo -e "${RED}❌ Error: Docker is not running or not accessible${NC}"
exit 1
fi
# Check if the database container is running
if ! docker ps --format "table {{.Names}}" | grep -q "^${DB_CONTAINER}$"; then
echo -e "${RED}❌ Error: Database container '${DB_CONTAINER}' is not running${NC}"
echo "Please start the services with: docker-compose up -d"
exit 1
fi
echo -e "${YELLOW}📋 Creating backup (excluding materialized views)...${NC}"
# Get list of materialized views
MATVIEWS=$(docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -Atc "SELECT schemaname || '.' || matviewname FROM pg_matviews;")
# Build exclude flags for pg_dump
EXCLUDE_FLAGS=""
for mv in $MATVIEWS; do
EXCLUDE_FLAGS+=" --exclude-table=$mv"
done
# Run pg_dump excluding materialized views
if docker exec $DB_CONTAINER pg_dump -U $DB_USER -d $DB_NAME $EXCLUDE_FLAGS > "$BACKUP_FILE"; then
echo -e "${GREEN}✅ Backup created: $BACKUP_FILE${NC}"
else
echo -e "${RED}❌ Error: Failed to create backup${NC}"
exit 1
fi
echo -e "${GREEN}🎉 Backup completed successfully!${NC}"
echo "=================================="

View File

@ -11,6 +11,8 @@ DB_NAME="opencand"
DB_USER="root" DB_USER="root"
SQL_URL="https://git.ivanch.me/ivanch/opencand/raw/branch/main/db/db.sql" SQL_URL="https://git.ivanch.me/ivanch/opencand/raw/branch/main/db/db.sql"
SQL_FILE="./db.sql" SQL_FILE="./db.sql"
MV_URL="https://git.ivanch.me/ivanch/opencand/raw/branch/main/db/mv.sql"
MV_FILE="./mv.sql"
# Colors for output # Colors for output
RED='\033[0;31m' RED='\033[0;31m'
@ -34,20 +36,32 @@ if ! docker ps --format "table {{.Names}}" | grep -q "^${DB_CONTAINER}$"; then
exit 1 exit 1
fi fi
# Download the SQL file # Download the SQL files
echo -e "${YELLOW}📥 Downloading SQL file...${NC}" echo -e "${YELLOW}📥 Downloading SQL files...${NC}"
if command -v curl &> /dev/null; then if command -v curl &> /dev/null; then
if curl -L -o "$SQL_FILE" "$SQL_URL"; then if curl -L -o "$SQL_FILE" "$SQL_URL"; then
echo "✅ SQL file downloaded successfully" echo "✅ db.sql downloaded successfully"
else else
echo -e "${RED}❌ Error: Failed to download SQL file from ${SQL_URL}${NC}" echo -e "${RED}❌ Error: Failed to download db.sql from ${SQL_URL}${NC}"
exit 1
fi
if curl -L -o "$MV_FILE" "$MV_URL"; then
echo "✅ mv.sql downloaded successfully"
else
echo -e "${RED}❌ Error: Failed to download mv.sql from ${MV_URL}${NC}"
exit 1 exit 1
fi fi
elif command -v wget &> /dev/null; then elif command -v wget &> /dev/null; then
if wget -O "$SQL_FILE" "$SQL_URL"; then if wget -O "$SQL_FILE" "$SQL_URL"; then
echo "✅ SQL file downloaded successfully" echo "✅ db.sql downloaded successfully"
else else
echo -e "${RED}❌ Error: Failed to download SQL file from ${SQL_URL}${NC}" echo -e "${RED}❌ Error: Failed to download db.sql from ${SQL_URL}${NC}"
exit 1
fi
if wget -O "$MV_FILE" "$MV_URL"; then
echo "✅ mv.sql downloaded successfully"
else
echo -e "${RED}❌ Error: Failed to download mv.sql from ${MV_URL}${NC}"
exit 1 exit 1
fi fi
else else
@ -55,16 +69,21 @@ else
exit 1 exit 1
fi fi
# Check if SQL file exists (after download) # Check if SQL files exist (after download)
if [ ! -f "$SQL_FILE" ]; then if [ ! -f "$SQL_FILE" ]; then
echo -e "${RED}❌ Error: SQL file '${SQL_FILE}' not found after download${NC}" echo -e "${RED}❌ Error: SQL file '${SQL_FILE}' not found after download${NC}"
exit 1 exit 1
fi fi
if [ ! -f "$MV_FILE" ]; then
echo -e "${RED}❌ Error: SQL file '${MV_FILE}' not found after download${NC}"
exit 1
fi
echo -e "${YELLOW}📋 Pre-deployment checks:${NC}" echo -e "${YELLOW}📋 Pre-deployment checks:${NC}"
echo "✅ Docker is running" echo "✅ Docker is running"
echo "✅ Database container is running" echo "✅ Database container is running"
echo "✅ SQL file downloaded" echo "✅ db.sql downloaded"
echo "✅ mv.sql downloaded"
echo "" echo ""
# Wait for database to be ready # Wait for database to be ready
@ -82,29 +101,48 @@ done
echo "✅ Database is ready" echo "✅ Database is ready"
echo "" echo ""
# Deploy the SQL file
echo -e "${YELLOW}🔧 Deploying database changes...${NC}" # Deploy the db.sql file
echo -e "${YELLOW}🔧 Deploying database changes (db.sql)...${NC}"
echo "Executing: $SQL_FILE" echo "Executing: $SQL_FILE"
if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $SQL_FILE; then if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $SQL_FILE; then
echo -e "${GREEN}✅ Database deployment completed successfully!${NC}" echo -e "${GREEN}✅ db.sql deployment completed successfully!${NC}"
echo ""
# Show table information
echo -e "${YELLOW}📊 Current database tables:${NC}"
docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dt"
else else
echo -e "${RED}❌ Error: Database deployment failed${NC}" echo -e "${RED}❌ Error: db.sql deployment failed${NC}"
echo -e "${YELLOW}💡 You can restore from backup using:${NC}" echo -e "${YELLOW}💡 You can restore from backup using:${NC}"
echo "docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $BACKUP_FILE" echo "docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $BACKUP_FILE"
# Clean up before exit
rm -f "$SQL_FILE" "$MV_FILE"
exit 1 exit 1
fi fi
# Clean up downloaded SQL file # Deploy the mv.sql file
echo -e "${YELLOW}<EFBFBD> Deploying database changes (mv.sql)...${NC}"
echo "Executing: $MV_FILE"
if docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $MV_FILE; then
echo -e "${GREEN}✅ mv.sql deployment completed successfully!${NC}"
else
echo -e "${RED}❌ Error: mv.sql deployment failed${NC}"
echo -e "${YELLOW}💡 You can restore from backup using:${NC}"
echo "docker exec -i $DB_CONTAINER psql -U $DB_USER -d $DB_NAME < $BACKUP_FILE"
# Clean up before exit
rm -f "$SQL_FILE" "$MV_FILE"
exit 1
fi
# Show table and materialized view information
echo -e "${YELLOW}📊 Current database tables:${NC}"
docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dt"
echo -e "${YELLOW}📄 Current materialized views:${NC}"
docker exec $DB_CONTAINER psql -U $DB_USER -d $DB_NAME -c "\dm"
# Clean up downloaded SQL files
echo -e "${YELLOW}🧹 Cleaning up...${NC}" echo -e "${YELLOW}🧹 Cleaning up...${NC}"
rm -f "$SQL_FILE" rm -f "$SQL_FILE" "$MV_FILE"
echo "✅ Temporary SQL file removed" echo "✅ Temporary SQL files removed"
echo "" echo ""
echo -e "${GREEN}🎉 Deployment completed successfully!${NC}" echo -e "${GREEN}🎉 Deployment completed successfully!${NC}"