mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 03:20:12 -08:00
added the ability to initiate a plex library scan from the CLI. tab completions are supported as well.
This commit is contained in:
151
plex/plex.sh
151
plex/plex.sh
@@ -19,6 +19,7 @@
|
||||
# Related Scripts:
|
||||
# - backup-plex.sh: Comprehensive backup solution
|
||||
# - restore-plex.sh: Backup restoration utilities
|
||||
# - scan-plex-libraries.sh: Library scanning and metadata management
|
||||
# - monitor-plex-backup.sh: Backup system monitoring
|
||||
# - validate-plex-backups.sh: Backup validation tools
|
||||
# - test-plex-backup.sh: Testing framework
|
||||
@@ -28,6 +29,7 @@
|
||||
# ./plex.sh stop # Stop Plex service
|
||||
# ./plex.sh restart # Restart Plex service
|
||||
# ./plex.sh status # Show service status
|
||||
# ./plex.sh scan # Launch library scanner
|
||||
# ./plex.sh # Interactive menu
|
||||
#
|
||||
# Dependencies:
|
||||
@@ -111,40 +113,40 @@ show_loading() {
|
||||
# 🔧 Enhanced 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)"
|
||||
local corrupted_dir="$db_dir/corrupted-$(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 3
|
||||
|
||||
|
||||
# Check if critical tables exist
|
||||
print_status "${INFO}" "Checking database structure..." "${BLUE}"
|
||||
local has_metadata_table=false
|
||||
if sudo -u plex sqlite3 "$main_db" "SELECT name FROM sqlite_master WHERE type='table' AND name='metadata_items';" 2>/dev/null | grep -q metadata_items; then
|
||||
has_metadata_table=true
|
||||
fi
|
||||
|
||||
|
||||
if [[ "$has_metadata_table" == "false" ]]; then
|
||||
print_status "${CROSS}" "Critical table 'metadata_items' is missing! Database is severely corrupted." "${RED}"
|
||||
print_status "${INFO}" "Attempting recovery from available backups..." "${YELLOW}"
|
||||
|
||||
|
||||
# Find the best recovery candidate
|
||||
local recovery_db=""
|
||||
local recovery_candidates=(
|
||||
"$db_dir/com.plexapp.plugins.library.db.recovery-"*
|
||||
"$db_dir/com.plexapp.plugins.library.db.20"*
|
||||
)
|
||||
|
||||
|
||||
for candidate in "${recovery_candidates[@]}"; do
|
||||
if [[ -f "$candidate" && "$candidate" != *"tmp"* && "$candidate" != *"empty"* ]]; then
|
||||
# Test if this candidate has the metadata_items table
|
||||
@@ -154,36 +156,36 @@ repair_database() {
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
if [[ -n "$recovery_db" ]]; then
|
||||
print_status "${CHECKMARK}" "Found recovery database: $(basename "$recovery_db")" "${GREEN}"
|
||||
|
||||
|
||||
# Move corrupted database to backup location
|
||||
print_status "${INFO}" "Moving corrupted database to backup location..." "${BLUE}"
|
||||
sudo mkdir -p "$corrupted_dir"
|
||||
sudo mv "$main_db" "$corrupted_dir/"
|
||||
sudo mv "$main_db-shm" "$corrupted_dir/" 2>/dev/null || true
|
||||
sudo mv "$main_db-wal" "$corrupted_dir/" 2>/dev/null || true
|
||||
|
||||
|
||||
# Copy recovery database as new main database
|
||||
print_status "${INFO}" "Restoring database from recovery file..." "${BLUE}"
|
||||
if sudo cp "$recovery_db" "$main_db"; then
|
||||
sudo chown plex:plex "$main_db"
|
||||
sudo chmod 644 "$main_db"
|
||||
print_status "${CHECKMARK}" "Database restored successfully!" "${GREEN}"
|
||||
|
||||
|
||||
# Verify the restored database
|
||||
print_status "${INFO}" "Verifying restored database..." "${BLUE}"
|
||||
local integrity_result
|
||||
integrity_result=$(sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" 2>&1)
|
||||
|
||||
|
||||
if echo "$integrity_result" | grep -q "ok"; then
|
||||
print_status "${CHECKMARK}" "Restored database integrity verified!" "${GREEN}"
|
||||
return 0
|
||||
elif echo "$integrity_result" | grep -q "no such collation sequence: icu"; then
|
||||
print_status "${CROSS}" "ICU collation sequence issue detected!" "${YELLOW}"
|
||||
print_status "${INFO}" "Attempting ICU-aware recovery..." "${BLUE}"
|
||||
|
||||
|
||||
# Try ICU-aware recovery script
|
||||
local icu_script="${SCRIPT_DIR}/icu-aware-recovery.sh"
|
||||
if [[ -f "$icu_script" ]]; then
|
||||
@@ -195,28 +197,28 @@ repair_database() {
|
||||
fi
|
||||
else
|
||||
print_status "${INFO}" "ICU recovery script not found, trying manual fix..." "${YELLOW}"
|
||||
|
||||
|
||||
# Try to recreate database without ICU dependencies
|
||||
local temp_db="/tmp/plex_temp_$(date +%Y%m%d_%H%M%S).db"
|
||||
print_status "${INFO}" "Attempting to dump and recreate database..." "${BLUE}"
|
||||
|
||||
|
||||
if sudo -u plex sqlite3 "$recovery_db" ".dump" | grep -v "COLLATE icu_" | sudo -u plex sqlite3 "$temp_db"; then
|
||||
print_status "${INFO}" "Database dump successful, replacing main database..." "${BLUE}"
|
||||
sudo mv "$temp_db" "$main_db"
|
||||
sudo chown plex:plex "$main_db"
|
||||
sudo chmod 644 "$main_db"
|
||||
|
||||
|
||||
# Verify the recreated database
|
||||
if sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" 2>/dev/null | grep -q "ok"; then
|
||||
print_status "${CHECKMARK}" "Database recreated successfully without ICU!" "${GREEN}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# Clean up temp file if it exists
|
||||
sudo rm -f "$temp_db" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
|
||||
print_status "${CROSS}" "Failed to resolve ICU collation issues!" "${RED}"
|
||||
return 1
|
||||
else
|
||||
@@ -238,21 +240,21 @@ repair_database() {
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# 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;" 2>/dev/null; then
|
||||
print_status "${CHECKMARK}" "Database VACUUM completed successfully" "${GREEN}"
|
||||
|
||||
|
||||
# Test integrity again
|
||||
if sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" 2>/dev/null | grep -q "ok"; then
|
||||
print_status "${CHECKMARK}" "Database integrity restored!" "${GREEN}"
|
||||
@@ -264,41 +266,41 @@ repair_database() {
|
||||
else
|
||||
print_status "${CROSS}" "VACUUM operation failed" "${RED}"
|
||||
fi
|
||||
|
||||
|
||||
# Try reindex as last resort
|
||||
print_status "${INFO}" "Attempting REINDEX operation..." "${BLUE}"
|
||||
if sudo -u plex sqlite3 "$main_db" "REINDEX;" 2>/dev/null; then
|
||||
print_status "${CHECKMARK}" "Database REINDEX completed" "${GREEN}"
|
||||
|
||||
|
||||
# Test integrity one more time
|
||||
if sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" 2>/dev/null | grep -q "ok"; then
|
||||
print_status "${CHECKMARK}" "Database integrity restored after REINDEX!" "${GREEN}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
print_status "${CROSS}" "All repair attempts failed" "${RED}"
|
||||
print_status "${INFO}" "Manual intervention required. Consider:" "${YELLOW}"
|
||||
echo -e "${DIM}${YELLOW} 1. Restore from external backup using restore-plex.sh${RESET}"
|
||||
echo -e "${DIM}${YELLOW} 2. Use nuclear recovery: ./nuclear-plex-recovery.sh${RESET}"
|
||||
echo -e "${DIM}${YELLOW} 3. Check corrupted database moved to: $corrupted_dir${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
|
||||
@@ -316,7 +318,7 @@ check_database_integrity() {
|
||||
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
|
||||
@@ -334,7 +336,7 @@ start_plex() {
|
||||
|
||||
# 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}"
|
||||
@@ -343,15 +345,15 @@ start_plex() {
|
||||
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
|
||||
|
||||
@@ -359,34 +361,34 @@ start_plex() {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -469,7 +471,7 @@ show_detailed_status() {
|
||||
|
||||
# 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
|
||||
@@ -487,7 +489,7 @@ show_detailed_status() {
|
||||
echo -e "${DIM}${logs}${RESET}"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
echo -e "${DIM}${CYAN}+----------------------------------+${RESET}"
|
||||
}
|
||||
|
||||
@@ -500,6 +502,7 @@ show_help() {
|
||||
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 " ${PURPLE}${BOLD}scan${RESET} ${SPARKLES} Library scanner operations"
|
||||
echo -e " ${RED}${BOLD}repair${RESET} [!] Repair database corruption issues"
|
||||
echo -e " ${RED}${BOLD}nuclear${RESET} [!!] Nuclear database recovery (last resort)"
|
||||
echo -e " ${PURPLE}${BOLD}help${RESET} [*] Show this help message"
|
||||
@@ -507,41 +510,72 @@ show_help() {
|
||||
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} scan # Launch library scanner interface${RESET}"
|
||||
echo -e " ${DIM}${SCRIPT_NAME} repair # Fix database issues${RESET}"
|
||||
echo -e " ${DIM}${SCRIPT_NAME} nuclear # Complete database replacement${RESET}"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# 📚 Function to launch library scanner
|
||||
launch_scanner() {
|
||||
print_status "${SPARKLES}" "Launching Plex Library Scanner..." "${PURPLE}"
|
||||
|
||||
local scanner_script="${SCRIPT_DIR}/scan-plex-libraries.sh"
|
||||
|
||||
if [[ ! -f "$scanner_script" ]]; then
|
||||
print_status "${CROSS}" "Library scanner script not found: $scanner_script" "${RED}"
|
||||
print_status "${INFO}" "Expected location: ${SCRIPT_DIR}/scan-plex-libraries.sh" "${YELLOW}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ ! -x "$scanner_script" ]]; then
|
||||
print_status "${INFO}" "Making scanner script executable..." "${BLUE}"
|
||||
chmod +x "$scanner_script"
|
||||
fi
|
||||
|
||||
# Check if additional arguments were passed
|
||||
if [[ $# -gt 1 ]]; then
|
||||
# Pass remaining arguments to the scanner script
|
||||
shift # Remove 'scan' argument
|
||||
print_status "${INFO}" "Executing scanner with arguments: $*" "${BLUE}"
|
||||
exec "$scanner_script" "$@"
|
||||
else
|
||||
# No additional arguments, launch interactive mode
|
||||
print_status "${INFO}" "Launching scanner in interactive mode..." "${BLUE}"
|
||||
exec "$scanner_script" interactive
|
||||
fi
|
||||
}
|
||||
|
||||
# Nuclear database recovery function
|
||||
nuclear_recovery() {
|
||||
print_status "${INFO}" "Starting nuclear database recovery..." "${RED}"
|
||||
|
||||
|
||||
local nuclear_script="${SCRIPT_DIR}/nuclear-plex-recovery.sh"
|
||||
|
||||
|
||||
if [[ ! -f "$nuclear_script" ]]; then
|
||||
print_status "${CROSS}" "Nuclear recovery script not found: $nuclear_script" "${RED}"
|
||||
print_status "${INFO}" "This script should be in the same directory as plex.sh" "${YELLOW}"
|
||||
return 2
|
||||
fi
|
||||
|
||||
|
||||
# Warning message
|
||||
echo -e "\n${RED}${BOLD}⚠️ WARNING: NUCLEAR RECOVERY ⚠️${RESET}"
|
||||
echo -e "${RED}This will completely replace your Plex database with a backup!${RESET}"
|
||||
echo -e "${RED}All changes since the backup was created will be lost!${RESET}"
|
||||
echo -e "${YELLOW}This should only be used when standard repair methods have failed.${RESET}\n"
|
||||
|
||||
|
||||
# Get user confirmation
|
||||
echo -e "${CYAN}Do you want to proceed with nuclear recovery? ${RESET}"
|
||||
echo -e "${DIM}Type 'YES' (uppercase) to confirm: ${RESET}"
|
||||
read -r confirmation
|
||||
|
||||
|
||||
if [[ "$confirmation" != "YES" ]]; then
|
||||
print_status "${INFO}" "Nuclear recovery cancelled by user" "${YELLOW}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
|
||||
print_status "${INFO}" "Executing nuclear recovery script..." "${BLUE}"
|
||||
|
||||
|
||||
# Execute the nuclear recovery script
|
||||
if sudo "$nuclear_script" --auto; then
|
||||
print_status "${CHECKMARK}" "Nuclear recovery completed successfully!" "${GREEN}"
|
||||
@@ -551,7 +585,7 @@ nuclear_recovery() {
|
||||
else
|
||||
local exit_code=$?
|
||||
print_status "${CROSS}" "Nuclear recovery failed!" "${RED}"
|
||||
|
||||
|
||||
case $exit_code in
|
||||
2) print_status "${INFO}" "Backup file issues - check backup integrity" "${YELLOW}" ;;
|
||||
3) print_status "${INFO}" "Database replacement failure - check permissions" "${YELLOW}" ;;
|
||||
@@ -559,7 +593,7 @@ nuclear_recovery() {
|
||||
5) print_status "${INFO}" "Rollback performed due to failure" "${YELLOW}" ;;
|
||||
*) print_status "${INFO}" "Unknown error occurred during recovery" "${YELLOW}" ;;
|
||||
esac
|
||||
|
||||
|
||||
print_footer
|
||||
return $exit_code
|
||||
fi
|
||||
@@ -568,11 +602,11 @@ nuclear_recovery() {
|
||||
# Database repair function
|
||||
repair_plex() {
|
||||
print_status "${INFO}" "Starting Plex database repair..." "${YELLOW}"
|
||||
|
||||
|
||||
# Run the enhanced repair function
|
||||
if repair_database; 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
|
||||
@@ -594,17 +628,17 @@ repair_plex() {
|
||||
else
|
||||
local repair_exit_code=$?
|
||||
print_status "${CROSS}" "Database repair failed!" "${RED}"
|
||||
|
||||
|
||||
# Try to start the service anyway in case partial repair helped
|
||||
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
|
||||
@@ -637,6 +671,9 @@ main() {
|
||||
"status"|"info")
|
||||
show_detailed_status
|
||||
;;
|
||||
"scan"|"scanner"|"library")
|
||||
launch_scanner "$@"
|
||||
;;
|
||||
"repair"|"fix")
|
||||
repair_plex
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user