From bc36f3b2f3483cc9ee86c41263cadc77b7026e0c Mon Sep 17 00:00:00 2001 From: RandomNinjaAtk Date: Sat, 15 Jul 2023 15:39:43 -0400 Subject: [PATCH] Create UnmappedFolderCleaner.bash --- radarr/UnmappedFolderCleaner.bash | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 radarr/UnmappedFolderCleaner.bash diff --git a/radarr/UnmappedFolderCleaner.bash b/radarr/UnmappedFolderCleaner.bash new file mode 100644 index 0000000..e95862e --- /dev/null +++ b/radarr/UnmappedFolderCleaner.bash @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +scriptVersion="1.0.2" + +if [ -z "$arrUrl" ] || [ -z "$arrApiKey" ]; then + arrUrlBase="$(cat /config/config.xml | xq | jq -r .Config.UrlBase)" + if [ "$arrUrlBase" == "null" ]; then + arrUrlBase="" + else + arrUrlBase="/$(echo "$arrUrlBase" | sed "s/\///g")" + fi + arrApiKey="$(cat /config/config.xml | xq | jq -r .Config.ApiKey)" + arrPort="$(cat /config/config.xml | xq | jq -r .Config.Port)" + arrUrl="http://127.0.0.1:${arrPort}${arrUrlBase}" +fi + +# auto-clean up log file to reduce space usage +if [ -f "/config/logs/UnmappedFolderCleaner.txt" ]; then + find /config/logs -type f -name "UnmappedFolderCleaner.txt" -size +1024k -delete +fi + +if [ ! -f "/config/logs/UnmappedFolderCleaner.txt" ]; then + touch "/config/logs/UnmappedFolderCleaner.txt" + chmod 777 "/config/logs/UnmappedFolderCleaner.txt" +fi +exec &> >(tee -a "/config/logs/UnmappedFolderCleaner.txt") + +log () { + m_time=`date "+%F %T"` + echo $m_time" :: UnmappedFolderCleaner :: $scriptVersion :: "$1 +} + +log "Finding UnmappedFolders to purge..." +OLDIFS="$IFS" +IFS=$'\n' +unmappedFolders=$(curl -s "$arrUrl/api/v3/rootFolder" -H "X-Api-Key: $arrApiKey" | jq -r ".[].unmappedFolders[].path") +unmappedFoldersCount=$(echo -n "$unmappedFolders" | wc -l) +log "$unmappedFoldersCount Folders Found!" +if [ $unmappedFoldersCount = 0 ]; then + log "No cleanup required, exiting..." + exit +fi +for folder in $(echo "$unmappedFolders"); do + log "Removing $folder" + rm -rf "$folder" +done +IFS="$OLDIFS" + +exit