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
+2 -2
View File
@@ -87,7 +87,7 @@ for container in "${NETWORK_CONNECT_CONTAINERS[@]}"; do
# Verify container exists # Verify container exists
if ! docker inspect "$container" &>/dev/null; then if ! docker inspect "$container" &>/dev/null; then
warn "$container not found — skipping" warn "$container not found — skipping"
FAILED+=("$container") continue
echo "" echo ""
continue continue
fi fi
@@ -98,7 +98,7 @@ for container in "${NETWORK_CONNECT_CONTAINERS[@]}"; do
# Verify network exists # Verify network exists
if ! docker network inspect "$network" &>/dev/null; then if ! docker network inspect "$network" &>/dev/null; then
warn "Network $network not found — skipping" warn "Network $network not found — skipping"
FAILED+=("$container:$network") continue
continue continue
fi fi
+41
View File
@@ -760,6 +760,17 @@ HOST1_LIDARR_URL="http://192.168.50.2:8686"
HOST1_LIDARR_API_KEY="b2977e71ef074bc0a0529d9fcce3b2dc" HOST1_LIDARR_API_KEY="b2977e71ef074bc0a0529d9fcce3b2dc"
HOST1_LIDARR_MUSIC_ROOT="/mnt/user/Music-New" HOST1_LIDARR_MUSIC_ROOT="/mnt/user/Music-New"
# Container path → host path translation
# Lidarr stores file paths using container paths — script scans host paths
# Add one entry per root folder configured in Lidarr Settings → Media Management → Root Folders
declare -A HOST1_LIDARR_PATH_MAP=(
["/ext-music"]="/mnt/user/Music-New"
)
declare -A HOST2_LIDARR_PATH_MAP=(
# HOST2 does not run Lidarr — fill in if that changes
# ["/ext-music"]="/mnt/user/Music-New"
)
LIDARR_ORPHAN_AGE=7 LIDARR_ORPHAN_AGE=7
LIDARR_EXTENSIONS=("flac" "mp3" "m4a" "wav" "aac" "ogg" "opus" "wma") LIDARR_EXTENSIONS=("flac" "mp3" "m4a" "wav" "aac" "ogg" "opus" "wma")
LIDARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.lrc") LIDARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.lrc")
@@ -772,10 +783,25 @@ HOST1_SONARR_URL="http://192.168.50.2:8989"
HOST1_SONARR_API_KEY="130decd3db5b4c25afad64864cd03f9f" HOST1_SONARR_API_KEY="130decd3db5b4c25afad64864cd03f9f"
HOST1_SONARR_TV_ROOT="/mnt/user/Tv_Shows" HOST1_SONARR_TV_ROOT="/mnt/user/Tv_Shows"
# Container path → host path translation
# Add one entry per root folder configured in Sonarr Settings → Media Management → Root Folders
# Note: stand-up_comedy in both Sonarr + Radarr — TV specials and movie specials, one folder
declare -A HOST1_SONARR_PATH_MAP=(
["/tv"]="/mnt/user/Tv_Shows"
["/ext-standup-comedy"]="/mnt/user/stand-up_comedy"
["/kids tv"]="/mnt/user/Kids_Tv_Shows"
["/ext-anime-shows"]="/mnt/user/Anime_Shows-Old"
)
HOST2_SONARR_URL="http://localhost:8989" HOST2_SONARR_URL="http://localhost:8989"
HOST2_SONARR_API_KEY="your-host2-sonarr-api-key" HOST2_SONARR_API_KEY="your-host2-sonarr-api-key"
HOST2_SONARR_TV_ROOT="/mnt/user/Anime_Shows" HOST2_SONARR_TV_ROOT="/mnt/user/Anime_Shows"
declare -A HOST2_SONARR_PATH_MAP=(
# Fill in when HOST2 is back online
# ["/tv"]="/mnt/user/Anime_Shows"
)
SONARR_ORPHAN_AGE=7 SONARR_ORPHAN_AGE=7
SONARR_EXTENSIONS=("mkv" "mp4" "avi" "m4v" "ts" "wmv" "mov") SONARR_EXTENSIONS=("mkv" "mp4" "avi" "m4v" "ts" "wmv" "mov")
SONARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.srt" "*.sub" "*.ass" "*.ssa") SONARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.srt" "*.sub" "*.ass" "*.ssa")
@@ -785,10 +811,25 @@ HOST1_RADARR_URL="http://192.168.50.2:7878"
HOST1_RADARR_API_KEY="d43a3ec6cf1549edb4af0cc63f98b2a9" HOST1_RADARR_API_KEY="d43a3ec6cf1549edb4af0cc63f98b2a9"
HOST1_RADARR_MOVIES_ROOT="/mnt/user/Movies" HOST1_RADARR_MOVIES_ROOT="/mnt/user/Movies"
# Container path → host path translation
# Add one entry per root folder configured in Radarr Settings → Media Management → Root Folders
# Note: stand-up_comedy in both Radarr + Sonarr — movie specials and TV specials, one folder
declare -A HOST1_RADARR_PATH_MAP=(
["/movies"]="/mnt/user/Movies"
["/kids movies"]="/mnt/user/Kids_Movies"
["/ext-stand-up-comedy"]="/mnt/user/stand-up_comedy"
["/anime-movies"]="/mnt/user/Anime_Movies-Old"
)
HOST2_RADARR_URL="http://localhost:7878" HOST2_RADARR_URL="http://localhost:7878"
HOST2_RADARR_API_KEY="your-host2-radarr-api-key" HOST2_RADARR_API_KEY="your-host2-radarr-api-key"
HOST2_RADARR_MOVIES_ROOT="/mnt/user/Anime_Movies" HOST2_RADARR_MOVIES_ROOT="/mnt/user/Anime_Movies"
declare -A HOST2_RADARR_PATH_MAP=(
# Fill in when HOST2 is back online
# ["/anime-movies"]="/mnt/user/Anime_Movies"
)
RADARR_ORPHAN_AGE=7 RADARR_ORPHAN_AGE=7
RADARR_EXTENSIONS=("mkv" "mp4" "avi" "m4v" "wmv" "mov") RADARR_EXTENSIONS=("mkv" "mp4" "avi" "m4v" "wmv" "mov")
RADARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.srt" "*.sub" "*.ass" "*.ssa") RADARR_PROTECTED_PATTERNS=("*.jpg" "*.jpeg" "*.png" "*.nfo" "*.srt" "*.sub" "*.ass" "*.ssa")
+143 -47
View File
@@ -19,6 +19,18 @@
# not include these in its tracked file API response. Without protection these would # 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. # 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. # Lidarr runs on HOST1 only — music library is HOST1's source of truth.
# If run on HOST2 this script exits cleanly with no action. # If run on HOST2 this script exits cleanly with no action.
# All configuration in Master.conf under Arr Cleanup section. # 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/../Master.conf"
source "$SCRIPT_DIR/../common.sh" 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 ━━━ # ━━━ $ICON_GEAR Setup ━━━
@@ -67,6 +90,7 @@ fi
LIDARR_URL="$HOST1_LIDARR_URL" LIDARR_URL="$HOST1_LIDARR_URL"
LIDARR_API_KEY="$HOST1_LIDARR_API_KEY" LIDARR_API_KEY="$HOST1_LIDARR_API_KEY"
LIDARR_MUSIC_ROOT="$HOST1_LIDARR_MUSIC_ROOT" 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 "Lidarr instance: $LOCAL_SERVER_NAME$LIDARR_URL"
info "Music root: $LIDARR_MUSIC_ROOT" info "Music root: $LIDARR_MUSIC_ROOT"
@@ -83,7 +107,43 @@ fi
acquire_lock "wait" 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 ━━━ # ━━━ $ICON_SUMMARY Status ━━━
@@ -94,6 +154,8 @@ if [[ "$SHOW_STATUS" == true ]]; then
echo "$ICON_GEAR Lidarr URL: $LIDARR_URL" echo "$ICON_GEAR Lidarr URL: $LIDARR_URL"
echo "$ICON_GEAR Music root: $LIDARR_MUSIC_ROOT" echo "$ICON_GEAR Music root: $LIDARR_MUSIC_ROOT"
echo "$ICON_TIME Orphan age: ${LIDARR_ORPHAN_AGE} days" 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 Extensions: ${LIDARR_EXTENSIONS[*]}"
echo "$ICON_GEAR Protected patterns: ${LIDARR_PROTECTED_PATTERNS[*]}" echo "$ICON_GEAR Protected patterns: ${LIDARR_PROTECTED_PATTERNS[*]}"
echo "$ICON_GEAR Dry Run: $DRY_RUN" echo "$ICON_GEAR Dry Run: $DRY_RUN"
@@ -101,13 +163,10 @@ if [[ "$SHOW_STATUS" == true ]]; then
exit 0 exit 0
fi fi
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no files will be deleted"
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# HELPERS # HELPERS
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# Makes a GET request to the Lidarr API — exits on connection failure
lidarr_api() { lidarr_api() {
local endpoint="$1" local endpoint="$1"
local response http_code body local response http_code body
@@ -129,7 +188,6 @@ lidarr_api() {
echo "$body" echo "$body"
} }
# Returns 0 if file extension matches LIDARR_EXTENSIONS list
is_music_file() { is_music_file() {
local ext="${1##*.}" local ext="${1##*.}"
ext="${ext,,}" ext="${ext,,}"
@@ -139,7 +197,6 @@ is_music_file() {
return 1 return 1
} }
# Returns 0 if filename matches any pattern in LIDARR_PROTECTED_PATTERNS
is_protected_file() { is_protected_file() {
local filename local filename
filename=$(basename "$1") filename=$(basename "$1")
@@ -158,6 +215,7 @@ is_protected_file() {
echo "" echo ""
echo "━━━ $ICON_SYNC Fetching Lidarr Tracked Files ━━━" echo "━━━ $ICON_SYNC Fetching Lidarr Tracked Files ━━━"
# Safety Layer 2 — API reachability
check_api "$LIDARR_URL" "Lidarr" || { check_api "$LIDARR_URL" "Lidarr" || {
notify "Lidarr cleanup aborted on $(hostname) — API unreachable" "Lidarr Cleanup" "warning" notify "Lidarr cleanup aborted on $(hostname) — API unreachable" "Lidarr Cleanup" "warning"
exit 1 exit 1
@@ -165,9 +223,7 @@ check_api "$LIDARR_URL" "Lidarr" || {
info "Querying Lidarr API: $LIDARR_URL" info "Querying Lidarr API: $LIDARR_URL"
# Lidarr requires artistId parameter for trackFile endpoint # Step 1 — Get all artists
# Step 1: Get all artist IDs
# Step 2: For each artist, get their track files
ARTIST_RESPONSE=$(lidarr_api "artist") || { ARTIST_RESPONSE=$(lidarr_api "artist") || {
error "Failed to fetch artists from Lidarr — check URL and API key" 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" 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_IDS=$(echo "$ARTIST_RESPONSE" | jq -r '.[].id' 2>/dev/null)
ARTIST_COUNT=$(echo "$ARTIST_IDS" | grep -c "[0-9]" 2>/dev/null || echo 0) 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 if [[ "$ARTIST_COUNT" -eq 0 ]]; then
warn "No artists found in Lidarr — library may be empty" error "No artists returned from Lidarr API — aborting to prevent mass deletion"
exit 0 notify "Lidarr cleanup aborted on $(hostname) — API returned 0 artists" "Lidarr Cleanup" "warning"
exit 1
fi fi
info "Found $ARTIST_COUNT artists — fetching track files..." info "Found $ARTIST_COUNT artists — fetching track files..."
@@ -200,17 +258,38 @@ while IFS= read -r artist_id; do
done <<< "$ARTIST_IDS" done <<< "$ARTIST_IDS"
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE" sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
TRACKED_COUNT=$(wc -l < "$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 if [[ "$TRACKED_COUNT" -eq 0 ]]; then
warn "No tracked files returned — Lidarr may not have scanned yet or library is empty" error "API returned 0 tracked files — aborting to prevent mass deletion"
warn "Aborting to prevent mass deletion" notify "Lidarr cleanup aborted on $(hostname) — API returned 0 tracked files" "Lidarr Cleanup" "warning"
notify "Lidarr cleanup aborted on $(hostname) — no tracked files returned from API" "Lidarr Cleanup" "warning"
exit 1 exit 1
fi 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 ━━━ # ━━━ $ICON_CLEAN Scanning Music Root ━━━
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
@@ -231,17 +310,16 @@ JUNK_BYTES=0
AGE_SECONDS=$(( LIDARR_ORPHAN_AGE * 86400 )) AGE_SECONDS=$(( LIDARR_ORPHAN_AGE * 86400 ))
NOW=$(date +%s) NOW=$(date +%s)
MAX_DELETE_BYTES=$(awk "BEGIN {printf \"%d\", $LIDARR_MAX_DELETE_GB * 1073741824}")
while IFS= read -r filepath; do while IFS= read -r filepath; do
[[ -z "$filepath" ]] && continue [[ -z "$filepath" ]] && continue
# TRACKED — Lidarr knows about this file
if grep -qF "$filepath" "$TRACKED_FILE" 2>/dev/null; then if grep -qF "$filepath" "$TRACKED_FILE" 2>/dev/null; then
log "TRACKED: $filepath" log "TRACKED: $filepath"
continue continue
fi fi
# PROTECTED — never delete regardless of tracking status
if is_protected_file "$filepath"; then if is_protected_file "$filepath"; then
log "PROTECTED: $filepath" log "PROTECTED: $filepath"
((PROTECTED_COUNT++)) ((PROTECTED_COUNT++))
@@ -251,7 +329,6 @@ while IFS= read -r filepath; do
FILE_SIZE=$(stat -c%s "$filepath" 2>/dev/null || echo 0) FILE_SIZE=$(stat -c%s "$filepath" 2>/dev/null || echo 0)
if is_music_file "$filepath"; then if is_music_file "$filepath"; then
# Music file not tracked — check age
FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0) FILE_MTIME=$(stat -c %Y "$filepath" 2>/dev/null || echo 0)
FILE_AGE=$(( NOW - FILE_MTIME )) FILE_AGE=$(( NOW - FILE_MTIME ))
@@ -261,35 +338,56 @@ while IFS= read -r filepath; do
continue continue
fi fi
# ORPHAN — old enough to delete
warn "$ICON_TRASH ORPHAN: $filepath" warn "$ICON_TRASH ORPHAN: $filepath"
if [[ "$DRY_RUN" == false ]]; then ((ORPHAN_COUNT++))
rm -f "$filepath" && { ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
((ORPHAN_COUNT++))
ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
} || error "Failed to delete: $filepath"
else
((ORPHAN_COUNT++))
ORPHAN_BYTES=$((ORPHAN_BYTES + FILE_SIZE))
fi
else else
# JUNK — not a music extension, not protected
log "JUNK: $filepath" log "JUNK: $filepath"
if [[ "$DRY_RUN" == false ]]; then ((JUNK_COUNT++))
rm -f "$filepath" && { JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
((JUNK_COUNT++))
JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
} || error "Failed to delete: $filepath"
else
((JUNK_COUNT++))
JUNK_BYTES=$((JUNK_BYTES + FILE_SIZE))
fi
fi fi
done < <(find "$LIDARR_MUSIC_ROOT" -type f 2>/dev/null) 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 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 "" echo ""
info "Cleaning up empty folders..." info "Cleaning up empty folders..."
find "$LIDARR_MUSIC_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null find "$LIDARR_MUSIC_ROOT" -mindepth 1 -type d -empty -delete 2>/dev/null
@@ -298,7 +396,6 @@ fi
END=$(date +%s) END=$(date +%s)
# Format bytes helper
format_bytes() { format_bytes() {
local bytes=$1 local bytes=$1
if (( bytes > 1073741824 )); then if (( bytes > 1073741824 )); then
@@ -312,17 +409,16 @@ format_bytes() {
ORPHAN_HUMAN=$(format_bytes $ORPHAN_BYTES) ORPHAN_HUMAN=$(format_bytes $ORPHAN_BYTES)
JUNK_HUMAN=$(format_bytes $JUNK_BYTES) JUNK_HUMAN=$(format_bytes $JUNK_BYTES)
TOTAL_REMOVED=$(( ORPHAN_COUNT + JUNK_COUNT ))
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SUMMARY Summary ━━━ # ━━━ $ICON_SUMMARY Summary ━━━
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
echo "" echo ""
echo "━━━━━ $ICON_SUMMARY LIDARR CLEANUP SUMMARY ━━━━━" 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_SHIELD Protected: $PROTECTED_COUNT files (cover art, metadata)"
echo "$ICON_TRASH Orphans removed: $ORPHAN_COUNT files ($ORPHAN_HUMAN)" echo "$ICON_TRASH Orphans: $ORPHAN_COUNT files ($ORPHAN_HUMAN)"
echo "$ICON_TRASH Junk removed: $JUNK_COUNT files ($JUNK_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 Recent skipped: $RECENT_COUNT files (under ${LIDARR_ORPHAN_AGE} days)"
echo "$ICON_TIME Duration: $(format_duration $((END - START)))" echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
echo "" echo ""
@@ -332,7 +428,7 @@ elif [[ "$TOTAL_REMOVED" -eq 0 ]]; then
echo "$ICON_DONE Status: $ICON_SUCCESS CLEAN — nothing to remove" echo "$ICON_DONE Status: $ICON_SUCCESS CLEAN — nothing to remove"
notify "Lidarr cleanup complete on $(hostname) — library is clean" "Lidarr Cleanup" "normal" notify "Lidarr cleanup complete on $(hostname) — library is clean" "Lidarr Cleanup" "normal"
else 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" notify "Lidarr cleanup on $(hostname) — removed $TOTAL_REMOVED files (orphans: $ORPHAN_HUMAN junk: $JUNK_HUMAN)" "Lidarr Cleanup" "normal"
fi fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
+27 -3
View File
@@ -69,6 +69,18 @@ else
RADARR_MOVIES_ROOT="$HOST2_RADARR_MOVIES_ROOT" RADARR_MOVIES_ROOT="$HOST2_RADARR_MOVIES_ROOT"
fi 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 "Radarr instance: $LOCAL_SERVER_NAME$RADARR_URL"
info "Movies root: $RADARR_MOVIES_ROOT" info "Movies root: $RADARR_MOVIES_ROOT"
@@ -174,7 +186,11 @@ mkdir -p "$TMP_DIR"
trap "rm -rf $TMP_DIR" EXIT trap "rm -rf $TMP_DIR" EXIT
TRACKED_FILE="$TMP_DIR/tracked_paths.txt" 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") TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
success "Radarr tracks $TRACKED_COUNT movie files" success "Radarr tracks $TRACKED_COUNT movie files"
@@ -256,12 +272,20 @@ while IFS= read -r filepath; do
fi fi
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 if [[ "$DRY_RUN" == false ]]; then
echo "" echo ""
info "Cleaning up empty folders..." 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" success "Empty folders removed"
fi fi
+27 -3
View File
@@ -69,6 +69,18 @@ else
SONARR_TV_ROOT="$HOST2_SONARR_TV_ROOT" SONARR_TV_ROOT="$HOST2_SONARR_TV_ROOT"
fi 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 "Sonarr instance: $LOCAL_SERVER_NAME$SONARR_URL"
info "TV root: $SONARR_TV_ROOT" info "TV root: $SONARR_TV_ROOT"
@@ -174,7 +186,11 @@ mkdir -p "$TMP_DIR"
trap "rm -rf $TMP_DIR" EXIT trap "rm -rf $TMP_DIR" EXIT
TRACKED_FILE="$TMP_DIR/tracked_paths.txt" 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") TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
success "Sonarr tracks $TRACKED_COUNT episode files" success "Sonarr tracks $TRACKED_COUNT episode files"
@@ -256,12 +272,20 @@ while IFS= read -r filepath; do
fi fi
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 if [[ "$DRY_RUN" == false ]]; then
echo "" echo ""
info "Cleaning up empty folders..." 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" success "Empty folders removed"
fi fi
+41 -13
View File
@@ -720,25 +720,18 @@ acquire_lock() {
warn "Stale lock detected for $script_name (PID $existing_pid gone) — clearing" warn "Stale lock detected for $script_name (PID $existing_pid gone) — clearing"
rm -f "$lockfile" rm -f "$lockfile"
else else
# Lock is active — check age # Lock is active — check age (skip warning for continuous scripts)
local lock_age local lock_age
lock_age=$(( $(date +%s) - $(stat -c %Y "$lockfile" 2>/dev/null || echo 0) )) lock_age=$(( $(date +%s) - $(stat -c %Y "$lockfile" 2>/dev/null || echo 0) ))
if [[ "$lock_age" -gt "$LOCK_WARN_AGE" ]]; then if [[ "$lock_age" -gt "$LOCK_WARN_AGE" ]] && [[ "$mode" != "continuous" ]]; then
warn "$script_name has been running for ${lock_age}s — may be stuck (PID $existing_pid)" warn "$script_name has been running for ${lock_age}s — may be stuck (PID $existing_pid)"
fi fi
if [[ "$mode" == "continuous" ]]; then if [[ "$mode" == "continuous" ]]; then
# Continuous scripts (watchdogs, failover) — healthy instance = skip gracefully # Continuous scripts (watchdogs, failover) — healthy instance = always skip
# Only force-restart if PID is stuck/unresponsive beyond LOCK_WARN_AGE # PID is alive and responding — this is correct behavior, not stuck
if [[ "$lock_age" -gt "$LOCK_WARN_AGE" ]]; then log "$ICON_SKIP $script_name already running healthy (PID $existing_pid) — skipping"
warn "$script_name appears stuck (running ${lock_age}s) — clearing and restarting" exit 0
kill "$existing_pid" 2>/dev/null
sleep 2
rm -f "$lockfile"
else
log "$ICON_SKIP $script_name already running healthy (PID $existing_pid) — skipping"
exit 0
fi
elif [[ "$mode" == "wait" ]]; then elif [[ "$mode" == "wait" ]]; then
info "Another instance of $script_name is running — waiting up to ${LOCK_WAIT_TIMEOUT}s" info "Another instance of $script_name is running — waiting up to ${LOCK_WAIT_TIMEOUT}s"
local waited=0 local waited=0
@@ -857,6 +850,41 @@ _release_rsync_on_exit() {
# Verifies API endpoint is reachable before attempting operations # Verifies API endpoint is reachable before attempting operations
# Usage: check_api "http://localhost:8989" "Sonarr" || exit 1 # Usage: check_api "http://localhost:8989" "Sonarr" || exit 1
# ----------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------
# -----------------------------------------------------------------------------------------------
# translate_path — translate container path to host path using a PATH_MAP array
# Used by arr cleanup scripts — arrs store paths using container mounts, not host paths
#
# Usage:
# translate_path "/ext-music/Artist/Album/track.flac" HOST1_LIDARR_PATH_MAP
# Returns: /mnt/user/Music-New/Artist/Album/track.flac
#
# If no match found in PATH_MAP — returns path unchanged
# Handles longest-match first to avoid partial path collisions
# -----------------------------------------------------------------------------------------------
translate_path() {
local api_path="$1"
local -n _path_map="$2" # nameref to the associative array
local best_match=""
local best_len=0
# Find longest matching prefix (handles overlapping container paths)
for container_path in "${!_path_map[@]}"; do
if [[ "$api_path" == "$container_path"* ]]; then
if [[ "${#container_path}" -gt "$best_len" ]]; then
best_match="$container_path"
best_len="${#container_path}"
fi
fi
done
if [[ -n "$best_match" ]]; then
echo "${_path_map[$best_match]}${api_path#$best_match}"
else
echo "$api_path" # no match — return unchanged
fi
}
check_api() { check_api() {
local url="$1" local url="$1"
local service="${2:-API}" local service="${2:-API}"
File diff suppressed because it is too large Load Diff