fixed arrs clean up showing only orphans

This commit is contained in:
2026-04-24 17:22:34 -04:00
parent 212af90425
commit b3f72417a4
7 changed files with 1115 additions and 452 deletions
+143 -47
View File
@@ -19,6 +19,18 @@
# 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.
#
# Safety layers — all must pass before any file is touched:
# 1. Container must be running and healthy
# 2. API must be reachable
# 3. Artist count must be > 0
# 4. Tracked file count must be > 0
# 5. Tracked count must be >= LIDARR_MIN_TRACKED_PCT% of last known count
# 6. Deletion size must be < LIDARR_MAX_DELETE_GB — or --i-know-what-im-doing required
#
# --i-know-what-im-doing flag:
# Required when deletion would exceed LIDARR_MAX_DELETE_GB
# Long and annoying by design — cannot be added accidentally
#
# 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.
@@ -30,7 +42,18 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# Check for --i-know-what-im-doing flag before parse_args
I_KNOW=false
FILTERED_ARGS=()
for arg in "$@"; do
if [[ "$arg" == "--i-know-what-im-doing" ]]; then
I_KNOW=true
else
FILTERED_ARGS+=("$arg")
fi
done
parse_args "${FILTERED_ARGS[@]}"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_GEAR Setup ━━━
@@ -67,6 +90,7 @@ fi
LIDARR_URL="$HOST1_LIDARR_URL"
LIDARR_API_KEY="$HOST1_LIDARR_API_KEY"
LIDARR_MUSIC_ROOT="$HOST1_LIDARR_MUSIC_ROOT"
LIDARR_CONTAINER="Lidarr" # Docker container name for health check
info "Lidarr instance: $LOCAL_SERVER_NAME$LIDARR_URL"
info "Music root: $LIDARR_MUSIC_ROOT"
@@ -83,7 +107,43 @@ fi
acquire_lock "wait"
success "Config validated"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SHIELD Safety Layer 1 — Container Health ━━━
# -----------------------------------------------------------------------------------------------
echo ""
echo "━━━ $ICON_SHIELD Safety Checks ━━━"
# Check container is running
CONTAINER_RUNNING=$(docker inspect -f '{{.State.Running}}' "$LIDARR_CONTAINER" 2>/dev/null)
if [[ "$CONTAINER_RUNNING" != "true" ]]; then
error "$LIDARR_CONTAINER container is not running — aborting"
notify "Lidarr cleanup aborted on $(hostname) — Lidarr container not running" "Lidarr Cleanup" "warning"
exit 1
fi
success "$LIDARR_CONTAINER is running"
# Check container health — only fail if explicitly unhealthy
CONTAINER_HEALTH=$(docker inspect -f '{{.State.Health.Status}}' "$LIDARR_CONTAINER" 2>/dev/null)
case "$CONTAINER_HEALTH" in
healthy)
success "$LIDARR_CONTAINER is healthy" ;;
"")
info "$LIDARR_CONTAINER has no health check configured — proceeding" ;;
starting)
error "$LIDARR_CONTAINER is still starting — aborting"
notify "Lidarr cleanup aborted on $(hostname) — Lidarr container still starting" "Lidarr Cleanup" "warning"
exit 1 ;;
unhealthy)
error "$LIDARR_CONTAINER is unhealthy — aborting"
notify "Lidarr cleanup aborted on $(hostname) — Lidarr container unhealthy" "Lidarr Cleanup" "warning"
exit 1 ;;
*)
warn "$LIDARR_CONTAINER health status: $CONTAINER_HEALTH — proceeding with caution" ;;
esac
success "All safety checks passed"
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no files will be deleted"
[[ "$I_KNOW" == true ]] && warn "OVERRIDE — --i-know-what-im-doing flag active"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SUMMARY Status ━━━
@@ -94,6 +154,8 @@ if [[ "$SHOW_STATUS" == true ]]; then
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 Max delete: ${LIDARR_MAX_DELETE_GB}GB (requires --i-know-what-im-doing)"
echo "$ICON_GEAR Min tracked %: ${LIDARR_MIN_TRACKED_PCT}% of last run"
echo "$ICON_GEAR Extensions: ${LIDARR_EXTENSIONS[*]}"
echo "$ICON_GEAR Protected patterns: ${LIDARR_PROTECTED_PATTERNS[*]}"
echo "$ICON_GEAR Dry Run: $DRY_RUN"
@@ -101,13 +163,10 @@ if [[ "$SHOW_STATUS" == true ]]; then
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
@@ -129,7 +188,6 @@ lidarr_api() {
echo "$body"
}
# Returns 0 if file extension matches LIDARR_EXTENSIONS list
is_music_file() {
local ext="${1##*.}"
ext="${ext,,}"
@@ -139,7 +197,6 @@ is_music_file() {
return 1
}
# Returns 0 if filename matches any pattern in LIDARR_PROTECTED_PATTERNS
is_protected_file() {
local filename
filename=$(basename "$1")
@@ -158,6 +215,7 @@ is_protected_file() {
echo ""
echo "━━━ $ICON_SYNC Fetching Lidarr Tracked Files ━━━"
# Safety Layer 2 — API reachability
check_api "$LIDARR_URL" "Lidarr" || {
notify "Lidarr cleanup aborted on $(hostname) — API unreachable" "Lidarr Cleanup" "warning"
exit 1
@@ -165,9 +223,7 @@ check_api "$LIDARR_URL" "Lidarr" || {
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
# Step 1 — Get all artists
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"
@@ -177,9 +233,11 @@ ARTIST_RESPONSE=$(lidarr_api "artist") || {
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)
# Safety Layer 3 — artist count
if [[ "$ARTIST_COUNT" -eq 0 ]]; then
warn "No artists found in Lidarr — library may be empty"
exit 0
error "No artists returned from Lidarr API — aborting to prevent mass deletion"
notify "Lidarr cleanup aborted on $(hostname) — API returned 0 artists" "Lidarr Cleanup" "warning"
exit 1
fi
info "Found $ARTIST_COUNT artists — fetching track files..."
@@ -200,17 +258,38 @@ while IFS= read -r artist_id; do
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"
# Safety Layer 4 — tracked file count
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"
error "API returned 0 tracked files — aborting to prevent mass deletion"
notify "Lidarr cleanup aborted on $(hostname) — API returned 0 tracked files" "Lidarr Cleanup" "warning"
exit 1
fi
success "Lidarr tracks $TRACKED_COUNT files across $ARTIST_COUNT artists"
# Safety Layer 5 — percentage drop check against last known count
if [[ -f "$LIDARR_TRACKED_COUNT_FILE" ]]; then
LAST_COUNT=$(cat "$LIDARR_TRACKED_COUNT_FILE" 2>/dev/null || echo 0)
if [[ "$LAST_COUNT" -gt 0 ]]; then
PCT=$(awk "BEGIN {printf \"%d\", ($TRACKED_COUNT / $LAST_COUNT) * 100}")
if [[ "$PCT" -lt "$LIDARR_MIN_TRACKED_PCT" ]]; then
error "Tracked file count dropped to ${PCT}% of last run ($TRACKED_COUNT vs $LAST_COUNT)"
error "This suggests an API issue — aborting to prevent mass deletion"
error "If this is expected (large library removal), delete: $LIDARR_TRACKED_COUNT_FILE"
notify "Lidarr cleanup aborted on $(hostname) — tracked count dropped to ${PCT}% of last run" "Lidarr Cleanup" "warning"
exit 1
fi
info "Tracked count check: ${PCT}% of last run ($TRACKED_COUNT vs $LAST_COUNT) ✅"
fi
else
info "No previous count on record — first run, saving baseline"
fi
# Save current count for next run comparison
echo "$TRACKED_COUNT" > "$LIDARR_TRACKED_COUNT_FILE"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_CLEAN Scanning Music Root ━━━
# -----------------------------------------------------------------------------------------------
@@ -231,17 +310,16 @@ JUNK_BYTES=0
AGE_SECONDS=$(( LIDARR_ORPHAN_AGE * 86400 ))
NOW=$(date +%s)
MAX_DELETE_BYTES=$(awk "BEGIN {printf \"%d\", $LIDARR_MAX_DELETE_GB * 1073741824}")
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++))
@@ -251,7 +329,6 @@ while IFS= read -r filepath; do
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 ))
@@ -261,35 +338,56 @@ while IFS= read -r filepath; do
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
((ORPHAN_COUNT++))
ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
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
((JUNK_COUNT++))
JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
fi
done < <(find "$LIDARR_MUSIC_ROOT" -type f 2>/dev/null)
# Empty folder cleanup
TOTAL_DELETE_BYTES=$(( ORPHAN_BYTES + JUNK_BYTES ))
TOTAL_REMOVED=$(( ORPHAN_COUNT + JUNK_COUNT ))
# -----------------------------------------------------------------------------------------------
# Safety Layer 6 — deletion size threshold
# -----------------------------------------------------------------------------------------------
if [[ "$TOTAL_DELETE_BYTES" -gt "$MAX_DELETE_BYTES" ]]; then
TOTAL_HUMAN=$(awk "BEGIN {printf \"%.1fGB\", $TOTAL_DELETE_BYTES / 1073741824}")
if [[ "$I_KNOW" != true ]]; then
echo ""
error "Deletion would exceed ${LIDARR_MAX_DELETE_GB}GB threshold — $TOTAL_HUMAN would be deleted"
error "Review the ORPHAN lines above carefully before proceeding"
error "If this is expected, rerun with: --i-know-what-im-doing"
notify "Lidarr cleanup halted on $(hostname)${TOTAL_HUMAN} deletion requires --i-know-what-im-doing" "Lidarr Cleanup" "warning"
exit 1
else
warn "OVERRIDE — deletion is ${TOTAL_HUMAN} — proceeding because --i-know-what-im-doing"
fi
fi
# All safety layers passed — execute deletions
if [[ "$DRY_RUN" == false ]]; then
while IFS= read -r filepath; do
[[ -z "$filepath" ]] && continue
if grep -qF "$filepath" "$TRACKED_FILE" 2>/dev/null; then continue; fi
if is_protected_file "$filepath"; then continue; fi
FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0)
FILE_AGE=$(( NOW - FILE_MTIME ))
if is_music_file "$filepath"; then
[[ "$FILE_AGE" -lt "$AGE_SECONDS" ]] && continue
fi
rm -f "$filepath" 2>/dev/null || error "Failed to delete: $filepath"
done < <(find "$LIDARR_MUSIC_ROOT" -type f 2>/dev/null)
echo ""
info "Cleaning up empty folders..."
find "$LIDARR_MUSIC_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
@@ -298,7 +396,6 @@ fi
END=$(date +%s)
# Format bytes helper
format_bytes() {
local bytes=$1
if (( bytes > 1073741824 )); then
@@ -312,17 +409,16 @@ format_bytes() {
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_SYNC Tracked by Lidarr: $TRACKED_COUNT files ($ARTIST_COUNT artists)"
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_TRASH Orphans: $ORPHAN_COUNT files ($ORPHAN_HUMAN)"
echo "$ICON_TRASH Junk: $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 ""
@@ -332,7 +428,7 @@ 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"
echo "$ICON_DONE Status: $ICON_SUCCESS DONE — $TOTAL_REMOVED files removed (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)"
notify "Lidarr cleanup on $(hostname) — removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" "Lidarr Cleanup" "normal"
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+27 -3
View File
@@ -69,6 +69,18 @@ else
RADARR_MOVIES_ROOT="$HOST2_RADARR_MOVIES_ROOT"
fi
# Load path map for this host
declare -A PATH_MAP
if [[ "$LOCAL_SERVER_NAME" == "$HOST1" ]]; then
for key in "${!HOST1_RADARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST1_RADARR_PATH_MAP[$key]}"
done
else
for key in "${!HOST2_RADARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST2_RADARR_PATH_MAP[$key]}"
done
fi
info "Radarr instance: $LOCAL_SERVER_NAME$RADARR_URL"
info "Movies root: $RADARR_MOVIES_ROOT"
@@ -174,7 +186,11 @@ mkdir -p "$TMP_DIR"
trap "rm -rf $TMP_DIR" EXIT
TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
echo "$MOVIEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null | sort > "$TRACKED_FILE"
while IFS= read -r api_path; do
[[ -z "$api_path" ]] && continue
translate_path "$api_path" PATH_MAP >> "$TRACKED_FILE"
done < <(echo "$MOVIEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null)
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
success "Radarr tracks $TRACKED_COUNT movie files"
@@ -256,12 +272,20 @@ while IFS= read -r filepath; do
fi
fi
done < <(find "$RADARR_MOVIES_ROOT" -type f 2>/dev/null)
done < <(
# Scan all host paths defined in PATH_MAP — covers all root folders managed by Radarr
for host_path in "${PATH_MAP[@]}" "$RADARR_MOVIES_ROOT"; do
[[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null
done | sort -u
)
if [[ "$DRY_RUN" == false ]]; then
echo ""
info "Cleaning up empty folders..."
find "$RADARR_MOVIES_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
for host_path in "${PATH_MAP[@]}" "$RADARR_MOVIES_ROOT"; do
[[ -d "$host_path" ]] && \
find "$host_path" -mindepth 1 -type d -empty -delete 2>/dev/null
done
success "Empty folders removed"
fi
+27 -3
View File
@@ -69,6 +69,18 @@ else
SONARR_TV_ROOT="$HOST2_SONARR_TV_ROOT"
fi
# Load path map for this host
declare -A PATH_MAP
if [[ "$LOCAL_SERVER_NAME" == "$HOST1" ]]; then
for key in "${!HOST1_SONARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST1_SONARR_PATH_MAP[$key]}"
done
else
for key in "${!HOST2_SONARR_PATH_MAP[@]}"; do
PATH_MAP["$key"]="${HOST2_SONARR_PATH_MAP[$key]}"
done
fi
info "Sonarr instance: $LOCAL_SERVER_NAME$SONARR_URL"
info "TV root: $SONARR_TV_ROOT"
@@ -174,7 +186,11 @@ mkdir -p "$TMP_DIR"
trap "rm -rf $TMP_DIR" EXIT
TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
echo "$EPISODEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null | sort > "$TRACKED_FILE"
while IFS= read -r api_path; do
[[ -z "$api_path" ]] && continue
translate_path "$api_path" PATH_MAP >> "$TRACKED_FILE"
done < <(echo "$EPISODEFILE_RESPONSE" | jq -r '.[].path' 2>/dev/null)
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
success "Sonarr tracks $TRACKED_COUNT episode files"
@@ -256,12 +272,20 @@ while IFS= read -r filepath; do
fi
fi
done < <(find "$SONARR_TV_ROOT" -type f 2>/dev/null)
done < <(
# Scan all host paths defined in PATH_MAP — covers all root folders managed by Sonarr
for host_path in "${PATH_MAP[@]}" "$SONARR_TV_ROOT"; do
[[ -d "$host_path" ]] && find "$host_path" -type f 2>/dev/null
done | sort -u
)
if [[ "$DRY_RUN" == false ]]; then
echo ""
info "Cleaning up empty folders..."
find "$SONARR_TV_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
for host_path in "${PATH_MAP[@]}" "$SONARR_TV_ROOT"; do
[[ -d "$host_path" ]] && \
find "$host_path" -mindepth 1 -type d -empty -delete 2>/dev/null
done
success "Empty folders removed"
fi