From ddaa6416683409a0d38fe0f69c754ea72dff94dd Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Sat, 7 Mar 2026 10:29:14 -0500 Subject: [PATCH] feat: Improve database integrity check and startup handling in plex.sh --- plex/plex.sh | 65 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/plex/plex.sh b/plex/plex.sh index a0159b1..38989b4 100755 --- a/plex/plex.sh +++ b/plex/plex.sh @@ -298,34 +298,57 @@ check_database_integrity() { 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 + # Clean up stale WAL/SHM journals left by non-clean shutdowns. + # If Plex was killed or the system rebooted unexpectedly, these files + # can prevent sqlite3 from opening the database at all, producing + # false "corruption" reports. + if [[ -f "${main_db}-wal" ]]; then + print_status "${INFO}" "WAL journal found — attempting checkpoint before integrity check..." "${BLUE}" + # TRUNCATE mode replays the journal and then removes it. + if ! sudo -u plex sqlite3 "$main_db" "PRAGMA wal_checkpoint(TRUNCATE);" 2>/dev/null; then + print_status "${INFO}" "WAL checkpoint failed (non-critical, continuing check)" "${YELLOW}" fi + fi + # Run the actual integrity check and capture stdout+stderr separately. + # PRAGMA integrity_check prints "ok" on success or error descriptions; + # the sqlite3 exit code alone is NOT reliable for detecting corruption. + local integrity_output + local sqlite_exit_code=0 + integrity_output=$(sudo -u plex sqlite3 "$main_db" "PRAGMA integrity_check;" 2>&1) || sqlite_exit_code=$? + + # Case 1: Output is exactly "ok" — database is healthy. + if [[ "$integrity_output" == "ok" ]]; then print_status "${CHECKMARK}" "Database integrity check passed" "${GREEN}" return 0 fi + + # Case 2: sqlite3 couldn't open the file at all (lock, permission, not a db). + if [[ $sqlite_exit_code -ne 0 && -z "$integrity_output" ]]; then + print_status "${CROSS}" "sqlite3 failed to open the database (exit code $sqlite_exit_code)" "${RED}" + print_status "${INFO}" "Check file permissions and ensure Plex is fully stopped" "${YELLOW}" + return 1 + fi + + # Case 3: Genuine corruption reported by PRAGMA integrity_check. + print_status "${CROSS}" "Database integrity check reported issues:" "${RED}" + # Show first 5 lines of the report to avoid flooding the terminal. + echo "$integrity_output" | head -n 5 | while IFS= read -r line; do + echo -e "${DIM}${YELLOW} $line${RESET}" + done + local total_lines + total_lines=$(echo "$integrity_output" | wc -l) + if (( total_lines > 5 )); then + echo -e "${DIM}${YELLOW} ... and $((total_lines - 5)) more issue(s)${RESET}" + fi + print_status "${INFO}" "Consider running database repair: plex repair" "${YELLOW}" + return 1 } # �🚀 Enhanced start function @@ -341,11 +364,11 @@ start_plex() { # Reset any failed state first sudo systemctl reset-failed "$PLEX_SERVICE" 2>/dev/null || true - # Check database integrity before starting + # Check database integrity before starting (warn only — don't block startup). + # Many "failures" are benign WAL journal leftovers that Plex resolves on its own. 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 + print_status "${INFO}" "Database integrity issues detected — starting Plex anyway (it may self-repair)." "${YELLOW}" + echo -e "${DIM}${YELLOW} If Plex fails to start, run: ${SCRIPT_NAME} repair${RESET}" fi print_status "${INFO}" "Attempting to start service..." "${BLUE}"