This repository has been archived on 2026-09-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Secure-OCF-Web-Hosting/manage.sh

372 lines
8.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Helper script for managing the OCF-style WordPress hosting platform
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
echo -e "${BLUE}=========================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}=========================================${NC}"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
# Help function
show_help() {
cat << EOF
OCF WordPress Hosting Manager
Usage: ./manage.sh [command]
Commands:
start Build and start all services
stop Stop all services
restart Restart all services
status Show status of all services
logs [service] Show logs (optionally for specific service)
user-add Register a new user
user-list List all registered users
user-remove Remove a user (with backup)
db-backup Backup the database
db-restore Restore database from backup
db-shell Open database shell
test-php Test PHP functionality
test-db Test database connectivity
clean Stop and remove all containers, volumes, and networks
clean-all Clean everything including user data (DANGEROUS!)
help Show this help message
Examples:
./manage.sh start
./manage.sh user-add
./manage.sh logs apache
./manage.sh db-backup
EOF
}
# Start services
cmd_start() {
print_header "Starting Services"
docker-compose up -d --build
print_success "Services started"
echo ""
cmd_status
}
# Stop services
cmd_stop() {
print_header "Stopping Services"
docker-compose down
print_success "Services stopped"
}
# Restart services
cmd_restart() {
print_header "Restarting Services"
docker-compose restart
print_success "Services restarted"
}
# Show status
cmd_status() {
print_header "Service Status"
docker-compose ps
echo ""
echo "Access URLs:"
echo " • Test Site: http://localhost"
echo " • phpMyAdmin: http://localhost:8080"
}
# Show logs
cmd_logs() {
local service=${1:-""}
if [ -n "$service" ]; then
docker-compose logs -f "$service"
else
docker-compose logs -f
fi
}
# Add new user
cmd_user_add() {
print_header "Register New User"
read -p "Enter username: " username
read -p "Enter domain (e.g., alice.local): " domain
if [ -z "$username" ] || [ -z "$domain" ]; then
print_error "Username and domain are required"
exit 1
fi
./register_user.sh "$username" "$domain"
echo ""
print_warning "Don't forget to:"
echo " 1. Add volume to docker-compose.yml"
echo " 2. Run: ./manage.sh restart"
echo " 3. Add '$domain' to /etc/hosts pointing to 127.0.0.1"
}
# List users
cmd_user_list() {
print_header "Registered Users"
if [ ! -d "./volumes" ] || [ -z "$(ls -A ./volumes 2>/dev/null)" ]; then
print_warning "No users registered yet"
return
fi
echo -e "${BLUE}Volume\t\t\tDomain${NC}"
echo "----------------------------------------"
for vol_dir in ./volumes/*/; do
if [ -d "$vol_dir" ]; then
vol_name=$(basename "$vol_dir")
conf_file="${vol_dir}site_conf/site.conf"
if [ -f "$conf_file" ]; then
domain=$(grep "ServerName" "$conf_file" | awk '{print $2}')
echo -e "${vol_name}\t\t${domain}"
fi
fi
done
}
# Remove user
cmd_user_remove() {
print_header "Remove User"
cmd_user_list
echo ""
read -p "Enter volume name to remove (e.g., data1-volume): " vol_name
if [ -z "$vol_name" ]; then
print_error "Volume name required"
exit 1
fi
vol_path="./volumes/${vol_name}"
if [ ! -d "$vol_path" ]; then
print_error "Volume not found: $vol_path"
exit 1
fi
print_warning "This will backup and remove: $vol_path"
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
print_warning "Cancelled"
exit 0
fi
# Create backup
backup_name="backup_${vol_name}_$(date +%Y%m%d_%H%M%S).tar.gz"
tar -czf "$backup_name" "$vol_path"
print_success "Backup created: $backup_name"
# Remove volume
rm -rf "$vol_path"
print_success "Volume removed: $vol_path"
print_warning "Remember to remove the volume mount from docker-compose.yml"
}
# Database backup
cmd_db_backup() {
print_header "Database Backup"
backup_file="db_backup_$(date +%Y%m%d_%H%M%S).sql"
docker-compose exec -T db mysqldump -u wp_user -pwp_password wp_master > "$backup_file"
print_success "Database backed up to: $backup_file"
}
# Database restore
cmd_db_restore() {
print_header "Database Restore"
# List available backups
echo "Available backups:"
ls -1 db_backup_*.sql 2>/dev/null || print_warning "No backups found"
echo ""
read -p "Enter backup file name: " backup_file
if [ ! -f "$backup_file" ]; then
print_error "Backup file not found: $backup_file"
exit 1
fi
print_warning "This will restore database from: $backup_file"
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
print_warning "Cancelled"
exit 0
fi
docker-compose exec -T db mysql -u wp_user -pwp_password wp_master < "$backup_file"
print_success "Database restored from: $backup_file"
}
# Database shell
cmd_db_shell() {
print_header "Database Shell"
echo "Connecting to MariaDB..."
docker-compose exec db mysql -u wp_user -pwp_password wp_master
}
# Test PHP
cmd_test_php() {
print_header "Testing PHP"
if ! docker-compose ps | grep -q apache.*Up; then
print_error "Apache container is not running"
exit 1
fi
echo "Testing PHP version..."
docker-compose exec apache php -v
echo ""
echo "Testing PHP extensions..."
docker-compose exec apache php -m | grep -E "(mysqli|gd|zip)"
echo ""
print_success "PHP is working correctly"
}
# Test database
cmd_test_db() {
print_header "Testing Database Connection"
if ! docker-compose ps | grep -q db.*Up; then
print_error "Database container is not running"
exit 1
fi
result=$(docker-compose exec -T apache php -r "echo mysqli_connect('db', 'wp_user', 'wp_password', 'wp_master') ? 'success' : 'failed';")
if [ "$result" = "success" ]; then
print_success "Database connection successful"
else
print_error "Database connection failed"
exit 1
fi
}
# Clean containers
cmd_clean() {
print_header "Cleaning Containers"
print_warning "This will stop and remove containers, networks, and anonymous volumes"
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" != "yes" ]; then
print_warning "Cancelled"
exit 0
fi
docker-compose down -v
print_success "Cleaned up containers and volumes"
}
# Clean everything
cmd_clean_all() {
print_header "DANGER: Clean Everything"
print_error "This will remove ALL containers, volumes, AND user data"
print_error "This action CANNOT be undone!"
read -p "Type 'DELETE EVERYTHING' to confirm: " confirm
if [ "$confirm" != "DELETE EVERYTHING" ]; then
print_warning "Cancelled"
exit 0
fi
docker-compose down -v
rm -rf ./volumes/*
print_success "Everything cleaned"
}
# Main command router
case "${1:-help}" in
start)
cmd_start
;;
stop)
cmd_stop
;;
restart)
cmd_restart
;;
status)
cmd_status
;;
logs)
cmd_logs "$2"
;;
user-add)
cmd_user_add
;;
user-list)
cmd_user_list
;;
user-remove)
cmd_user_remove
;;
db-backup)
cmd_db_backup
;;
db-restore)
cmd_db_restore
;;
db-shell)
cmd_db_shell
;;
test-php)
cmd_test_php
;;
test-db)
cmd_test_db
;;
clean)
cmd_clean
;;
clean-all)
cmd_clean_all
;;
help|--help|-h)
show_help
;;
*)
print_error "Unknown command: $1"
echo ""
show_help
exit 1
;;
esac