#!/bin/bash # Homelab Docker Management Script set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Function to show help show_help() { echo "Homelab Docker Management Script" echo "" echo "Usage: $0 [COMMAND] [SERVICE]" echo "" echo "Commands:" echo " start [service] Start all services or specific service" echo " stop [service] Stop all services or specific service" echo " restart [service] Restart all services or specific service" echo " status Show status of all services" echo " logs [service] Show logs for all services or specific service" echo " update Pull latest images and restart services" echo " clean Remove unused containers, networks, and images" echo " backup Backup important data volumes" echo " help Show this help message" echo "" echo "Services:" echo " firefly, frigate, gitea, immich, mongodb, n8n, portainer, redis, wud, webmap" echo "" echo "Examples:" echo " $0 start # Start all services" echo " $0 start frigate # Start only Frigate" echo " $0 logs immich # Show Immich logs" echo " $0 restart firefly # Restart Firefly III" } # Function to start services start_services() { local service=$1 if [ -z "$service" ]; then print_status "Starting all homelab services..." docker-compose up -d print_success "All services started!" else print_status "Starting $service..." docker-compose up -d $service print_success "$service started!" fi } # Function to stop services stop_services() { local service=$1 if [ -z "$service" ]; then print_status "Stopping all homelab services..." docker-compose down print_success "All services stopped!" else print_status "Stopping $service..." docker-compose stop $service print_success "$service stopped!" fi } # Function to restart services restart_services() { local service=$1 if [ -z "$service" ]; then print_status "Restarting all homelab services..." docker-compose restart print_success "All services restarted!" else print_status "Restarting $service..." docker-compose restart $service print_success "$service restarted!" fi } # Function to show status show_status() { print_status "Homelab services status:" echo "" docker-compose ps echo "" print_status "Resource usage:" docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" } # Function to show logs show_logs() { local service=$1 if [ -z "$service" ]; then print_status "Showing logs for all services (last 50 lines each)..." docker-compose logs --tail=50 else print_status "Showing logs for $service..." docker-compose logs -f $service fi } # Function to update services update_services() { print_status "Updating homelab services..." docker-compose pull docker-compose up -d print_success "Services updated and restarted!" } # Function to clean up clean_up() { print_warning "This will remove unused containers, networks, and images." read -p "Are you sure? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then print_status "Cleaning up Docker resources..." docker system prune -f docker volume prune -f print_success "Cleanup completed!" else print_status "Cleanup cancelled." fi } # Function to backup data backup_data() { local backup_dir="./backups/$(date +%Y%m%d_%H%M%S)" print_status "Creating backup in $backup_dir..." mkdir -p "$backup_dir" # Backup important volumes docker run --rm -v firefly_iii_db:/data -v "$(pwd)/$backup_dir":/backup alpine tar czf /backup/firefly_db.tar.gz -C /data . docker run --rm -v mongodb-data:/data -v "$(pwd)/$backup_dir":/backup alpine tar czf /backup/mongodb.tar.gz -C /data . docker run --rm -v redis-data:/data -v "$(pwd)/$backup_dir":/backup alpine tar czf /backup/redis.tar.gz -C /data . print_success "Backup completed in $backup_dir" } # Main script logic case "$1" in start) start_services "$2" ;; stop) stop_services "$2" ;; restart) restart_services "$2" ;; status) show_status ;; logs) show_logs "$2" ;; update) update_services ;; clean) clean_up ;; backup) backup_data ;; help|--help|-h) show_help ;; *) print_error "Unknown command: $1" echo "" show_help exit 1 ;; esac