mirror of
https://github.com/acedanger/shell.git
synced 2026-03-24 20:11:49 -07:00
feat: Enhance database integrity checks and repair functionality across scripts
This commit is contained in:
@@ -52,10 +52,26 @@ readonly MAIN_DB="$PLEX_DB_DIR/com.plexapp.plugins.library.db"
|
||||
readonly BLOBS_DB="$PLEX_DB_DIR/com.plexapp.plugins.library.blobs.db"
|
||||
readonly BACKUP_ROOT="/mnt/share/media/backups/plex"
|
||||
|
||||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||
readonly SCRIPT_DIR
|
||||
readonly LOG_FILE="$SCRIPT_DIR/logs/db-manager-$(date +%Y%m%d_%H%M%S).log"
|
||||
|
||||
# DBRepair.sh location — searched in order: script dir, database dir, /usr/local/bin
|
||||
find_dbrepair() {
|
||||
local candidates=(
|
||||
"${SCRIPT_DIR}/DBRepair.sh"
|
||||
"${PLEX_DB_DIR}/DBRepair.sh"
|
||||
"/usr/local/bin/DBRepair.sh"
|
||||
)
|
||||
for path in "${candidates[@]}"; do
|
||||
if [[ -x "$path" ]]; then
|
||||
echo "$path"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Create log directory
|
||||
mkdir -p "$SCRIPT_DIR/logs"
|
||||
|
||||
@@ -210,26 +226,54 @@ check_database_integrity() {
|
||||
fi
|
||||
fi
|
||||
|
||||
# Run integrity check
|
||||
# Run structural integrity check
|
||||
local integrity_result
|
||||
integrity_result=$(sudo "$PLEX_SQLITE" "$db_file" "PRAGMA integrity_check;" 2>&1)
|
||||
local check_exit_code=$?
|
||||
|
||||
if [[ $check_exit_code -ne 0 ]]; then
|
||||
log_error "Failed to run integrity check on $db_name"
|
||||
if [[ $check_exit_code -ne 0 && -z "$integrity_result" ]]; then
|
||||
log_error "Failed to open database: $db_name (exit code $check_exit_code)"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if echo "$integrity_result" | grep -q "^ok$"; then
|
||||
log_success "Database integrity check passed: $db_name"
|
||||
return 0
|
||||
local struct_ok=true
|
||||
if [[ "$integrity_result" == "ok" ]]; then
|
||||
log_success "Structural integrity check passed: $db_name"
|
||||
else
|
||||
log_warning "Database integrity issues detected in $db_name:"
|
||||
echo "$integrity_result" | while IFS= read -r line; do
|
||||
struct_ok=false
|
||||
log_warning "Structural integrity issues detected in $db_name:"
|
||||
echo "$integrity_result" | head -n 10 | while IFS= read -r line; do
|
||||
log_warning " $line"
|
||||
done
|
||||
return 1
|
||||
fi
|
||||
|
||||
# FTS (Full-Text Search) index integrity check
|
||||
# Standard PRAGMA integrity_check does NOT detect FTS corruption.
|
||||
local fts_ok=true
|
||||
local fts_tables
|
||||
fts_tables=$(sudo "$PLEX_SQLITE" "$db_file" \
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND sql LIKE '%fts%';" 2>/dev/null) || true
|
||||
|
||||
if [[ -n "$fts_tables" ]]; then
|
||||
log_message "Checking FTS (Full-Text Search) indexes in $db_name..."
|
||||
while IFS= read -r table; do
|
||||
[[ -z "$table" ]] && continue
|
||||
local fts_result
|
||||
fts_result=$(sudo "$PLEX_SQLITE" "$db_file" \
|
||||
"INSERT INTO ${table}(${table}) VALUES('integrity-check');" 2>&1) || true
|
||||
if [[ -n "$fts_result" ]]; then
|
||||
fts_ok=false
|
||||
log_warning "FTS index '${table}' — DAMAGED: $fts_result"
|
||||
else
|
||||
log_success "FTS index '${table}' — OK"
|
||||
fi
|
||||
done <<< "$fts_tables"
|
||||
fi
|
||||
|
||||
if [[ "$struct_ok" == true && "$fts_ok" == true ]]; then
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Check all databases
|
||||
@@ -315,20 +359,73 @@ main() {
|
||||
;;
|
||||
|
||||
"repair")
|
||||
echo -e "${RED}${BOLD}⚠️ REPAIR FUNCTIONALITY TEMPORARILY DISABLED${RESET}"
|
||||
echo -e "${YELLOW}Database repairs are disabled until corruption issues are resolved.${RESET}"
|
||||
echo -e "${CYAN}Use the individual repair scripts if manual intervention is needed:${RESET}"
|
||||
echo -e " ${DIM}- plex-database-repair.sh${RESET}"
|
||||
echo -e " ${DIM}- recover-plex-database.sh${RESET}"
|
||||
echo -e " ${DIM}- nuclear-plex-recovery.sh${RESET}"
|
||||
exit 2
|
||||
print_header
|
||||
check_prerequisites
|
||||
|
||||
local dbrepair_bin
|
||||
if dbrepair_bin=$(find_dbrepair); then
|
||||
log_success "Found DBRepair.sh: $dbrepair_bin"
|
||||
log_message "Running: stop → auto (check + repair + reindex + FTS rebuild) → start → exit"
|
||||
if sudo "$dbrepair_bin" stop auto start exit; then
|
||||
log_success "DBRepair automatic repair completed successfully"
|
||||
exit 0
|
||||
else
|
||||
log_error "DBRepair automatic repair failed"
|
||||
exit 2
|
||||
fi
|
||||
else
|
||||
echo -e "${RED}${BOLD}⚠️ DBRepair.sh NOT FOUND${RESET}"
|
||||
echo -e "${YELLOW}Install DBRepair for repair/optimize/reindex/FTS-rebuild:${RESET}"
|
||||
echo -e " ${CYAN}wget -O ${SCRIPT_DIR}/DBRepair.sh https://github.com/ChuckPa/PlexDBRepair/releases/latest/download/DBRepair.sh${RESET}"
|
||||
echo -e " ${CYAN}chmod +x ${SCRIPT_DIR}/DBRepair.sh${RESET}"
|
||||
echo -e "${YELLOW}Then re-run: $(basename "$0") repair${RESET}"
|
||||
exit 2
|
||||
fi
|
||||
;;
|
||||
|
||||
"nuclear")
|
||||
echo -e "${RED}${BOLD}⚠️ NUCLEAR RECOVERY TEMPORARILY DISABLED${RESET}"
|
||||
echo -e "${YELLOW}Nuclear recovery is disabled until corruption issues are resolved.${RESET}"
|
||||
echo -e "${CYAN}Use nuclear-plex-recovery.sh directly if absolutely necessary.${RESET}"
|
||||
exit 2
|
||||
print_header
|
||||
check_prerequisites
|
||||
|
||||
echo -e "\n${RED}${BOLD}⚠️ WARNING: NUCLEAR RECOVERY ⚠️${RESET}"
|
||||
echo -e "${RED}This replaces your Plex database with the best available PMS backup!${RESET}"
|
||||
echo -e "${YELLOW}All changes since the backup was created will be lost.${RESET}\n"
|
||||
|
||||
echo -e "${CYAN}Type 'YES' to proceed: ${RESET}"
|
||||
read -r confirmation
|
||||
if [[ "$confirmation" != "YES" ]]; then
|
||||
log_message "Nuclear recovery cancelled by user"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
local dbrepair_bin
|
||||
if dbrepair_bin=$(find_dbrepair); then
|
||||
log_success "Found DBRepair.sh: $dbrepair_bin"
|
||||
log_message "Running: stop → replace → reindex → start → exit"
|
||||
if sudo "$dbrepair_bin" stop replace reindex start exit; then
|
||||
log_success "Nuclear recovery (replace from backup) completed"
|
||||
exit 0
|
||||
else
|
||||
log_error "Nuclear recovery failed"
|
||||
exit 2
|
||||
fi
|
||||
else
|
||||
# Fallback to dedicated nuclear script
|
||||
local nuclear_script="${SCRIPT_DIR}/nuclear-plex-recovery.sh"
|
||||
if [[ -x "$nuclear_script" ]]; then
|
||||
log_message "DBRepair not found, falling back to nuclear-plex-recovery.sh"
|
||||
if sudo "$nuclear_script" --auto; then
|
||||
log_success "Nuclear recovery completed"
|
||||
exit 0
|
||||
else
|
||||
log_error "Nuclear recovery failed"
|
||||
exit 2
|
||||
fi
|
||||
else
|
||||
log_error "Neither DBRepair.sh nor nuclear-plex-recovery.sh found"
|
||||
exit 2
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
|
||||
"help"|"--help"|"-h")
|
||||
|
||||
Reference in New Issue
Block a user