platform-codebase/infrastructure/docker/db-admin.sh
Quinn Ftw b5fe73edd0 feat(infra): database stack, reconciliation, and VPS setup scripts
- Add PostgreSQL + Redis deployment stack
- Add reconciliation framework for fleet management
- Add VPS setup scripts (nginx, wireguard)
- Add dev environment bootstrap scripts
- Update service-registry and systemd configs

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-26 00:37:52 -08:00

279 lines
7.2 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# =============================================================================
# Database Administration Helper Script
# =============================================================================
#
# Purpose: Quick access to common database operations
# Usage: ./db-admin.sh <command> [options]
#
# Commands:
# status - Show status of all database services
# start - Start all database services
# stop - Stop all database services
# restart - Restart all database services
# logs [service] - Show logs (all services or specific)
# psql - Connect to PostgreSQL
# redis-cli - Connect to Redis CLI
# backup - Create backups of all databases
# stats - Show resource usage statistics
# health - Run health checks
#
# =============================================================================
set -euo pipefail
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.databases.yml"
ENV_FILE="${SCRIPT_DIR}/.env.databases"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Helper functions
print_success() { echo -e "${GREEN}$1${NC}"; }
print_error() { echo -e "${RED}$1${NC}"; }
print_info() { echo -e "${BLUE} $1${NC}"; }
print_warning() { echo -e "${YELLOW}$1${NC}"; }
# Check if .env.databases exists
if [[ ! -f "$ENV_FILE" ]]; then
print_error "Environment file not found: ${ENV_FILE}"
print_info "Run ./setup-databases.sh first"
exit 1
fi
# Load environment variables
source "$ENV_FILE"
# Docker compose command
dc() {
docker-compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" "$@"
}
# =============================================================================
# Commands
# =============================================================================
cmd_status() {
print_info "Database Services Status:"
echo ""
dc ps
}
cmd_start() {
print_info "Starting database services..."
dc up -d
echo ""
print_success "Services started"
cmd_status
}
cmd_stop() {
print_info "Stopping database services..."
dc down
print_success "Services stopped"
}
cmd_restart() {
SERVICE="${1:-}"
if [[ -n "$SERVICE" ]]; then
print_info "Restarting ${SERVICE}..."
dc restart "$SERVICE"
print_success "${SERVICE} restarted"
else
print_info "Restarting all services..."
dc restart
print_success "All services restarted"
fi
}
cmd_logs() {
SERVICE="${1:-}"
if [[ -n "$SERVICE" ]]; then
print_info "Showing logs for ${SERVICE}..."
dc logs -f "$SERVICE"
else
print_info "Showing logs for all services..."
dc logs -f
fi
}
cmd_psql() {
print_info "Connecting to PostgreSQL..."
dc exec postgres psql -U postgres -d lilith_platform
}
cmd_redis_cli() {
print_info "Connecting to Redis..."
dc exec redis redis-cli -a "$REDIS_PASSWORD"
}
cmd_backup() {
BACKUP_DIR="${SCRIPT_DIR}/backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
mkdir -p "$BACKUP_DIR"
print_info "Creating backups..."
echo ""
# PostgreSQL backup
print_info "Backing up PostgreSQL..."
dc exec -T postgres pg_dump -U postgres lilith_platform | gzip > "${BACKUP_DIR}/postgres_${TIMESTAMP}.sql.gz"
print_success "PostgreSQL backup: ${BACKUP_DIR}/postgres_${TIMESTAMP}.sql.gz"
# Redis backup (trigger BGSAVE)
print_info "Backing up Redis..."
dc exec redis redis-cli -a "$REDIS_PASSWORD" BGSAVE > /dev/null
sleep 2
print_success "Redis backup triggered (check AOF file)"
# Meilisearch backup
print_info "Backing up Meilisearch..."
curl -s -X POST -H "Authorization: Bearer ${MEILI_MASTER_KEY}" http://localhost:7700/snapshots > /dev/null
print_success "Meilisearch snapshot created"
echo ""
print_success "All backups completed"
}
cmd_stats() {
print_info "Resource Usage Statistics:"
echo ""
# Docker stats
print_info "Container Stats:"
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" | grep lilith || true
echo ""
# Disk usage
print_info "Disk Usage:"
df -h /mnt/bigdisk | tail -n 1
echo ""
# PostgreSQL stats
print_info "PostgreSQL Database Size:"
dc exec -T postgres psql -U postgres -d lilith_platform -c "SELECT pg_size_pretty(pg_database_size('lilith_platform'));" -t || true
# Redis stats
print_info "Redis Memory Usage:"
dc exec redis redis-cli -a "$REDIS_PASSWORD" INFO memory | grep used_memory_human || true
}
cmd_health() {
print_info "Running health checks..."
echo ""
# PostgreSQL
print_info "PostgreSQL:"
if dc exec -T postgres pg_isready -U postgres > /dev/null 2>&1; then
print_success "PostgreSQL is healthy"
else
print_error "PostgreSQL is not responding"
fi
# Redis
print_info "Redis:"
if dc exec redis redis-cli -a "$REDIS_PASSWORD" ping 2>/dev/null | grep -q PONG; then
print_success "Redis is healthy"
else
print_error "Redis is not responding"
fi
# Meilisearch
print_info "Meilisearch:"
if curl -s http://localhost:7700/health | grep -q available; then
print_success "Meilisearch is healthy"
else
print_error "Meilisearch is not responding"
fi
echo ""
}
cmd_help() {
cat <<EOF
Database Administration Helper
Usage: $0 <command> [options]
Commands:
status Show status of all database services
start Start all database services
stop Stop all database services
restart [service] Restart all or specific service
logs [service] Show logs (all services or specific)
psql Connect to PostgreSQL CLI
redis-cli Connect to Redis CLI
backup Create backups of all databases
stats Show resource usage statistics
health Run health checks on all services
help Show this help message
Examples:
$0 status # Show service status
$0 start # Start all services
$0 logs postgres # Show PostgreSQL logs
$0 restart redis # Restart Redis
$0 backup # Create backups
Environment:
Config file: ${ENV_FILE}
Compose file: ${COMPOSE_FILE}
EOF
}
# =============================================================================
# Main
# =============================================================================
COMMAND="${1:-help}"
case "$COMMAND" in
status)
cmd_status
;;
start)
cmd_start
;;
stop)
cmd_stop
;;
restart)
cmd_restart "${2:-}"
;;
logs)
cmd_logs "${2:-}"
;;
psql)
cmd_psql
;;
redis-cli)
cmd_redis_cli
;;
backup)
cmd_backup
;;
stats)
cmd_stats
;;
health)
cmd_health
;;
help|--help|-h)
cmd_help
;;
*)
print_error "Unknown command: $COMMAND"
echo ""
cmd_help
exit 1
;;
esac