mirror of
https://github.com/acedanger/shell.git
synced 2026-03-24 23:41:50 -07:00
Compare commits
6 Commits
9bb99aecbf
...
9196879b6c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9196879b6c | ||
|
|
dc8d35f593 | ||
|
|
c3af84b3e6 | ||
|
|
edae8513d1 | ||
|
|
8e7e22a1a3 | ||
|
|
2bae9bc6ce |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -43,3 +43,4 @@ dotfiles/my-aliases.zsh
|
|||||||
|
|
||||||
# Compiled binaries
|
# Compiled binaries
|
||||||
tui/tui
|
tui/tui
|
||||||
|
plex/DBRepair.sh
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ readonly SCRIPT_DIR
|
|||||||
readonly LOG_FILE="$SCRIPT_DIR/logs/db-manager-$(date +%Y%m%d_%H%M%S).log"
|
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
|
# DBRepair.sh location — searched in order: script dir, database dir, /usr/local/bin
|
||||||
|
readonly DBREPAIR_INSTALL_PATH="${SCRIPT_DIR}/DBRepair.sh"
|
||||||
|
readonly DBREPAIR_GITHUB_API="https://api.github.com/repos/ChuckPa/PlexDBRepair/releases"
|
||||||
|
readonly DBREPAIR_DOWNLOAD_BASE="https://github.com/ChuckPa/PlexDBRepair/releases/download"
|
||||||
|
|
||||||
find_dbrepair() {
|
find_dbrepair() {
|
||||||
local candidates=(
|
local candidates=(
|
||||||
"${SCRIPT_DIR}/DBRepair.sh"
|
"${SCRIPT_DIR}/DBRepair.sh"
|
||||||
@@ -72,6 +76,231 @@ find_dbrepair() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get the latest non-beta release tag from GitHub
|
||||||
|
_dbrepair_latest_release_tag() {
|
||||||
|
local tag
|
||||||
|
tag=$(curl -fsSL "${DBREPAIR_GITHUB_API}" 2>/dev/null \
|
||||||
|
| grep -Eo '"tag_name"\s*:\s*"[^"]+"' \
|
||||||
|
| head -n 1 \
|
||||||
|
| sed 's/"tag_name"\s*:\s*"//;s/"//')
|
||||||
|
if [[ -z "$tag" ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo "$tag"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get currently installed DBRepair version
|
||||||
|
_dbrepair_installed_version() {
|
||||||
|
local bin="$1"
|
||||||
|
grep -oP 'Version\s+v\K[0-9.]+' "$bin" 2>/dev/null | head -n 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Install or update DBRepair.sh
|
||||||
|
install_or_update_dbrepair() {
|
||||||
|
log_message "Checking DBRepair (ChuckPa/PlexDBRepair)..."
|
||||||
|
|
||||||
|
local latest_tag
|
||||||
|
if ! latest_tag=$(_dbrepair_latest_release_tag); then
|
||||||
|
log_error "Failed to query GitHub for the latest release"
|
||||||
|
log_warning "Check your internet connection or try: https://github.com/ChuckPa/PlexDBRepair/releases"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
log_message "Latest stable release: ${latest_tag}"
|
||||||
|
|
||||||
|
local dbrepair_bin
|
||||||
|
if dbrepair_bin=$(find_dbrepair); then
|
||||||
|
local installed_ver
|
||||||
|
installed_ver=$(_dbrepair_installed_version "$dbrepair_bin")
|
||||||
|
local remote_ver
|
||||||
|
remote_ver=$(echo "$latest_tag" | sed 's/^v//')
|
||||||
|
|
||||||
|
if [[ -n "$installed_ver" ]]; then
|
||||||
|
log_message "Installed version: v${installed_ver} at ${dbrepair_bin}"
|
||||||
|
if [[ "$installed_ver" == "$remote_ver" ]]; then
|
||||||
|
log_success "DBRepair is already up to date (v${installed_ver})"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log_warning "Update available: v${installed_ver} -> ${latest_tag}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log_warning "Installed at ${dbrepair_bin} (version unknown), will update"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log_message "DBRepair not found — installing to ${DBREPAIR_INSTALL_PATH}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local download_url="${DBREPAIR_DOWNLOAD_BASE}/${latest_tag}/DBRepair.sh"
|
||||||
|
log_message "Downloading ${download_url}"
|
||||||
|
if curl -fsSL -o "${DBREPAIR_INSTALL_PATH}" "$download_url"; then
|
||||||
|
chmod +x "${DBREPAIR_INSTALL_PATH}"
|
||||||
|
log_success "DBRepair ${latest_tag} installed to ${DBREPAIR_INSTALL_PATH}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log_error "Download failed"
|
||||||
|
rm -f "${DBREPAIR_INSTALL_PATH}" 2>/dev/null
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Suggest installing DBRepair when errors are found and it's not available
|
||||||
|
_hint_install_dbrepair() {
|
||||||
|
if ! find_dbrepair >/dev/null 2>&1; then
|
||||||
|
echo ""
|
||||||
|
log_warning "DBRepair is NOT installed. It can fix most database issues automatically."
|
||||||
|
echo -e " ${CYAN}Install it now: $(basename "$0") install-dbrepair${RESET}"
|
||||||
|
echo -e " ${CYAN}Then repair: $(basename "$0") repair${RESET}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# List and manage database backup files
|
||||||
|
list_db_backups() {
|
||||||
|
local db_dir="$PLEX_DB_DIR"
|
||||||
|
local -a backup_files=()
|
||||||
|
local -a backup_paths=()
|
||||||
|
|
||||||
|
while IFS= read -r -d '' entry; do
|
||||||
|
backup_paths+=("$entry")
|
||||||
|
done < <(sudo find "$db_dir" -maxdepth 1 \( \
|
||||||
|
-name '*-BACKUP-*' -o \
|
||||||
|
-name '*-BKUP-*' -o \
|
||||||
|
-name '*.backup.*' -o \
|
||||||
|
-name '*recovery*' -o \
|
||||||
|
-name 'corrupted-*' -o \
|
||||||
|
-name '*-BLOATED-*' \
|
||||||
|
\) -print0 2>/dev/null | sort -z)
|
||||||
|
|
||||||
|
if [[ ${#backup_paths[@]} -eq 0 ]]; then
|
||||||
|
log_message "No database backup files found in the Plex database directory"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\n${BOLD}${WHITE} # Type Size Created Name${RESET}"
|
||||||
|
echo -e " --- ------------- ----------- ------------------------ ------------------------------------"
|
||||||
|
|
||||||
|
local idx=0
|
||||||
|
for entry in "${backup_paths[@]}"; do
|
||||||
|
((idx++))
|
||||||
|
local name
|
||||||
|
name=$(basename "$entry")
|
||||||
|
|
||||||
|
local type_label
|
||||||
|
if [[ "$name" == *-BACKUP-* || "$name" == *-BKUP-* ]]; then
|
||||||
|
type_label="DBRepair"
|
||||||
|
elif [[ "$name" == *-BLOATED-* ]]; then
|
||||||
|
type_label="Bloated"
|
||||||
|
elif [[ "$name" == *.backup.* ]]; then
|
||||||
|
type_label="Script"
|
||||||
|
elif [[ "$name" == corrupted-* ]]; then
|
||||||
|
type_label="Corrupted"
|
||||||
|
elif [[ "$name" == *recovery* ]]; then
|
||||||
|
type_label="Recovery"
|
||||||
|
else
|
||||||
|
type_label="Other"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local size
|
||||||
|
if [[ -d "$entry" ]]; then
|
||||||
|
size=$(sudo du -sh "$entry" 2>/dev/null | cut -f1)
|
||||||
|
type_label="${type_label}/dir"
|
||||||
|
else
|
||||||
|
size=$(sudo stat --printf='%s' "$entry" 2>/dev/null)
|
||||||
|
if [[ -n "$size" ]]; then
|
||||||
|
size=$(numfmt --to=iec-i --suffix=B "$size" 2>/dev/null || echo "${size}B")
|
||||||
|
else
|
||||||
|
size="?"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
local created
|
||||||
|
created=$(sudo stat --printf='%y' "$entry" 2>/dev/null | cut -d. -f1)
|
||||||
|
[[ -z "$created" ]] && created="unknown"
|
||||||
|
|
||||||
|
printf " ${WHITE}%-3s${RESET} ${CYAN}%-13s${RESET} ${YELLOW}%-11s${RESET} %-24s %s\n" \
|
||||||
|
"$idx" "$type_label" "$size" "$created" "$name"
|
||||||
|
|
||||||
|
backup_files+=("$entry")
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e " --- ------------- ----------- ------------------------ ------------------------------------"
|
||||||
|
echo -e " Total: ${idx} backup file(s)\n"
|
||||||
|
|
||||||
|
_BACKUP_LIST=("${backup_files[@]}")
|
||||||
|
_BACKUP_COUNT=$idx
|
||||||
|
}
|
||||||
|
|
||||||
|
# Interactive backup deletion
|
||||||
|
delete_db_backups_interactive() {
|
||||||
|
list_db_backups
|
||||||
|
if [[ ${_BACKUP_COUNT:-0} -eq 0 ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${CYAN}Enter backup number(s) to delete (comma-separated), or 'q' to cancel:${RESET} "
|
||||||
|
read -r selection
|
||||||
|
|
||||||
|
if [[ "$selection" == "q" || -z "$selection" ]]; then
|
||||||
|
log_message "Cancelled"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
IFS=',' read -ra nums <<< "$selection"
|
||||||
|
local deleted=0
|
||||||
|
for num in "${nums[@]}"; do
|
||||||
|
num=$(echo "$num" | tr -d ' ')
|
||||||
|
if ! [[ "$num" =~ ^[0-9]+$ ]] || (( num < 1 || num > _BACKUP_COUNT )); then
|
||||||
|
log_error "Invalid selection: $num (skipping)"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
local target="${_BACKUP_LIST[$((num-1))]}"
|
||||||
|
local target_name
|
||||||
|
target_name=$(basename "$target")
|
||||||
|
echo -e "${YELLOW}Delete ${target_name}? [y/N]:${RESET} "
|
||||||
|
read -r confirm
|
||||||
|
if [[ "${confirm,,}" == "y" ]]; then
|
||||||
|
if [[ -d "$target" ]]; then
|
||||||
|
sudo rm -rf "$target"
|
||||||
|
else
|
||||||
|
sudo rm -f "$target"
|
||||||
|
fi
|
||||||
|
log_success "Deleted: $target_name"
|
||||||
|
((deleted++))
|
||||||
|
else
|
||||||
|
log_message "Skipped: $target_name"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo ""
|
||||||
|
log_message "Deleted $deleted backup(s)"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Delete backup by name/pattern
|
||||||
|
delete_db_backup_by_name() {
|
||||||
|
local pattern="$1"
|
||||||
|
local db_dir="$PLEX_DB_DIR"
|
||||||
|
local found=0
|
||||||
|
|
||||||
|
while IFS= read -r -d '' entry; do
|
||||||
|
local name
|
||||||
|
name=$(basename "$entry")
|
||||||
|
echo -e "${YELLOW}Delete ${name}? [y/N]:${RESET} "
|
||||||
|
read -r confirm
|
||||||
|
if [[ "${confirm,,}" == "y" ]]; then
|
||||||
|
if [[ -d "$entry" ]]; then
|
||||||
|
sudo rm -rf "$entry"
|
||||||
|
else
|
||||||
|
sudo rm -f "$entry"
|
||||||
|
fi
|
||||||
|
log_success "Deleted: $name"
|
||||||
|
((found++))
|
||||||
|
fi
|
||||||
|
done < <(sudo find "$db_dir" -maxdepth 1 -name "*${pattern}*" -print0 2>/dev/null)
|
||||||
|
|
||||||
|
if [[ $found -eq 0 ]]; then
|
||||||
|
log_error "No backups matching '${pattern}' found"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
log_message "Deleted $found file(s)"
|
||||||
|
}
|
||||||
|
|
||||||
# Create log directory
|
# Create log directory
|
||||||
mkdir -p "$SCRIPT_DIR/logs"
|
mkdir -p "$SCRIPT_DIR/logs"
|
||||||
|
|
||||||
@@ -298,26 +527,35 @@ check_all_databases() {
|
|||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
log_warning "Found integrity issues in $issues database(s)"
|
log_warning "Found integrity issues in $issues database(s)"
|
||||||
|
_hint_install_dbrepair
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Show help
|
# Show help
|
||||||
show_help() {
|
show_help() {
|
||||||
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}$(basename "$0")${RESET} ${YELLOW}<command>${RESET} ${DIM}[options]${RESET}"
|
local script
|
||||||
|
script=$(basename "$0")
|
||||||
|
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${script}${RESET} ${YELLOW}<command>${RESET} ${DIM}[options]${RESET}"
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${BOLD}${WHITE}Commands:${RESET}"
|
echo -e "${BOLD}${WHITE}Commands:${RESET}"
|
||||||
echo -e " ${GREEN}${BOLD}check${RESET} Read-only database integrity check"
|
printf " ${GREEN}${BOLD}%-18s${RESET} %s\n" "check" "Read-only database integrity check"
|
||||||
echo -e " ${YELLOW}${BOLD}repair${RESET} Interactive database repair"
|
printf " ${YELLOW}${BOLD}%-18s${RESET} %s\n" "repair" "Interactive database repair"
|
||||||
echo -e " ${YELLOW}${BOLD}repair --gentle${RESET} Gentle repair methods only"
|
printf " ${YELLOW}${BOLD}%-18s${RESET} %s\n" "repair --gentle" "Gentle repair methods only"
|
||||||
echo -e " ${RED}${BOLD}repair --force${RESET} Aggressive repair methods"
|
printf " ${RED}${BOLD}%-18s${RESET} %s\n" "repair --force" "Aggressive repair methods"
|
||||||
echo -e " ${RED}${BOLD}nuclear${RESET} Nuclear recovery (replace from backup)"
|
printf " ${RED}${BOLD}%-18s${RESET} %s\n" "nuclear" "Nuclear recovery (replace from backup)"
|
||||||
echo -e " ${CYAN}${BOLD}help${RESET} Show this help message"
|
printf " ${CYAN}${BOLD}%-18s${RESET} %s\n" "backups" "List and manage database backup files"
|
||||||
|
printf " ${GREEN}${BOLD}%-18s${RESET} %s\n" "install-dbrepair" "Install or update DBRepair tool"
|
||||||
|
printf " ${CYAN}${BOLD}%-18s${RESET} %s\n" "help" "Show this help message"
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${BOLD}${WHITE}Examples:${RESET}"
|
echo -e "${BOLD}${WHITE}Examples:${RESET}"
|
||||||
echo -e " ${DIM}$(basename "$0") check # Safe integrity check${RESET}"
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} check" "Safe integrity check"
|
||||||
echo -e " ${DIM}$(basename "$0") repair # Interactive repair${RESET}"
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} repair" "Interactive repair"
|
||||||
echo -e " ${DIM}$(basename "$0") repair --gentle # Minimal repair only${RESET}"
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} repair --gentle" "Minimal repair only"
|
||||||
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} backups" "List DB backups"
|
||||||
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} backups delete" "Interactive backup deletion"
|
||||||
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} backups delete --name foo" "Delete by name pattern"
|
||||||
|
printf " ${DIM}%-46s # %s${RESET}\n" "${script} install-dbrepair" "Install/update DBRepair"
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${BOLD}${YELLOW}⚠️ WARNING:${RESET} Always run ${CYAN}check${RESET} first before attempting repairs!"
|
echo -e "${BOLD}${YELLOW}⚠️ WARNING:${RESET} Always run ${CYAN}check${RESET} first before attempting repairs!"
|
||||||
echo ""
|
echo ""
|
||||||
@@ -375,9 +613,8 @@ main() {
|
|||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
echo -e "${RED}${BOLD}⚠️ DBRepair.sh NOT FOUND${RESET}"
|
echo -e "${RED}${BOLD}⚠️ DBRepair.sh NOT FOUND${RESET}"
|
||||||
echo -e "${YELLOW}Install DBRepair for repair/optimize/reindex/FTS-rebuild:${RESET}"
|
echo -e "${YELLOW}You can install it automatically:${RESET}"
|
||||||
echo -e " ${CYAN}wget -O ${SCRIPT_DIR}/DBRepair.sh https://github.com/ChuckPa/PlexDBRepair/releases/latest/download/DBRepair.sh${RESET}"
|
echo -e " ${CYAN}$(basename "$0") install-dbrepair${RESET}"
|
||||||
echo -e " ${CYAN}chmod +x ${SCRIPT_DIR}/DBRepair.sh${RESET}"
|
|
||||||
echo -e "${YELLOW}Then re-run: $(basename "$0") repair${RESET}"
|
echo -e "${YELLOW}Then re-run: $(basename "$0") repair${RESET}"
|
||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
@@ -433,6 +670,24 @@ main() {
|
|||||||
show_help
|
show_help
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
"install-dbrepair"|"update-dbrepair"|"dbrepair")
|
||||||
|
print_header
|
||||||
|
install_or_update_dbrepair
|
||||||
|
;;
|
||||||
|
|
||||||
|
"backups"|"backup-list")
|
||||||
|
print_header
|
||||||
|
if [[ $# -ge 2 && "${2,,}" == "delete" ]]; then
|
||||||
|
if [[ $# -ge 4 && "${3}" == "--name" ]]; then
|
||||||
|
delete_db_backup_by_name "$4"
|
||||||
|
else
|
||||||
|
delete_db_backups_interactive
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
list_db_backups
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
print_header
|
print_header
|
||||||
log_error "Unknown command: $1"
|
log_error "Unknown command: $1"
|
||||||
|
|||||||
291
plex/plex.sh
291
plex/plex.sh
@@ -78,6 +78,10 @@ SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
|||||||
readonly SCRIPT_DIR
|
readonly SCRIPT_DIR
|
||||||
|
|
||||||
# DBRepair.sh location — searched in order: script dir, database dir, /usr/local/bin
|
# DBRepair.sh location — searched in order: script dir, database dir, /usr/local/bin
|
||||||
|
readonly DBREPAIR_INSTALL_PATH="${SCRIPT_DIR}/DBRepair.sh"
|
||||||
|
readonly DBREPAIR_GITHUB_API="https://api.github.com/repos/ChuckPa/PlexDBRepair/releases"
|
||||||
|
readonly DBREPAIR_DOWNLOAD_BASE="https://github.com/ChuckPa/PlexDBRepair/releases/download"
|
||||||
|
|
||||||
find_dbrepair() {
|
find_dbrepair() {
|
||||||
local candidates=(
|
local candidates=(
|
||||||
"${SCRIPT_DIR}/DBRepair.sh"
|
"${SCRIPT_DIR}/DBRepair.sh"
|
||||||
@@ -93,6 +97,245 @@ find_dbrepair() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get the latest non-beta release tag from GitHub
|
||||||
|
_dbrepair_latest_release_tag() {
|
||||||
|
# Fetch releases, filter out pre-releases & drafts, grab the first tag_name
|
||||||
|
local tag
|
||||||
|
tag=$(curl -fsSL "${DBREPAIR_GITHUB_API}" 2>/dev/null \
|
||||||
|
| grep -Eo '"tag_name"\s*:\s*"[^"]+"' \
|
||||||
|
| head -n 1 \
|
||||||
|
| sed 's/"tag_name"\s*:\s*"//;s/"//')
|
||||||
|
if [[ -z "$tag" ]]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo "$tag"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get currently installed DBRepair version (from its own version output)
|
||||||
|
_dbrepair_installed_version() {
|
||||||
|
local bin="$1"
|
||||||
|
# DBRepair prints "Version vX.YY.ZZ" near the top when run interactively;
|
||||||
|
# we can also grep the script file itself for the version string.
|
||||||
|
grep -oP 'Version\s+v\K[0-9.]+' "$bin" 2>/dev/null | head -n 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Install or update DBRepair.sh
|
||||||
|
install_or_update_dbrepair() {
|
||||||
|
print_status "${INFO}" "Checking DBRepair (ChuckPa/PlexDBRepair)..." "${BLUE}"
|
||||||
|
|
||||||
|
# Determine latest non-beta release
|
||||||
|
local latest_tag
|
||||||
|
if ! latest_tag=$(_dbrepair_latest_release_tag); then
|
||||||
|
print_status "${CROSS}" "Failed to query GitHub for the latest release" "${RED}"
|
||||||
|
print_status "${INFO}" "Check your internet connection or try: https://github.com/ChuckPa/PlexDBRepair/releases" "${YELLOW}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
print_status "${INFO}" "Latest stable release: ${latest_tag}" "${BLUE}"
|
||||||
|
|
||||||
|
# Check if already installed
|
||||||
|
local dbrepair_bin
|
||||||
|
if dbrepair_bin=$(find_dbrepair); then
|
||||||
|
local installed_ver
|
||||||
|
installed_ver=$(_dbrepair_installed_version "$dbrepair_bin")
|
||||||
|
local remote_ver
|
||||||
|
remote_ver=$(echo "$latest_tag" | sed 's/^v//')
|
||||||
|
|
||||||
|
if [[ -n "$installed_ver" ]]; then
|
||||||
|
print_status "${INFO}" "Installed version: v${installed_ver} at ${dbrepair_bin}" "${BLUE}"
|
||||||
|
if [[ "$installed_ver" == "$remote_ver" ]]; then
|
||||||
|
print_status "${CHECKMARK}" "DBRepair is already up to date (v${installed_ver})" "${GREEN}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_status "${INFO}" "Update available: v${installed_ver} -> ${latest_tag}" "${YELLOW}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_status "${INFO}" "Installed at ${dbrepair_bin} (version unknown), will update" "${YELLOW}"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_status "${INFO}" "DBRepair not found — installing to ${DBREPAIR_INSTALL_PATH}" "${BLUE}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Download
|
||||||
|
local download_url="${DBREPAIR_DOWNLOAD_BASE}/${latest_tag}/DBRepair.sh"
|
||||||
|
print_status "${INFO}" "Downloading ${download_url}" "${BLUE}"
|
||||||
|
if curl -fsSL -o "${DBREPAIR_INSTALL_PATH}" "$download_url"; then
|
||||||
|
chmod +x "${DBREPAIR_INSTALL_PATH}"
|
||||||
|
print_status "${CHECKMARK}" "DBRepair ${latest_tag} installed to ${DBREPAIR_INSTALL_PATH}" "${GREEN}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_status "${CROSS}" "Download failed" "${RED}"
|
||||||
|
rm -f "${DBREPAIR_INSTALL_PATH}" 2>/dev/null
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Suggest installing DBRepair when errors are found and it's not available
|
||||||
|
_hint_install_dbrepair() {
|
||||||
|
if ! find_dbrepair >/dev/null 2>&1; then
|
||||||
|
echo ""
|
||||||
|
print_status "${INFO}" "DBRepair is NOT installed. It can fix most database issues automatically." "${YELLOW}"
|
||||||
|
echo -e "${DIM}${CYAN} Install it now: ${SCRIPT_NAME} install-dbrepair${RESET}"
|
||||||
|
echo -e "${DIM}${CYAN} Then repair: ${SCRIPT_NAME} repair${RESET}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📦 List and manage database backup files
|
||||||
|
# Covers: DBRepair backups (-BACKUP-*, -BKUP-*), script backups (*.backup.*),
|
||||||
|
# corrupted dirs (corrupted-*), recovery files (*recovery*)
|
||||||
|
list_db_backups() {
|
||||||
|
local db_dir="$PLEX_DB_DIR"
|
||||||
|
local -a backup_files=()
|
||||||
|
local -a backup_paths=()
|
||||||
|
|
||||||
|
# Collect all backup-like files and dirs
|
||||||
|
while IFS= read -r -d '' entry; do
|
||||||
|
backup_paths+=("$entry")
|
||||||
|
done < <(sudo find "$db_dir" -maxdepth 1 \( \
|
||||||
|
-name '*-BACKUP-*' -o \
|
||||||
|
-name '*-BKUP-*' -o \
|
||||||
|
-name '*.backup.*' -o \
|
||||||
|
-name '*recovery*' -o \
|
||||||
|
-name 'corrupted-*' -o \
|
||||||
|
-name '*-BLOATED-*' \
|
||||||
|
\) -print0 2>/dev/null | sort -z)
|
||||||
|
|
||||||
|
if [[ ${#backup_paths[@]} -eq 0 ]]; then
|
||||||
|
print_status "${INFO}" "No database backup files found in the Plex database directory" "${YELLOW}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\n${BOLD}${WHITE} # Type Size Created Name${RESET}"
|
||||||
|
echo -e "${DIM}${CYAN} --- ------------- ----------- ------------------------ ------------------------------------${RESET}"
|
||||||
|
|
||||||
|
local idx=0
|
||||||
|
for entry in "${backup_paths[@]}"; do
|
||||||
|
((idx++))
|
||||||
|
local name
|
||||||
|
name=$(basename "$entry")
|
||||||
|
|
||||||
|
# Determine type label
|
||||||
|
local type_label
|
||||||
|
if [[ "$name" == *-BACKUP-* || "$name" == *-BKUP-* ]]; then
|
||||||
|
type_label="DBRepair"
|
||||||
|
elif [[ "$name" == *-BLOATED-* ]]; then
|
||||||
|
type_label="Bloated"
|
||||||
|
elif [[ "$name" == *.backup.* ]]; then
|
||||||
|
type_label="Script"
|
||||||
|
elif [[ "$name" == corrupted-* ]]; then
|
||||||
|
type_label="Corrupted"
|
||||||
|
elif [[ "$name" == *recovery* ]]; then
|
||||||
|
type_label="Recovery"
|
||||||
|
else
|
||||||
|
type_label="Other"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Size (human-readable)
|
||||||
|
local size
|
||||||
|
if [[ -d "$entry" ]]; then
|
||||||
|
size=$(sudo du -sh "$entry" 2>/dev/null | cut -f1)
|
||||||
|
type_label="${type_label}/dir"
|
||||||
|
else
|
||||||
|
size=$(sudo stat --printf='%s' "$entry" 2>/dev/null)
|
||||||
|
if [[ -n "$size" ]]; then
|
||||||
|
size=$(numfmt --to=iec-i --suffix=B "$size" 2>/dev/null || echo "${size}B")
|
||||||
|
else
|
||||||
|
size="?"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Created date
|
||||||
|
local created
|
||||||
|
created=$(sudo stat --printf='%y' "$entry" 2>/dev/null | cut -d. -f1)
|
||||||
|
[[ -z "$created" ]] && created="unknown"
|
||||||
|
|
||||||
|
printf " ${WHITE}%-3s${RESET} ${CYAN}%-13s${RESET} ${YELLOW}%-11s${RESET} ${DIM}%-24s${RESET} %s\n" \
|
||||||
|
"$idx" "$type_label" "$size" "$created" "$name"
|
||||||
|
|
||||||
|
backup_files+=("$entry")
|
||||||
|
done
|
||||||
|
|
||||||
|
echo -e "${DIM}${CYAN} --- ------------- ----------- ------------------------ ------------------------------------${RESET}"
|
||||||
|
echo -e " ${DIM}Total: ${idx} backup file(s)${RESET}\n"
|
||||||
|
|
||||||
|
# Store for use by delete function
|
||||||
|
_BACKUP_LIST=("${backup_files[@]}")
|
||||||
|
_BACKUP_COUNT=$idx
|
||||||
|
}
|
||||||
|
|
||||||
|
# Interactive backup deletion
|
||||||
|
delete_db_backups_interactive() {
|
||||||
|
list_db_backups
|
||||||
|
if [[ ${_BACKUP_COUNT:-0} -eq 0 ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${CYAN}Enter backup number(s) to delete (comma-separated), or 'q' to cancel:${RESET} "
|
||||||
|
read -r selection
|
||||||
|
|
||||||
|
if [[ "$selection" == "q" || -z "$selection" ]]; then
|
||||||
|
print_status "${INFO}" "Cancelled" "${YELLOW}"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse comma-separated numbers
|
||||||
|
IFS=',' read -ra nums <<< "$selection"
|
||||||
|
local deleted=0
|
||||||
|
for num in "${nums[@]}"; do
|
||||||
|
num=$(echo "$num" | tr -d ' ')
|
||||||
|
if ! [[ "$num" =~ ^[0-9]+$ ]] || (( num < 1 || num > _BACKUP_COUNT )); then
|
||||||
|
print_status "${CROSS}" "Invalid selection: $num (skipping)" "${RED}"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
local target="${_BACKUP_LIST[$((num-1))]}"
|
||||||
|
local target_name
|
||||||
|
target_name=$(basename "$target")
|
||||||
|
echo -e "${YELLOW}Delete ${target_name}? [y/N]:${RESET} "
|
||||||
|
read -r confirm
|
||||||
|
if [[ "${confirm,,}" == "y" ]]; then
|
||||||
|
if [[ -d "$target" ]]; then
|
||||||
|
sudo rm -rf "$target"
|
||||||
|
else
|
||||||
|
sudo rm -f "$target"
|
||||||
|
fi
|
||||||
|
print_status "${CHECKMARK}" "Deleted: $target_name" "${GREEN}"
|
||||||
|
((deleted++))
|
||||||
|
else
|
||||||
|
print_status "${INFO}" "Skipped: $target_name" "${YELLOW}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo ""
|
||||||
|
print_status "${INFO}" "Deleted $deleted backup(s)" "${BLUE}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Delete backup by name/pattern (for scripted use)
|
||||||
|
delete_db_backup_by_name() {
|
||||||
|
local pattern="$1"
|
||||||
|
local db_dir="$PLEX_DB_DIR"
|
||||||
|
local found=0
|
||||||
|
|
||||||
|
while IFS= read -r -d '' entry; do
|
||||||
|
local name
|
||||||
|
name=$(basename "$entry")
|
||||||
|
echo -e "${YELLOW}Delete ${name}? [y/N]:${RESET} "
|
||||||
|
read -r confirm
|
||||||
|
if [[ "${confirm,,}" == "y" ]]; then
|
||||||
|
if [[ -d "$entry" ]]; then
|
||||||
|
sudo rm -rf "$entry"
|
||||||
|
else
|
||||||
|
sudo rm -f "$entry"
|
||||||
|
fi
|
||||||
|
print_status "${CHECKMARK}" "Deleted: $name" "${GREEN}"
|
||||||
|
((found++))
|
||||||
|
fi
|
||||||
|
done < <(sudo find "$db_dir" -maxdepth 1 -name "*${pattern}*" -print0 2>/dev/null)
|
||||||
|
|
||||||
|
if [[ $found -eq 0 ]]; then
|
||||||
|
print_status "${CROSS}" "No backups matching '${pattern}' found" "${RED}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
print_status "${INFO}" "Deleted $found file(s)" "${BLUE}"
|
||||||
|
}
|
||||||
|
|
||||||
# 🎭 ASCII symbols for compatible output
|
# 🎭 ASCII symbols for compatible output
|
||||||
readonly CHECKMARK="[✓]"
|
readonly CHECKMARK="[✓]"
|
||||||
readonly CROSS="[✗]"
|
readonly CROSS="[✗]"
|
||||||
@@ -353,6 +596,7 @@ check_database_integrity() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
print_status "${INFO}" "Consider running database repair: ${SCRIPT_NAME} repair" "${YELLOW}"
|
print_status "${INFO}" "Consider running database repair: ${SCRIPT_NAME} repair" "${YELLOW}"
|
||||||
|
_hint_install_dbrepair
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,6 +618,7 @@ start_plex() {
|
|||||||
if ! check_database_integrity; then
|
if ! check_database_integrity; then
|
||||||
print_status "${INFO}" "Database integrity issues detected — starting Plex anyway (it may self-repair)." "${YELLOW}"
|
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}"
|
echo -e "${DIM}${YELLOW} If Plex fails to start, run: ${SCRIPT_NAME} repair${RESET}"
|
||||||
|
_hint_install_dbrepair
|
||||||
fi
|
fi
|
||||||
|
|
||||||
print_status "${INFO}" "Attempting to start service..." "${BLUE}"
|
print_status "${INFO}" "Attempting to start service..." "${BLUE}"
|
||||||
@@ -530,21 +775,27 @@ show_help() {
|
|||||||
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
|
echo -e "${BOLD}${WHITE}Usage:${RESET} ${CYAN}${SCRIPT_NAME}${RESET} ${YELLOW}<command>${RESET}"
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
|
echo -e "${BOLD}${WHITE}Available Commands:${RESET}"
|
||||||
echo -e " ${GREEN}${BOLD}start${RESET} ${ROCKET} Start Plex Media Server"
|
printf " ${GREEN}${BOLD}%-18s${RESET} %s %s\n" "start" "${ROCKET}" "Start Plex Media Server"
|
||||||
echo -e " ${YELLOW}${BOLD}stop${RESET} ${STOP_SIGN} Stop Plex Media Server"
|
printf " ${YELLOW}${BOLD}%-18s${RESET} %s %s\n" "stop" "${STOP_SIGN}" "Stop Plex Media Server"
|
||||||
echo -e " ${BLUE}${BOLD}restart${RESET} ${RECYCLE} Restart Plex Media Server"
|
printf " ${BLUE}${BOLD}%-18s${RESET} %s %s\n" "restart" "${RECYCLE}" "Restart Plex Media Server"
|
||||||
echo -e " ${CYAN}${BOLD}status${RESET} ${INFO} Show detailed service status"
|
printf " ${CYAN}${BOLD}%-18s${RESET} %s %s\n" "status" "${INFO}" "Show detailed service status"
|
||||||
echo -e " ${PURPLE}${BOLD}scan${RESET} ${SPARKLES} Library scanner operations"
|
printf " ${PURPLE}${BOLD}%-18s${RESET} %s %s\n" "scan" "${SPARKLES}" "Library scanner operations"
|
||||||
echo -e " ${RED}${BOLD}repair${RESET} [!] Repair database corruption issues"
|
printf " ${RED}${BOLD}%-18s${RESET} %s %s\n" "repair" "[!]" "Repair database corruption issues"
|
||||||
echo -e " ${RED}${BOLD}nuclear${RESET} [!!] Nuclear database recovery (last resort)"
|
printf " ${RED}${BOLD}%-18s${RESET} %s %s\n" "nuclear" "[!!]" "Nuclear database recovery (last resort)"
|
||||||
echo -e " ${PURPLE}${BOLD}help${RESET} [*] Show this help message"
|
printf " ${CYAN}${BOLD}%-18s${RESET} %s %s\n" "backups" "[#]" "List and manage database backup files"
|
||||||
|
printf " ${GREEN}${BOLD}%-18s${RESET} %s %s\n" "install-dbrepair" "[+]" "Install or update DBRepair tool"
|
||||||
|
printf " ${PURPLE}${BOLD}%-18s${RESET} %s %s\n" "help" "${HOURGLASS}" "Show this help message"
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${DIM}${WHITE}Examples:${RESET}"
|
echo -e "${DIM}${WHITE}Examples:${RESET}"
|
||||||
echo -e " ${DIM}${SCRIPT_NAME} start # Start the Plex service${RESET}"
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} start" "Start the Plex service"
|
||||||
echo -e " ${DIM}${SCRIPT_NAME} status # Show current status${RESET}"
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} status" "Show current status"
|
||||||
echo -e " ${DIM}${SCRIPT_NAME} scan # Launch library scanner interface${RESET}"
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} scan" "Launch library scanner interface"
|
||||||
echo -e " ${DIM}${SCRIPT_NAME} repair # Fix database issues${RESET}"
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} repair" "Fix database issues"
|
||||||
echo -e " ${DIM}${SCRIPT_NAME} nuclear # Complete database replacement${RESET}"
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} nuclear" "Complete database replacement"
|
||||||
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} backups" "List and manage DB backups"
|
||||||
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} backups delete" "Interactive backup deletion"
|
||||||
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} backups delete --name foo" "Delete by name pattern"
|
||||||
|
printf " ${DIM}%-40s # %s${RESET}\n" "${SCRIPT_NAME} install-dbrepair" "Install/update DBRepair"
|
||||||
echo ""
|
echo ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -712,6 +963,20 @@ main() {
|
|||||||
"nuclear"|"nuke")
|
"nuclear"|"nuke")
|
||||||
nuclear_recovery
|
nuclear_recovery
|
||||||
;;
|
;;
|
||||||
|
"install-dbrepair"|"update-dbrepair"|"dbrepair")
|
||||||
|
install_or_update_dbrepair
|
||||||
|
;;
|
||||||
|
"backups"|"backup-list")
|
||||||
|
if [[ $# -ge 2 && "${2,,}" == "delete" ]]; then
|
||||||
|
if [[ $# -ge 4 && "${3}" == "--name" ]]; then
|
||||||
|
delete_db_backup_by_name "$4"
|
||||||
|
else
|
||||||
|
delete_db_backups_interactive
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
list_db_backups
|
||||||
|
fi
|
||||||
|
;;
|
||||||
"help"|"--help"|"-h")
|
"help"|"--help"|"-h")
|
||||||
show_help
|
show_help
|
||||||
;;
|
;;
|
||||||
|
|||||||
@@ -57,6 +57,8 @@ readonly RESET='\033[0m'
|
|||||||
|
|
||||||
# 🔧 Configuration
|
# 🔧 Configuration
|
||||||
readonly PLEX_SERVICE="plexmediaserver"
|
readonly PLEX_SERVICE="plexmediaserver"
|
||||||
|
readonly PLEX_PREFS="/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml"
|
||||||
|
readonly PLEX_API_BASE="http://localhost:32400"
|
||||||
readonly SCRIPT_NAME="$(basename "$0")"
|
readonly SCRIPT_NAME="$(basename "$0")"
|
||||||
readonly SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
readonly SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
|
||||||
readonly LOG_DIR="${SCRIPT_DIR}/../logs"
|
readonly LOG_DIR="${SCRIPT_DIR}/../logs"
|
||||||
@@ -144,6 +146,126 @@ find_scanner() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# 🚀 Run Plex Media Scanner as the plex user with correct environment
|
||||||
|
# Usage: run_scanner [args...] — runs and returns exit code
|
||||||
|
run_scanner() {
|
||||||
|
sudo -u plex \
|
||||||
|
env LD_LIBRARY_PATH=/usr/lib/plexmediaserver \
|
||||||
|
PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="/var/lib/plexmediaserver/Library/Application Support" \
|
||||||
|
"$SCANNER_PATH" "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 🔑 Get Plex authentication token from Preferences.xml
|
||||||
|
get_plex_token() {
|
||||||
|
local token
|
||||||
|
token=$(sudo grep -oP 'PlexOnlineToken="\K[^"]+' "$PLEX_PREFS" 2>/dev/null)
|
||||||
|
if [[ -z "$token" ]]; then
|
||||||
|
log_verbose "Could not read Plex token from Preferences.xml"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
echo "$token"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📡 Trigger a library scan via the Plex API (replaces deprecated --scan)
|
||||||
|
# Usage: api_scan_section <section_id>
|
||||||
|
api_scan_section() {
|
||||||
|
local section_id="$1"
|
||||||
|
local token
|
||||||
|
if ! token=$(get_plex_token); then
|
||||||
|
log_verbose "Cannot scan: no Plex token available"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local http_code
|
||||||
|
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
|
||||||
|
-X GET "${PLEX_API_BASE}/library/sections/${section_id}/refresh?X-Plex-Token=${token}")
|
||||||
|
|
||||||
|
if [[ "$http_code" =~ ^2 ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log_verbose "API scan failed for section $section_id (HTTP $http_code)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📡 Trigger a metadata refresh via the Plex API (replaces deprecated --refresh)
|
||||||
|
# Usage: api_refresh_section <section_id> [force]
|
||||||
|
api_refresh_section() {
|
||||||
|
local section_id="$1"
|
||||||
|
local force="${2:-false}"
|
||||||
|
local token
|
||||||
|
if ! token=$(get_plex_token); then
|
||||||
|
log_verbose "Cannot refresh: no Plex token available"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local url="${PLEX_API_BASE}/library/sections/${section_id}/refresh?X-Plex-Token=${token}"
|
||||||
|
if [[ "$force" == "true" ]]; then
|
||||||
|
url+="&force=1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local http_code
|
||||||
|
http_code=$(curl -s -o /dev/null -w "%{http_code}" -X GET "$url")
|
||||||
|
|
||||||
|
if [[ "$http_code" =~ ^2 ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log_verbose "API refresh failed for section $section_id (HTTP $http_code)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📡 Trigger media analysis via the Plex API (replaces deprecated --analyze)
|
||||||
|
# Usage: api_analyze_section <section_id>
|
||||||
|
api_analyze_section() {
|
||||||
|
local section_id="$1"
|
||||||
|
local token
|
||||||
|
if ! token=$(get_plex_token); then
|
||||||
|
log_verbose "Cannot analyze: no Plex token available"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local http_code
|
||||||
|
http_code=$(curl -s -o /dev/null -w "%{http_code}" \
|
||||||
|
-X PUT "${PLEX_API_BASE}/library/sections/${section_id}/analyze?X-Plex-Token=${token}")
|
||||||
|
|
||||||
|
if [[ "$http_code" =~ ^2 ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log_verbose "API analyze failed for section $section_id (HTTP $http_code)"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📡 List library sections via the Plex API
|
||||||
|
# Output format: "key|title|type" per line (e.g. "1|Movies|movie")
|
||||||
|
api_list_sections() {
|
||||||
|
local token
|
||||||
|
if ! token=$(get_plex_token); then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
local xml
|
||||||
|
if ! xml=$(curl -fsS "${PLEX_API_BASE}/library/sections?X-Plex-Token=${token}" 2>/dev/null); then
|
||||||
|
log_verbose "Plex API request failed"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse XML: extract key, title, and type from <Directory> elements
|
||||||
|
echo "$xml" | grep -oP '<Directory[^>]*>' | while IFS= read -r tag; do
|
||||||
|
local key title type
|
||||||
|
key=$(echo "$tag" | grep -oP 'key="\K[^"]+')
|
||||||
|
title=$(echo "$tag" | grep -oP 'title="\K[^"]+')
|
||||||
|
type=$(echo "$tag" | grep -oP 'type="\K[^"]+')
|
||||||
|
echo "${key}|${title}|${type}"
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
# 📋 Get just the section IDs from the API (one per line)
|
||||||
|
api_list_section_ids() {
|
||||||
|
api_list_sections | cut -d'|' -f1
|
||||||
|
}
|
||||||
|
|
||||||
# 🏥 Function to check Plex service status
|
# 🏥 Function to check Plex service status
|
||||||
check_plex_service() {
|
check_plex_service() {
|
||||||
log_verbose "Checking Plex service status..."
|
log_verbose "Checking Plex service status..."
|
||||||
@@ -166,38 +288,24 @@ list_libraries() {
|
|||||||
return 2
|
return 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! find_scanner; then
|
local sections
|
||||||
return 3
|
if ! sections=$(api_list_sections) || [[ -z "$sections" ]]; then
|
||||||
|
print_status "${CROSS}" "Failed to retrieve library sections from Plex API" "${RED}"
|
||||||
|
return 5
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Set library path for Linux
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
|
|
||||||
local output
|
|
||||||
if output=$("$SCANNER_PATH" --list 2>&1); then
|
|
||||||
echo ""
|
echo ""
|
||||||
echo -e "${BOLD}${CYAN}Available Library Sections:${RESET}"
|
echo -e "${BOLD}${CYAN}Available Library Sections:${RESET}"
|
||||||
echo -e "${DIM}${CYAN}=========================${RESET}"
|
echo -e "${DIM}${CYAN}=========================${RESET}"
|
||||||
|
|
||||||
# Parse and format the output
|
while IFS='|' read -r key title type; do
|
||||||
echo "$output" | while IFS= read -r line; do
|
[[ -n "$key" ]] || continue
|
||||||
if [[ "$line" =~ ^[[:space:]]*([0-9]+):[[:space:]]*(.+)$ ]]; then
|
printf " ${GREEN}${BOLD}%-4s${RESET} ${WHITE}%-30s${RESET} ${DIM}(%s)${RESET}\n" "${key}:" "$title" "$type"
|
||||||
local section_id="${BASH_REMATCH[1]}"
|
done <<< "$sections"
|
||||||
local section_name="${BASH_REMATCH[2]}"
|
|
||||||
echo -e "${GREEN}${BOLD} ${section_id}:${RESET} ${WHITE}${section_name}${RESET}"
|
|
||||||
elif [[ -n "$line" ]]; then
|
|
||||||
echo -e "${DIM} $line${RESET}"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
print_status "${CHECKMARK}" "Library listing completed" "${GREEN}"
|
print_status "${CHECKMARK}" "Library listing completed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
|
||||||
print_status "${CROSS}" "Failed to list libraries" "${RED}"
|
|
||||||
echo -e "${DIM}${RED}Error output: $output${RESET}"
|
|
||||||
return 5
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 🔍 Function to validate section ID
|
# 🔍 Function to validate section ID
|
||||||
@@ -209,10 +317,9 @@ validate_section_id() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get list of valid section IDs
|
# Get list of valid section IDs via API
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
local valid_ids
|
local valid_ids
|
||||||
if valid_ids=$("$SCANNER_PATH" --list 2>/dev/null | grep -oE '^[[:space:]]*[0-9]+:' | grep -oE '[0-9]+'); then
|
if valid_ids=$(api_list_section_ids) && [[ -n "$valid_ids" ]]; then
|
||||||
if echo "$valid_ids" | grep -q "^${section_id}$"; then
|
if echo "$valid_ids" | grep -q "^${section_id}$"; then
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -237,9 +344,7 @@ scan_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
if api_scan_section "$section_id"; then
|
||||||
|
|
||||||
if "$SCANNER_PATH" --scan --section "$section_id" ${VERBOSE:+--verbose}; then
|
|
||||||
print_status "${CHECKMARK}" "Library section $section_id scan completed" "${GREEN}"
|
print_status "${CHECKMARK}" "Library section $section_id scan completed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -250,16 +355,15 @@ scan_library() {
|
|||||||
print_status "${ROCKET}" "Scanning all libraries for new media..." "${BLUE}"
|
print_status "${ROCKET}" "Scanning all libraries for new media..." "${BLUE}"
|
||||||
|
|
||||||
# Get all section IDs and scan each one
|
# Get all section IDs and scan each one
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
local section_ids
|
local section_ids
|
||||||
if section_ids=$("$SCANNER_PATH" --list 2>/dev/null | grep -oE '^[[:space:]]*[0-9]+:' | grep -oE '[0-9]+'); then
|
if section_ids=$(api_list_section_ids) && [[ -n "$section_ids" ]]; then
|
||||||
local failed_sections=()
|
local failed_sections=()
|
||||||
|
|
||||||
while IFS= read -r id; do
|
while IFS= read -r id; do
|
||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Scanning section $id..." "${YELLOW}"
|
print_status "${INFO}" "Scanning section $id..." "${YELLOW}"
|
||||||
|
|
||||||
if "$SCANNER_PATH" --scan --section "$id" ${VERBOSE:+--verbose}; then
|
if api_scan_section "$id"; then
|
||||||
print_status "${CHECKMARK}" "Section $id scanned successfully" "${GREEN}"
|
print_status "${CHECKMARK}" "Section $id scanned successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to scan section $id" "${RED}"
|
print_status "${CROSS}" "Failed to scan section $id" "${RED}"
|
||||||
@@ -286,11 +390,6 @@ refresh_library() {
|
|||||||
local section_id="$1"
|
local section_id="$1"
|
||||||
local force="${2:-false}"
|
local force="${2:-false}"
|
||||||
|
|
||||||
local force_flag=""
|
|
||||||
if [[ "$force" == "true" ]]; then
|
|
||||||
force_flag="--force"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$section_id" ]]; then
|
if [[ -n "$section_id" ]]; then
|
||||||
print_status "${RECYCLE}" "Refreshing metadata for library section $section_id..." "${BLUE}"
|
print_status "${RECYCLE}" "Refreshing metadata for library section $section_id..." "${BLUE}"
|
||||||
|
|
||||||
@@ -298,9 +397,7 @@ refresh_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
if api_refresh_section "$section_id" "$force"; then
|
||||||
|
|
||||||
if "$SCANNER_PATH" --refresh $force_flag --section "$section_id" ${VERBOSE:+--verbose}; then
|
|
||||||
print_status "${CHECKMARK}" "Library section $section_id metadata refreshed" "${GREEN}"
|
print_status "${CHECKMARK}" "Library section $section_id metadata refreshed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -311,16 +408,15 @@ refresh_library() {
|
|||||||
print_status "${RECYCLE}" "Refreshing metadata for all libraries..." "${BLUE}"
|
print_status "${RECYCLE}" "Refreshing metadata for all libraries..." "${BLUE}"
|
||||||
|
|
||||||
# Get all section IDs and refresh each one
|
# Get all section IDs and refresh each one
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
local section_ids
|
local section_ids
|
||||||
if section_ids=$("$SCANNER_PATH" --list 2>/dev/null | grep -oE '^[[:space:]]*[0-9]+:' | grep -oE '[0-9]+'); then
|
if section_ids=$(api_list_section_ids) && [[ -n "$section_ids" ]]; then
|
||||||
local failed_sections=()
|
local failed_sections=()
|
||||||
|
|
||||||
while IFS= read -r id; do
|
while IFS= read -r id; do
|
||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Refreshing section $id..." "${YELLOW}"
|
print_status "${INFO}" "Refreshing section $id..." "${YELLOW}"
|
||||||
|
|
||||||
if "$SCANNER_PATH" --refresh $force_flag --section "$id" ${VERBOSE:+--verbose}; then
|
if api_refresh_section "$id" "$force"; then
|
||||||
print_status "${CHECKMARK}" "Section $id refreshed successfully" "${GREEN}"
|
print_status "${CHECKMARK}" "Section $id refreshed successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to refresh section $id" "${RED}"
|
print_status "${CROSS}" "Failed to refresh section $id" "${RED}"
|
||||||
@@ -347,11 +443,6 @@ analyze_library() {
|
|||||||
local section_id="$1"
|
local section_id="$1"
|
||||||
local deep="${2:-false}"
|
local deep="${2:-false}"
|
||||||
|
|
||||||
local analyze_flag="--analyze"
|
|
||||||
if [[ "$deep" == "true" ]]; then
|
|
||||||
analyze_flag="--analyze-deeply"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -n "$section_id" ]]; then
|
if [[ -n "$section_id" ]]; then
|
||||||
print_status "${SEARCH}" "Analyzing media in library section $section_id..." "${BLUE}"
|
print_status "${SEARCH}" "Analyzing media in library section $section_id..." "${BLUE}"
|
||||||
|
|
||||||
@@ -359,9 +450,7 @@ analyze_library() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
if api_analyze_section "$section_id"; then
|
||||||
|
|
||||||
if "$SCANNER_PATH" $analyze_flag --section "$section_id" ${VERBOSE:+--verbose}; then
|
|
||||||
print_status "${CHECKMARK}" "Library section $section_id analysis completed" "${GREEN}"
|
print_status "${CHECKMARK}" "Library section $section_id analysis completed" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -372,16 +461,15 @@ analyze_library() {
|
|||||||
print_status "${SEARCH}" "Analyzing media in all libraries..." "${BLUE}"
|
print_status "${SEARCH}" "Analyzing media in all libraries..." "${BLUE}"
|
||||||
|
|
||||||
# Get all section IDs and analyze each one
|
# Get all section IDs and analyze each one
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
local section_ids
|
local section_ids
|
||||||
if section_ids=$("$SCANNER_PATH" --list 2>/dev/null | grep -oE '^[[:space:]]*[0-9]+:' | grep -oE '[0-9]+'); then
|
if section_ids=$(api_list_section_ids) && [[ -n "$section_ids" ]]; then
|
||||||
local failed_sections=()
|
local failed_sections=()
|
||||||
|
|
||||||
while IFS= read -r id; do
|
while IFS= read -r id; do
|
||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Analyzing section $id..." "${YELLOW}"
|
print_status "${INFO}" "Analyzing section $id..." "${YELLOW}"
|
||||||
|
|
||||||
if "$SCANNER_PATH" $analyze_flag --section "$id" ${VERBOSE:+--verbose}; then
|
if api_analyze_section "$id"; then
|
||||||
print_status "${CHECKMARK}" "Section $id analyzed successfully" "${GREEN}"
|
print_status "${CHECKMARK}" "Section $id analyzed successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to analyze section $id" "${RED}"
|
print_status "${CROSS}" "Failed to analyze section $id" "${RED}"
|
||||||
@@ -414,9 +502,7 @@ generate_thumbnails() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
if run_scanner --generate --section "$section_id" ${VERBOSE:+--verbose}; then
|
||||||
|
|
||||||
if "$SCANNER_PATH" --generate --section "$section_id" ${VERBOSE:+--verbose}; then
|
|
||||||
print_status "${CHECKMARK}" "Thumbnails generated for library section $section_id" "${GREEN}"
|
print_status "${CHECKMARK}" "Thumbnails generated for library section $section_id" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -427,16 +513,15 @@ generate_thumbnails() {
|
|||||||
print_status "${SPARKLES}" "Generating thumbnails for all libraries..." "${BLUE}"
|
print_status "${SPARKLES}" "Generating thumbnails for all libraries..." "${BLUE}"
|
||||||
|
|
||||||
# Get all section IDs and generate thumbnails for each one
|
# Get all section IDs and generate thumbnails for each one
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
|
||||||
local section_ids
|
local section_ids
|
||||||
if section_ids=$("$SCANNER_PATH" --list 2>/dev/null | grep -oE '^[[:space:]]*[0-9]+:' | grep -oE '[0-9]+'); then
|
if section_ids=$(api_list_section_ids) && [[ -n "$section_ids" ]]; then
|
||||||
local failed_sections=()
|
local failed_sections=()
|
||||||
|
|
||||||
while IFS= read -r id; do
|
while IFS= read -r id; do
|
||||||
[[ -n "$id" ]] || continue
|
[[ -n "$id" ]] || continue
|
||||||
print_status "${INFO}" "Generating thumbnails for section $id..." "${YELLOW}"
|
print_status "${INFO}" "Generating thumbnails for section $id..." "${YELLOW}"
|
||||||
|
|
||||||
if "$SCANNER_PATH" --generate --section "$id" ${VERBOSE:+--verbose}; then
|
if run_scanner --generate --section "$id" ${VERBOSE:+--verbose}; then
|
||||||
print_status "${CHECKMARK}" "Section $id thumbnails generated successfully" "${GREEN}"
|
print_status "${CHECKMARK}" "Section $id thumbnails generated successfully" "${GREEN}"
|
||||||
else
|
else
|
||||||
print_status "${CROSS}" "Failed to generate thumbnails for section $id" "${RED}"
|
print_status "${CROSS}" "Failed to generate thumbnails for section $id" "${RED}"
|
||||||
@@ -473,9 +558,7 @@ show_library_tree() {
|
|||||||
return 4
|
return 4
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export LD_LIBRARY_PATH=/usr/lib/plexmediaserver:${LD_LIBRARY_PATH:-}
|
if run_scanner --tree --section "$section_id"; then
|
||||||
|
|
||||||
if "$SCANNER_PATH" --tree --section "$section_id"; then
|
|
||||||
print_status "${CHECKMARK}" "Tree display completed for library section $section_id" "${GREEN}"
|
print_status "${CHECKMARK}" "Tree display completed for library section $section_id" "${GREEN}"
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
@@ -531,15 +614,11 @@ interactive_mode() {
|
|||||||
echo -e "${DIM}Select an operation to perform:${RESET}"
|
echo -e "${DIM}Select an operation to perform:${RESET}"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# First, check if Plex is running and scanner is available
|
# First, check if Plex is running
|
||||||
if ! check_plex_service; then
|
if ! check_plex_service; then
|
||||||
return 2
|
return 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! find_scanner; then
|
|
||||||
return 3
|
|
||||||
fi
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
echo -e "${BOLD}Available Operations:${RESET}"
|
echo -e "${BOLD}Available Operations:${RESET}"
|
||||||
echo -e "${GREEN}1)${RESET} List all libraries"
|
echo -e "${GREEN}1)${RESET} List all libraries"
|
||||||
@@ -638,6 +717,9 @@ interactive_mode() {
|
|||||||
;;
|
;;
|
||||||
5)
|
5)
|
||||||
echo ""
|
echo ""
|
||||||
|
if ! find_scanner; then
|
||||||
|
print_status "${CROSS}" "Scanner binary required for thumbnail generation" "${RED}"
|
||||||
|
else
|
||||||
echo -e "${BOLD}Thumbnail Generation Options:${RESET}"
|
echo -e "${BOLD}Thumbnail Generation Options:${RESET}"
|
||||||
echo -e "${GREEN}1)${RESET} Generate for all libraries"
|
echo -e "${GREEN}1)${RESET} Generate for all libraries"
|
||||||
echo -e "${GREEN}2)${RESET} Generate for specific library"
|
echo -e "${GREEN}2)${RESET} Generate for specific library"
|
||||||
@@ -655,11 +737,16 @@ interactive_mode() {
|
|||||||
print_status "${CROSS}" "Invalid choice" "${RED}"
|
print_status "${CROSS}" "Invalid choice" "${RED}"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
6)
|
6)
|
||||||
echo ""
|
echo ""
|
||||||
|
if ! find_scanner; then
|
||||||
|
print_status "${CROSS}" "Scanner binary required for tree display" "${RED}"
|
||||||
|
else
|
||||||
read -p "$(echo -e "${BOLD}Enter section ID to show tree:${RESET} ")" section_id
|
read -p "$(echo -e "${BOLD}Enter section ID to show tree:${RESET} ")" section_id
|
||||||
show_library_tree "$section_id"
|
show_library_tree "$section_id"
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
q|Q)
|
q|Q)
|
||||||
print_status "${INFO}" "Goodbye!" "${CYAN}"
|
print_status "${INFO}" "Goodbye!" "${CYAN}"
|
||||||
@@ -716,10 +803,6 @@ main() {
|
|||||||
exit 2
|
exit 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! find_scanner; then
|
|
||||||
exit 3
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Handle commands
|
# Handle commands
|
||||||
case "${1,,}" in
|
case "${1,,}" in
|
||||||
"list")
|
"list")
|
||||||
@@ -740,10 +823,12 @@ main() {
|
|||||||
analyze_library "$section_id" "$deep"
|
analyze_library "$section_id" "$deep"
|
||||||
;;
|
;;
|
||||||
"generate"|"thumbnails")
|
"generate"|"thumbnails")
|
||||||
|
if ! find_scanner; then exit 3; fi
|
||||||
local section_id="${2:-}"
|
local section_id="${2:-}"
|
||||||
generate_thumbnails "$section_id"
|
generate_thumbnails "$section_id"
|
||||||
;;
|
;;
|
||||||
"tree")
|
"tree")
|
||||||
|
if ! find_scanner; then exit 3; fi
|
||||||
local section_id="$2"
|
local section_id="$2"
|
||||||
if [[ -z "$section_id" ]]; then
|
if [[ -z "$section_id" ]]; then
|
||||||
print_status "${CROSS}" "Section ID required for tree command" "${RED}"
|
print_status "${CROSS}" "Section ID required for tree command" "${RED}"
|
||||||
|
|||||||
Reference in New Issue
Block a user