feat: Enhance database integrity checks and repair functionality across scripts

This commit is contained in:
Peter Wood
2026-03-07 10:42:41 -05:00
parent ddaa641668
commit 9bb99aecbf
3 changed files with 355 additions and 213 deletions

View File

@@ -66,6 +66,7 @@ NC='\033[0m' # No Color
# Configuration
PLEX_DB_DIR="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases"
PLEX_SQLITE="/usr/lib/plexmediaserver/Plex SQLite"
PLEX_USER="plex"
PLEX_GROUP="plex"
BACKUP_TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
@@ -244,27 +245,66 @@ restore_from_backup() {
fi
}
# Function to verify restored databases
# Function to verify restored databases (structural + FTS)
verify_databases() {
print_status "$YELLOW" "Verifying restored databases..."
# Check main database
if sqlite3 "${PLEX_DB_DIR}/com.plexapp.plugins.library.db" "PRAGMA integrity_check;" | grep -q "ok"; then
print_status "$GREEN" "Main database integrity check: PASSED"
else
print_status "$RED" "Main database integrity check: FAILED"
return 1
# Use Plex's bundled SQLite for ICU compatibility; fall back to system sqlite3
local sqlite_bin="sqlite3"
if [[ -x "$PLEX_SQLITE" ]]; then
sqlite_bin="$PLEX_SQLITE"
fi
# Check blobs database
if sqlite3 "${PLEX_DB_DIR}/com.plexapp.plugins.library.blobs.db" "PRAGMA integrity_check;" | grep -q "ok"; then
print_status "$GREEN" "Blobs database integrity check: PASSED"
local overall_ok=true
for db_file in \
"${PLEX_DB_DIR}/com.plexapp.plugins.library.db" \
"${PLEX_DB_DIR}/com.plexapp.plugins.library.blobs.db"; do
local db_name
db_name=$(basename "$db_file")
if [[ ! -f "$db_file" ]]; then
print_status "$RED" "$db_name: NOT FOUND"
overall_ok=false
continue
fi
# Structural integrity
local result
result=$("$sqlite_bin" "$db_file" "PRAGMA integrity_check;" 2>&1)
if [[ "$result" == "ok" ]]; then
print_status "$GREEN" "$db_name structural integrity: PASSED"
else
print_status "$RED" "$db_name structural integrity: FAILED"
overall_ok=false
fi
# FTS index integrity
local fts_tables
fts_tables=$("$sqlite_bin" "$db_file" \
"SELECT name FROM sqlite_master WHERE type='table' AND sql LIKE '%fts%';" 2>/dev/null) || true
if [[ -n "$fts_tables" ]]; then
while IFS= read -r table; do
[[ -z "$table" ]] && continue
local fts_result
fts_result=$("$sqlite_bin" "$db_file" \
"INSERT INTO ${table}(${table}) VALUES('integrity-check');" 2>&1) || true
if [[ -n "$fts_result" ]]; then
print_status "$RED" "$db_name FTS index '$table': DAMAGED"
overall_ok=false
fi
done <<< "$fts_tables"
fi
done
if [[ "$overall_ok" == true ]]; then
print_status "$GREEN" "All database integrity checks passed!"
return 0
else
print_status "$RED" "Blobs database integrity check: FAILED"
print_status "$RED" "One or more database checks failed!"
return 1
fi
print_status "$GREEN" "All database integrity checks passed!"
}
# Function to fix ownership issues