mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 01:10:12 -08:00
- Created a centralized database repair script (`plex-database-repair.sh`) to handle all database integrity checks and repairs for Plex Media Server. - Updated the main Plex management script (`plex.sh`) to integrate the new repair functionality and fixed Unicode/ASCII display issues. - Refactored the backup script (`backup-plex.sh`) to remove duplicate repair functions and ensure it utilizes the new repair script. - Conducted thorough code validation and functional testing to ensure all scripts operate correctly with the new changes. - Enhanced documentation for the new repair script, detailing usage, features, and integration points with other scripts. - Fixed critical bugs related to WAL file handling and corrected typos in script options.
509 lines
18 KiB
Bash
Executable File
509 lines
18 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
################################################################################
|
||
# Plex Media Server Management Script
|
||
################################################################################
|
||
#
|
||
# Author: Peter Wood <peter@peterwood.dev>
|
||
# Description: Modern, user-friendly Plex Media Server management script with
|
||
# styled output and comprehensive service control capabilities.
|
||
# Provides an interactive interface for common Plex operations.
|
||
#
|
||
# Features:
|
||
# - Service start/stop/restart/status operations
|
||
# - Web interface launcher
|
||
# - Styled console output with Unicode symbols
|
||
# - Service health monitoring
|
||
# - Process management and monitoring
|
||
# - Interactive menu system
|
||
#
|
||
# Related Scripts:
|
||
# - backup-plex.sh: Comprehensive backup solution
|
||
# - restore-plex.sh: Backup restoration utilities
|
||
# - monitor-plex-backup.sh: Backup system monitoring
|
||
# - validate-plex-backups.sh: Backup validation tools
|
||
# - test-plex-backup.sh: Testing framework
|
||
#
|
||
# Usage:
|
||
# ./plex.sh start # Start Plex service
|
||
# ./plex.sh stop # Stop Plex service
|
||
# ./plex.sh restart # Restart Plex service
|
||
# ./plex.sh status # Show service status
|
||
# ./plex.sh web # Open web interface
|
||
# ./plex.sh # Interactive menu
|
||
#
|
||
# Dependencies:
|
||
# - systemctl (systemd service management)
|
||
# - Plex Media Server package
|
||
# - Web browser (for web interface launching)
|
||
#
|
||
# Exit Codes:
|
||
# 0 - Success
|
||
# 1 - General error
|
||
# 2 - Service operation failure
|
||
# 3 - Invalid command or option
|
||
#
|
||
################################################################################
|
||
|
||
# 🎬 Plex Media Server Management Script
|
||
# A sexy, modern script for managing Plex Media Server with style
|
||
# Author: acedanger
|
||
# Version: 2.0
|
||
|
||
set -euo pipefail
|
||
|
||
# 🎨 Color definitions for sexy output
|
||
readonly RED='\033[0;31m'
|
||
readonly GREEN='\033[0;32m'
|
||
readonly YELLOW='\033[1;33m'
|
||
readonly BLUE='\033[0;34m'
|
||
readonly PURPLE='\033[0;35m'
|
||
readonly CYAN='\033[0;36m'
|
||
readonly WHITE='\033[1;37m'
|
||
readonly BOLD='\033[1m'
|
||
readonly DIM='\033[2m'
|
||
readonly RESET='\033[0m'
|
||
|
||
# 🔧 Configuration
|
||
readonly PLEX_SERVICE="plexmediaserver"
|
||
SCRIPT_NAME="$(basename "$0")"
|
||
readonly SCRIPT_NAME
|
||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||
readonly SCRIPT_DIR
|
||
|
||
# 🎭 ASCII symbols for compatible output
|
||
readonly CHECKMARK="[✓]"
|
||
readonly CROSS="[✗]"
|
||
readonly ROCKET="[>]"
|
||
readonly STOP_SIGN="[■]"
|
||
readonly RECYCLE="[~]"
|
||
readonly INFO="[i]"
|
||
readonly HOURGLASS="[*]"
|
||
readonly SPARKLES="[*]"
|
||
|
||
# 📊 Function to print fancy headers
|
||
print_header() {
|
||
echo -e "\n${PURPLE}${BOLD}+==============================================================+${RESET}"
|
||
echo -e "${PURPLE}${BOLD}| [*] PLEX MEDIA SERVER [*] |${RESET}"
|
||
echo -e "${PURPLE}${BOLD}+==============================================================+${RESET}\n"
|
||
}
|
||
|
||
# 🎉 Function to print completion footer
|
||
print_footer() {
|
||
echo -e "\n${DIM}${CYAN}--- Operation completed [*] ---${RESET}\n"
|
||
}
|
||
|
||
# 🎯 Function to print status with style
|
||
print_status() {
|
||
local status="$1"
|
||
local message="$2"
|
||
local color="$3"
|
||
echo -e "${color}${BOLD}[${status}]${RESET} ${message}"
|
||
}
|
||
|
||
# ⏱️ Function to show loading animation
|
||
show_loading() {
|
||
local message="$1"
|
||
local pid="$2"
|
||
local spin='-\|/'
|
||
local i=0
|
||
|
||
echo -ne "${CYAN}${HOURGLASS} ${message}${RESET}"
|
||
while kill -0 "$pid" 2>/dev/null; do
|
||
i=$(( (i+1) %4 ))
|
||
printf "\r%s%s %s %s%s" "${CYAN}" "${HOURGLASS}" "${message}" "${spin:$i:1}" "${RESET}"
|
||
sleep 0.1
|
||
done
|
||
printf "\r%s%s %s %s%s\n" "${CYAN}" "${HOURGLASS}" "${message}" "${CHECKMARK}" "${RESET}"
|
||
}
|
||
|
||
# 🔧 Function to repair database issues
|
||
repair_database() {
|
||
print_status "${INFO}" "Attempting to repair Plex database..." "${BLUE}"
|
||
|
||
local db_dir="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
|
||
local main_db="$db_dir/com.plexapp.plugins.library.db"
|
||
local backup_db="$db_dir/com.plexapp.plugins.library.db.backup.$(date +%Y%m%d_%H%M%S)"
|
||
|
||
if [[ ! -f "$main_db" ]]; then
|
||
print_status "${CROSS}" "Main database not found at: $main_db" "${RED}"
|
||
return 1
|
||
fi
|
||
|
||
# Stop Plex service first
|
||
print_status "${INFO}" "Stopping Plex service..." "${BLUE}"
|
||
sudo systemctl stop "$PLEX_SERVICE" 2>/dev/null || true
|
||
sleep 2
|
||
|
||
# Create backup of current database
|
||
print_status "${INFO}" "Creating backup of current database..." "${BLUE}"
|
||
if ! sudo cp "$main_db" "$backup_db"; then
|
||
print_status "${CROSS}" "Failed to create database backup!" "${RED}"
|
||
return 1
|
||
fi
|
||
|
||
print_status "${CHECKMARK}" "Database backed up to: $backup_db" "${GREEN}"
|
||
|
||
# Try to vacuum the database
|
||
print_status "${INFO}" "Running VACUUM on database..." "${BLUE}"
|
||
if sudo -u plex sqlite3 "$main_db" "VACUUM;"; then
|
||
print_status "${CHECKMARK}" "Database VACUUM completed successfully" "${GREEN}"
|
||
|
||
# Test integrity again
|
||
if sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" | grep -q "ok"; then
|
||
print_status "${CHECKMARK}" "Database integrity restored!" "${GREEN}"
|
||
print_status "${INFO}" "You can now try starting Plex again" "${BLUE}"
|
||
return 0
|
||
else
|
||
print_status "${CROSS}" "Database still corrupted after VACUUM" "${RED}"
|
||
fi
|
||
else
|
||
print_status "${CROSS}" "VACUUM operation failed" "${RED}"
|
||
fi
|
||
|
||
# If VACUUM failed, suggest restore options
|
||
print_status "${INFO}" "VACUUM repair failed. Consider these options:" "${YELLOW}"
|
||
echo -e "${DIM}${YELLOW} 1. Restore from a backup using restore-plex.sh${RESET}"
|
||
echo -e "${DIM}${YELLOW} 2. Delete corrupted database (Plex will rebuild, but you'll lose metadata)${RESET}"
|
||
echo -e "${DIM}${YELLOW} 3. Check for corrupted database backups in: $db_dir/corrupted-*/${RESET}"
|
||
|
||
return 1
|
||
}
|
||
|
||
# 🔍 Function to check database integrity
|
||
check_database_integrity() {
|
||
print_status "${INFO}" "Checking database integrity..." "${BLUE}"
|
||
|
||
local db_dir="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
|
||
local main_db="$db_dir/com.plexapp.plugins.library.db"
|
||
local repair_script="${SCRIPT_DIR}/plex-database-repair.sh"
|
||
|
||
if [[ ! -f "$main_db" ]]; then
|
||
print_status "${CROSS}" "Main database not found at: $main_db" "${RED}"
|
||
return 1
|
||
fi
|
||
|
||
# Use shared repair script for integrity checking if available
|
||
if [[ -f "$repair_script" ]]; then
|
||
if "$repair_script" check "$main_db" >/dev/null 2>&1; then
|
||
print_status "${CHECKMARK}" "Database integrity check passed" "${GREEN}"
|
||
return 0
|
||
else
|
||
print_status "${CROSS}" "Database integrity check failed!" "${RED}"
|
||
print_status "${INFO}" "Consider running database repair: plex repair" "${YELLOW}"
|
||
return 1
|
||
fi
|
||
else
|
||
# Fallback to basic sqlite3 check
|
||
if ! sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" >/dev/null 2>&1; then
|
||
print_status "${CROSS}" "Database integrity check failed!" "${RED}"
|
||
print_status "${INFO}" "Consider running database repair or restore from backup" "${YELLOW}"
|
||
return 1
|
||
fi
|
||
|
||
print_status "${CHECKMARK}" "Database integrity check passed" "${GREEN}"
|
||
return 0
|
||
fi
|
||
}
|
||
|
||
# <20>🚀 Enhanced start function
|
||
start_plex() {
|
||
print_status "${ROCKET}" "Starting Plex Media Server..." "${GREEN}"
|
||
|
||
if systemctl is-active --quiet "$PLEX_SERVICE"; then
|
||
print_status "${INFO}" "Plex is already running!" "${YELLOW}"
|
||
show_detailed_status
|
||
return 0
|
||
fi
|
||
|
||
# Reset any failed state first
|
||
sudo systemctl reset-failed "$PLEX_SERVICE" 2>/dev/null || true
|
||
|
||
# Check database integrity before starting
|
||
if ! check_database_integrity; then
|
||
print_status "${CROSS}" "Database integrity issues detected. Service may fail to start." "${RED}"
|
||
echo -e "${DIM}${YELLOW} Try: sudo systemctl stop plexmediaserver && sudo -u plex sqlite3 /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases/com.plexapp.plugins.library.db 'VACUUM;'${RESET}"
|
||
return 1
|
||
fi
|
||
|
||
print_status "${INFO}" "Attempting to start service..." "${BLUE}"
|
||
|
||
if ! sudo systemctl start "$PLEX_SERVICE"; then
|
||
print_status "${CROSS}" "Failed to start Plex Media Server!" "${RED}"
|
||
print_status "${INFO}" "Checking service logs..." "${BLUE}"
|
||
|
||
# Show recent error logs
|
||
echo -e "\n${DIM}${RED}Recent error logs:${RESET}"
|
||
sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "1 minute ago" | tail -5
|
||
|
||
return 1
|
||
fi
|
||
|
||
# Wait and verify startup
|
||
sleep 3
|
||
local timeout=30
|
||
local elapsed=0
|
||
|
||
print_status "${HOURGLASS}" "Waiting for service to initialize..." "${CYAN}"
|
||
|
||
while [[ $elapsed -lt $timeout ]]; do
|
||
if systemctl is-active --quiet "$PLEX_SERVICE"; then
|
||
print_status "${CHECKMARK}" "Plex Media Server started successfully!" "${GREEN}"
|
||
print_footer
|
||
return 0
|
||
fi
|
||
|
||
sleep 2
|
||
elapsed=$((elapsed + 2))
|
||
echo -ne "${DIM}${CYAN} Waiting... ${elapsed}s/${timeout}s${RESET}\r"
|
||
done
|
||
|
||
echo ""
|
||
print_status "${CROSS}" "Service startup timeout or failed!" "${RED}"
|
||
|
||
# Show current status
|
||
local status
|
||
status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "unknown")
|
||
print_status "${INFO}" "Current status: $status" "${YELLOW}"
|
||
|
||
if [[ "$status" == "failed" ]]; then
|
||
echo -e "\n${DIM}${RED}Recent error logs:${RESET}"
|
||
sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 10 --since "2 minutes ago"
|
||
fi
|
||
|
||
return 1
|
||
}
|
||
|
||
# 🛑 Enhanced stop function
|
||
stop_plex() {
|
||
print_status "${STOP_SIGN}" "Stopping Plex Media Server..." "${YELLOW}"
|
||
|
||
if ! systemctl is-active --quiet "$PLEX_SERVICE"; then
|
||
print_status "${INFO}" "Plex is already stopped!" "${YELLOW}"
|
||
return 0
|
||
fi
|
||
|
||
sudo systemctl stop "$PLEX_SERVICE" &
|
||
local pid=$!
|
||
show_loading "Gracefully shutting down Plex" $pid
|
||
wait $pid
|
||
|
||
if ! systemctl is-active --quiet "$PLEX_SERVICE"; then
|
||
print_status "${CHECKMARK}" "Plex Media Server stopped successfully!" "${GREEN}"
|
||
print_footer
|
||
else
|
||
print_status "${CROSS}" "Failed to stop Plex Media Server!" "${RED}"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
# ♻️ Enhanced restart function
|
||
restart_plex() {
|
||
print_status "${RECYCLE}" "Restarting Plex Media Server..." "${BLUE}"
|
||
|
||
if systemctl is-active --quiet "$PLEX_SERVICE"; then
|
||
stop_plex
|
||
echo ""
|
||
fi
|
||
|
||
start_plex
|
||
}
|
||
|
||
# 📊 Enhanced status function with detailed info
|
||
show_detailed_status() {
|
||
local service_status
|
||
service_status=$(systemctl is-active "$PLEX_SERVICE" 2>/dev/null || echo "inactive")
|
||
|
||
echo -e "\n${BOLD}${BLUE}+==============================================================+${RESET}"
|
||
echo -e "${BOLD}${BLUE}| SERVICE STATUS |${RESET}"
|
||
echo -e "${BOLD}${BLUE}+==============================================================+${RESET}"
|
||
|
||
case "$service_status" in
|
||
"active")
|
||
print_status "${CHECKMARK}" "Service Status: ${GREEN}${BOLD}ACTIVE${RESET}" "${GREEN}"
|
||
|
||
# Get additional info
|
||
local uptime
|
||
uptime=$(systemctl show "$PLEX_SERVICE" --property=ActiveEnterTimestamp --value | xargs -I {} date -d {} "+%Y-%m-%d %H:%M:%S" 2>/dev/null || echo "Unknown")
|
||
|
||
local memory_usage
|
||
memory_usage=$(systemctl show "$PLEX_SERVICE" --property=MemoryCurrent --value 2>/dev/null || echo "0")
|
||
if [[ "$memory_usage" != "0" ]] && [[ "$memory_usage" =~ ^[0-9]+$ ]]; then
|
||
memory_usage="$(( memory_usage / 1024 / 1024 )) MB"
|
||
else
|
||
memory_usage="Unknown"
|
||
fi
|
||
|
||
echo -e "${DIM}${CYAN} Started: ${WHITE}${uptime}${RESET}"
|
||
echo -e "${DIM}${CYAN} Memory Usage: ${WHITE}${memory_usage}${RESET}"
|
||
echo -e "${DIM}${CYAN} Service Name: ${WHITE}${PLEX_SERVICE}${RESET}"
|
||
;;
|
||
"inactive")
|
||
print_status "${CROSS}" "Service Status: ${RED}${BOLD}INACTIVE${RESET}" "${RED}"
|
||
echo -e "${DIM}${YELLOW} Use '${SCRIPT_NAME} start' to start the service${RESET}"
|
||
;;
|
||
"failed")
|
||
print_status "${CROSS}" "Service Status: ${RED}${BOLD}FAILED${RESET}" "${RED}"
|
||
echo -e "${DIM}${RED} Check logs with: ${WHITE}journalctl -u ${PLEX_SERVICE}${RESET}"
|
||
;;
|
||
*)
|
||
print_status "${INFO}" "Service Status: ${YELLOW}${BOLD}${service_status^^}${RESET}" "${YELLOW}"
|
||
;;
|
||
esac
|
||
|
||
# Show recent logs
|
||
echo -e "\n${DIM}${CYAN}+--- Recent Service Logs (24h) ---+${RESET}"
|
||
|
||
# Try to get logs with sudo, fall back to user permissions
|
||
local logs
|
||
if logs=$(sudo journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" --output=short 2>/dev/null); then
|
||
if [[ -n "$logs" && "$logs" != "-- No entries --" ]]; then
|
||
echo -e "${DIM}${logs}${RESET}"
|
||
else
|
||
echo -e "${DIM}${YELLOW}No recent log entries found${RESET}"
|
||
fi
|
||
else
|
||
# Fallback: try without sudo
|
||
logs=$(journalctl -u "$PLEX_SERVICE" --no-pager -n 5 --since "24 hours ago" 2>/dev/null || echo "Unable to access logs")
|
||
if [[ "$logs" == "Unable to access logs" || "$logs" == "-- No entries --" ]]; then
|
||
echo -e "${DIM}${YELLOW}Unable to access recent logs (try: sudo journalctl -u ${PLEX_SERVICE})${RESET}"
|
||
else
|
||
echo -e "${DIM}${logs}${RESET}"
|
||
fi
|
||
fi
|
||
|
||
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
|
||
}
|
||
|
||
# 🔧 Show available commands
|
||
show_help() {
|
||
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
|
||
echo ""
|
||
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
|
||
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
|
||
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
|
||
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
|
||
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
|
||
echo -e " ${RED}${BOLD}repair${RESET} [!] Repair database corruption issues"
|
||
echo -e " ${PURPLE}${BOLD}help${RESET} [*] Show this help message"
|
||
echo ""
|
||
echo -e "${DIM}${WHITE}Examples:${RESET}"
|
||
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
|
||
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}"
|
||
echo -e " ${DIM}${SCRIPT_NAME} repair # Fix database issues${RESET}"
|
||
echo ""
|
||
}
|
||
|
||
# Database repair function using shared script
|
||
repair_plex() {
|
||
print_header "DATABASE REPAIR"
|
||
print_status "${INFO}" "Attempting to repair Plex database..." "${YELLOW}"
|
||
|
||
local db_dir="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
|
||
local main_db="$db_dir/com.plexapp.plugins.library.db"
|
||
local repair_script="${SCRIPT_DIR}/plex-database-repair.sh"
|
||
|
||
if [[ ! -f "$repair_script" ]]; then
|
||
print_status "${CROSS}" "Database repair script not found: $repair_script" "${RED}"
|
||
return 2
|
||
fi
|
||
|
||
if [[ ! -f "$main_db" ]]; then
|
||
print_status "${CROSS}" "Main database not found at: $main_db" "${RED}"
|
||
return 1
|
||
fi
|
||
|
||
# Stop Plex service first
|
||
print_status "${INFO}" "Stopping Plex service for repair..." "${BLUE}"
|
||
if ! sudo systemctl stop "$PLEX_SERVICE"; then
|
||
print_status "${CROSS}" "Failed to stop Plex service!" "${RED}"
|
||
return 1
|
||
fi
|
||
|
||
# Run the repair
|
||
print_status "${INFO}" "Running database repair (this may take a while)..." "${BLUE}"
|
||
|
||
if "$repair_script" repair "$main_db"; then
|
||
print_status "${CHECKMARK}" "Database repair completed successfully!" "${GREEN}"
|
||
|
||
# Try to start the service
|
||
print_status "${INFO}" "Starting Plex service..." "${BLUE}"
|
||
if sudo systemctl start "$PLEX_SERVICE"; then
|
||
print_status "${CHECKMARK}" "Plex service started successfully!" "${GREEN}"
|
||
print_footer
|
||
return 0
|
||
else
|
||
print_status "${CROSS}" "Failed to start Plex service after repair!" "${RED}"
|
||
return 1
|
||
fi
|
||
else
|
||
local repair_exit_code=$?
|
||
print_status "${CROSS}" "Database repair failed!" "${RED}"
|
||
|
||
# Try to start the service anyway
|
||
print_status "${INFO}" "Attempting to start Plex service anyway..." "${BLUE}"
|
||
sudo systemctl start "$PLEX_SERVICE" 2>/dev/null || true
|
||
|
||
if [[ $repair_exit_code -eq 2 ]]; then
|
||
print_status "${INFO}" "Critical error - manual intervention required" "${YELLOW}"
|
||
else
|
||
print_status "${INFO}" "Repair failed but service may still work with corrupted database" "${YELLOW}"
|
||
fi
|
||
|
||
print_footer
|
||
return $repair_exit_code
|
||
fi
|
||
}
|
||
|
||
# 🎯 Main script logic
|
||
main() {
|
||
# Check if running as root
|
||
if [[ $EUID -eq 0 ]]; then
|
||
print_header
|
||
print_status "${CROSS}" "Don't run this script as root! Use your regular user account." "${RED}"
|
||
exit 1
|
||
fi
|
||
|
||
# Check if no arguments provided
|
||
if [[ $# -eq 0 ]]; then
|
||
print_header
|
||
show_help
|
||
exit 1
|
||
fi
|
||
|
||
# Show header for all operations except help
|
||
if [[ "${1,,}" != "help" ]] && [[ "${1,,}" != "--help" ]] && [[ "${1,,}" != "-h" ]]; then
|
||
print_header
|
||
fi
|
||
|
||
case "${1,,}" in # Convert to lowercase
|
||
"start")
|
||
start_plex
|
||
;;
|
||
"stop")
|
||
stop_plex
|
||
;;
|
||
"restart"|"reload")
|
||
restart_plex
|
||
;;
|
||
"status"|"info")
|
||
show_detailed_status
|
||
;;
|
||
"repair"|"fix")
|
||
repair_plex
|
||
;;
|
||
"help"|"--help"|"-h")
|
||
print_header
|
||
show_help
|
||
;;
|
||
*)
|
||
print_status "${CROSS}" "Unknown command: ${RED}${BOLD}$1${RESET}" "${RED}"
|
||
echo ""
|
||
show_help
|
||
exit 1
|
||
;;
|
||
esac
|
||
}
|
||
|
||
# 🚀 Execute main function with all arguments
|
||
main "$@" |