338 lines
12 KiB
Bash
338 lines
12 KiB
Bash
#!/bin/bash
|
|
# -----------------------------------------------------------------------------------------------
|
|
# --------------------------------- Lidarr Cleanup Script --------------------------------------
|
|
# -----------------------------------------------------------------------------------------------
|
|
# Removes orphaned music files from the library that Lidarr no longer tracks.
|
|
# Uses the Lidarr API to build a complete list of tracked file paths then compares
|
|
# against what exists on disk — anything not tracked and older than LIDARR_ORPHAN_AGE
|
|
# days is considered an orphan and deleted.
|
|
#
|
|
# File classification:
|
|
# TRACKED — Lidarr API knows about this exact file path → leave it alone
|
|
# PROTECTED — matches LIDARR_PROTECTED_PATTERNS → never delete (cover art, .nfo etc.)
|
|
# ORPHAN — music file, not tracked, older than LIDARR_ORPHAN_AGE days → delete
|
|
# JUNK — not a music extension, not protected → delete regardless of age
|
|
# RECENT — not tracked, under LIDARR_ORPHAN_AGE days old → skip (may be mid-import)
|
|
#
|
|
# Why protected patterns matter:
|
|
# Lidarr generates cover art (*.jpg), metadata (*.nfo) and lyrics (*.lrc) but does
|
|
# not include these in its tracked file API response. Without protection these would
|
|
# be classified as orphans and deleted — breaking Lidarr and Emby metadata display.
|
|
#
|
|
# Lidarr runs on HOST1 only — music library is HOST1's source of truth.
|
|
# If run on HOST2 this script exits cleanly with no action.
|
|
# All configuration in Master.conf under Arr Cleanup section.
|
|
# Supports --dry-run to preview what would be deleted without making changes.
|
|
# -----------------------------------------------------------------------------------------------
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
source "$SCRIPT_DIR/../Master.conf"
|
|
source "$SCRIPT_DIR/../common.sh"
|
|
|
|
parse_args "$@"
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# ━━━ $ICON_GEAR Setup ━━━
|
|
# -----------------------------------------------------------------------------------------------
|
|
echo ""
|
|
echo "━━━ $ICON_GEAR Setup ━━━"
|
|
|
|
if [[ "$EUID" -ne 0 ]]; then
|
|
error "Must be run as root"
|
|
exit 1
|
|
fi
|
|
|
|
success "Running as root"
|
|
|
|
if ! command -v curl >/dev/null 2>&1; then
|
|
error "curl not found — required for Lidarr API calls"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v jq >/dev/null 2>&1; then
|
|
error "jq not found — required for JSON parsing"
|
|
notify "Lidarr cleanup failed on $(hostname) — jq not installed" "Lidarr Cleanup" "warning"
|
|
exit 1
|
|
fi
|
|
|
|
# Lidarr runs on HOST1 only — exit cleanly if running on HOST2
|
|
detect_hosts
|
|
|
|
if [[ "$LOCAL_SERVER_NAME" != "$HOST1" ]]; then
|
|
info "Lidarr runs on $HOST1 only — skipping on $LOCAL_SERVER_NAME"
|
|
exit 0
|
|
fi
|
|
|
|
LIDARR_URL="$HOST1_LIDARR_URL"
|
|
LIDARR_API_KEY="$HOST1_LIDARR_API_KEY"
|
|
LIDARR_MUSIC_ROOT="$HOST1_LIDARR_MUSIC_ROOT"
|
|
|
|
info "Lidarr instance: $LOCAL_SERVER_NAME → $LIDARR_URL"
|
|
info "Music root: $LIDARR_MUSIC_ROOT"
|
|
|
|
require_var LIDARR_URL
|
|
require_var LIDARR_API_KEY
|
|
require_var LIDARR_MUSIC_ROOT
|
|
|
|
if [[ ! -d "$LIDARR_MUSIC_ROOT" ]]; then
|
|
error "Music root not found: $LIDARR_MUSIC_ROOT"
|
|
notify "Lidarr cleanup failed on $(hostname) — music root not found: $LIDARR_MUSIC_ROOT" "Lidarr Cleanup" "warning"
|
|
exit 1
|
|
fi
|
|
|
|
acquire_lock "wait"
|
|
|
|
success "Config validated"
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# ━━━ $ICON_SUMMARY Status ━━━
|
|
# -----------------------------------------------------------------------------------------------
|
|
if [[ "$SHOW_STATUS" == true ]]; then
|
|
echo ""
|
|
echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━"
|
|
echo "$ICON_GEAR Lidarr URL: $LIDARR_URL"
|
|
echo "$ICON_GEAR Music root: $LIDARR_MUSIC_ROOT"
|
|
echo "$ICON_TIME Orphan age: ${LIDARR_ORPHAN_AGE} days"
|
|
echo "$ICON_GEAR Extensions: ${LIDARR_EXTENSIONS[*]}"
|
|
echo "$ICON_GEAR Protected patterns: ${LIDARR_PROTECTED_PATTERNS[*]}"
|
|
echo "$ICON_GEAR Dry Run: $DRY_RUN"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━"
|
|
exit 0
|
|
fi
|
|
|
|
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no files will be deleted"
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# HELPERS
|
|
# -----------------------------------------------------------------------------------------------
|
|
|
|
# Makes a GET request to the Lidarr API — exits on connection failure
|
|
lidarr_api() {
|
|
local endpoint="$1"
|
|
local response http_code body
|
|
|
|
response=$(curl -sf \
|
|
--max-time 30 \
|
|
-H "X-Api-Key: $LIDARR_API_KEY" \
|
|
-w "\n%{http_code}" \
|
|
"${LIDARR_URL}/api/v1/${endpoint}" 2>/dev/null)
|
|
|
|
http_code=$(echo "$response" | tail -1)
|
|
body=$(echo "$response" | head -n -1)
|
|
|
|
if [[ "$http_code" != "200" ]]; then
|
|
error "Lidarr API returned HTTP $http_code for endpoint: $endpoint"
|
|
return 1
|
|
fi
|
|
|
|
echo "$body"
|
|
}
|
|
|
|
# Returns 0 if file extension matches LIDARR_EXTENSIONS list
|
|
is_music_file() {
|
|
local ext="${1##*.}"
|
|
ext="${ext,,}"
|
|
for valid_ext in "${LIDARR_EXTENSIONS[@]}"; do
|
|
[[ "$ext" == "$valid_ext" ]] && return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Returns 0 if filename matches any pattern in LIDARR_PROTECTED_PATTERNS
|
|
is_protected_file() {
|
|
local filename
|
|
filename=$(basename "$1")
|
|
for pattern in "${LIDARR_PROTECTED_PATTERNS[@]}"; do
|
|
# shellcheck disable=SC2254
|
|
case "$filename" in
|
|
$pattern) return 0 ;;
|
|
esac
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# ━━━ $ICON_SYNC Fetching Lidarr Tracked Files ━━━
|
|
# -----------------------------------------------------------------------------------------------
|
|
echo ""
|
|
echo "━━━ $ICON_SYNC Fetching Lidarr Tracked Files ━━━"
|
|
|
|
check_api "$LIDARR_URL" "Lidarr" || {
|
|
notify "Lidarr cleanup aborted on $(hostname) — API unreachable" "Lidarr Cleanup" "warning"
|
|
exit 1
|
|
}
|
|
|
|
info "Querying Lidarr API: $LIDARR_URL"
|
|
|
|
# Lidarr requires artistId parameter for trackFile endpoint
|
|
# Step 1: Get all artist IDs
|
|
# Step 2: For each artist, get their track files
|
|
ARTIST_RESPONSE=$(lidarr_api "artist") || {
|
|
error "Failed to fetch artists from Lidarr — check URL and API key"
|
|
notify "Lidarr cleanup failed on $(hostname) — could not fetch artists" "Lidarr Cleanup" "warning"
|
|
exit 1
|
|
}
|
|
|
|
ARTIST_IDS=$(echo "$ARTIST_RESPONSE" | jq -r '.[].id' 2>/dev/null)
|
|
ARTIST_COUNT=$(echo "$ARTIST_IDS" | grep -c "[0-9]" 2>/dev/null || echo 0)
|
|
|
|
if [[ "$ARTIST_COUNT" -eq 0 ]]; then
|
|
warn "No artists found in Lidarr — library may be empty"
|
|
exit 0
|
|
fi
|
|
|
|
info "Found $ARTIST_COUNT artists — fetching track files..."
|
|
|
|
TMP_DIR="/tmp/lidarr_cleanup_$$"
|
|
mkdir -p "$TMP_DIR"
|
|
trap "rm -rf $TMP_DIR" EXIT
|
|
|
|
TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
|
|
> "$TRACKED_FILE"
|
|
|
|
while IFS= read -r artist_id; do
|
|
[[ -z "$artist_id" ]] && continue
|
|
ARTIST_TRACKS=$(lidarr_api "trackFile?artistId=${artist_id}" 2>/dev/null)
|
|
if [[ -n "$ARTIST_TRACKS" ]]; then
|
|
echo "$ARTIST_TRACKS" | jq -r '.[].path' 2>/dev/null >> "$TRACKED_FILE"
|
|
fi
|
|
done <<< "$ARTIST_IDS"
|
|
|
|
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
|
|
|
|
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
|
|
success "Lidarr tracks $TRACKED_COUNT files across $ARTIST_COUNT artists"
|
|
|
|
if [[ "$TRACKED_COUNT" -eq 0 ]]; then
|
|
warn "No tracked files returned — Lidarr may not have scanned yet or library is empty"
|
|
warn "Aborting to prevent mass deletion"
|
|
notify "Lidarr cleanup aborted on $(hostname) — no tracked files returned from API" "Lidarr Cleanup" "warning"
|
|
exit 1
|
|
fi
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# ━━━ $ICON_CLEAN Scanning Music Root ━━━
|
|
# -----------------------------------------------------------------------------------------------
|
|
echo ""
|
|
echo "━━━ $ICON_CLEAN Scanning Music Root ━━━"
|
|
info "Root: $LIDARR_MUSIC_ROOT"
|
|
info "Orphan age: ${LIDARR_ORPHAN_AGE} days"
|
|
info "Protected: ${LIDARR_PROTECTED_PATTERNS[*]}"
|
|
echo ""
|
|
|
|
START=$(date +%s)
|
|
ORPHAN_COUNT=0
|
|
JUNK_COUNT=0
|
|
RECENT_COUNT=0
|
|
PROTECTED_COUNT=0
|
|
ORPHAN_BYTES=0
|
|
JUNK_BYTES=0
|
|
|
|
AGE_SECONDS=$(( LIDARR_ORPHAN_AGE * 86400 ))
|
|
NOW=$(date +%s)
|
|
|
|
while IFS= read -r filepath; do
|
|
[[ -z "$filepath" ]] && continue
|
|
|
|
# TRACKED — Lidarr knows about this file
|
|
if grep -qF "$filepath" "$TRACKED_FILE" 2>/dev/null; then
|
|
log "TRACKED: $filepath"
|
|
continue
|
|
fi
|
|
|
|
# PROTECTED — never delete regardless of tracking status
|
|
if is_protected_file "$filepath"; then
|
|
log "PROTECTED: $filepath"
|
|
((PROTECTED_COUNT++))
|
|
continue
|
|
fi
|
|
|
|
FILE_SIZE=$(stat -c%s "$filepath" 2>/dev/null || echo 0)
|
|
|
|
if is_music_file "$filepath"; then
|
|
# Music file not tracked — check age
|
|
FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0)
|
|
FILE_AGE=$(( NOW - FILE_MTIME ))
|
|
|
|
if [[ "$FILE_AGE" -lt "$AGE_SECONDS" ]]; then
|
|
log "RECENT (skipping): $filepath"
|
|
((RECENT_COUNT++))
|
|
continue
|
|
fi
|
|
|
|
# ORPHAN — old enough to delete
|
|
warn "$ICON_TRASH ORPHAN: $filepath"
|
|
if [[ "$DRY_RUN" == false ]]; then
|
|
rm -f "$filepath" && {
|
|
((ORPHAN_COUNT++))
|
|
ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
|
|
} || error "Failed to delete: $filepath"
|
|
else
|
|
((ORPHAN_COUNT++))
|
|
ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
|
|
fi
|
|
else
|
|
# JUNK — not a music extension, not protected
|
|
log "JUNK: $filepath"
|
|
if [[ "$DRY_RUN" == false ]]; then
|
|
rm -f "$filepath" && {
|
|
((JUNK_COUNT++))
|
|
JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
|
|
} || error "Failed to delete: $filepath"
|
|
else
|
|
((JUNK_COUNT++))
|
|
JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
|
|
fi
|
|
fi
|
|
|
|
done < <(find "$LIDARR_MUSIC_ROOT" -type f 2>/dev/null)
|
|
|
|
# Empty folder cleanup
|
|
if [[ "$DRY_RUN" == false ]]; then
|
|
echo ""
|
|
info "Cleaning up empty folders..."
|
|
find "$LIDARR_MUSIC_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
|
|
success "Empty folders removed"
|
|
fi
|
|
|
|
END=$(date +%s)
|
|
|
|
# Format bytes helper
|
|
format_bytes() {
|
|
local bytes=$1
|
|
if (( bytes > 1073741824 )); then
|
|
awk "BEGIN {printf \"%.1fGB\", $bytes / 1073741824}"
|
|
elif (( bytes > 1048576 )); then
|
|
awk "BEGIN {printf \"%.1fMB\", $bytes / 1048576}"
|
|
else
|
|
echo "${bytes}B"
|
|
fi
|
|
}
|
|
|
|
ORPHAN_HUMAN=$(format_bytes $ORPHAN_BYTES)
|
|
JUNK_HUMAN=$(format_bytes $JUNK_BYTES)
|
|
TOTAL_REMOVED=$(( ORPHAN_COUNT + JUNK_COUNT ))
|
|
|
|
# -----------------------------------------------------------------------------------------------
|
|
# ━━━ $ICON_SUMMARY Summary ━━━
|
|
# -----------------------------------------------------------------------------------------------
|
|
echo ""
|
|
echo "━━━━━ $ICON_SUMMARY LIDARR CLEANUP SUMMARY ━━━━━"
|
|
echo "$ICON_SYNC Tracked by Lidarr: $TRACKED_COUNT files"
|
|
echo "$ICON_SHIELD Protected: $PROTECTED_COUNT files (cover art, metadata)"
|
|
echo "$ICON_TRASH Orphans removed: $ORPHAN_COUNT files ($ORPHAN_HUMAN)"
|
|
echo "$ICON_TRASH Junk removed: $JUNK_COUNT files ($JUNK_HUMAN)"
|
|
echo "$ICON_TIME Recent skipped: $RECENT_COUNT files (under ${LIDARR_ORPHAN_AGE} days)"
|
|
echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
|
|
echo ""
|
|
if [[ "$DRY_RUN" == true ]]; then
|
|
echo "$ICON_WARN Status: DRY RUN — no files deleted"
|
|
elif [[ "$TOTAL_REMOVED" -eq 0 ]]; then
|
|
echo "$ICON_DONE Status: $ICON_SUCCESS CLEAN — nothing to remove"
|
|
notify "Lidarr cleanup complete on $(hostname) — library is clean" "Lidarr Cleanup" "normal"
|
|
else
|
|
echo "$ICON_DONE Status: $ICON_SUCCESS DONE — $TOTAL_REMOVED files removed"
|
|
notify "Lidarr cleanup on $(hostname) — removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" "Lidarr Cleanup" "normal"
|
|
fi
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |