33# --- Docker Tool Suite ---
44# =========================
55
6- SCRIPT_VERSION=v1.5.4.5
6+ SCRIPT_VERSION=v1.5.4.6
77
88# --- Strict Mode & Globals ---
99set -euo pipefail
@@ -135,6 +135,19 @@ validate_loaded_config() {
135135CONFIG_DIR=" /home/${CURRENT_USER} /.config/dtools"
136136CONFIG_FILE=" ${CONFIG_DIR} /config.conf"
137137
138+ # --- Helper: Pattern Matching ---
139+ _is_item_ignored () {
140+ local item=" $1 "
141+ local -n ignore_list_ref=" $2 "
142+ for pattern in " ${ignore_list_ref[@]} " ; do
143+ # The right side is intentionally unquoted to allow bash globbing (wildcards)
144+ if [[ " $item " == $pattern ]]; then
145+ return 0
146+ fi
147+ done
148+ return 1
149+ }
150+
138151# --- SHARED UI FUNCTION ---
139152print_standard_menu () {
140153 local title=" $1 "
@@ -245,12 +258,12 @@ _enable_cron_logging() {
245258
246259check_root () {
247260 if [[ $EUID -ne 0 ]]; then
248- echo -e " ${C_YELLOW} This action requires root privileges. Please enter your password.${C_RESET} "
261+ echo -e " \n ${C_YELLOW} This action requires root privileges. Please enter your password.${C_RESET} "
249262 if ! sudo -v; then
250- echo -e " ${C_RED} Authentication failed. Aborting.${C_RESET} " >&2
263+ echo -e " \n ${C_RED} Authentication failed. Aborting.${C_RESET} " >&2
251264 exit 1
252265 fi
253- echo -e " ${C_GREEN} Authentication successful.${C_RESET} \n"
266+ echo -e " \n ${C_GREEN} Authentication successful.${C_RESET} \n"
254267 fi
255268}
256269
@@ -1024,7 +1037,7 @@ _update_app_task() {
10241037 log " No images defined in compose file for $app_name ."
10251038 else
10261039 for image in " ${all_app_images[@]} " ; do
1027- if [[ " ${IGNORED_IMAGES[*]-} " == * " ${ image} " * ]] ; then
1040+ if _is_item_ignored " $ image" IGNORED_IMAGES ; then
10281041 log " Skipping ignored image: $image " " -> Skipping ignored image: ${C_GRAY}${image}${C_RESET} "
10291042 else
10301043 images_to_pull+=(" $image " )
@@ -1656,7 +1669,7 @@ volume_smart_backup_main() {
16561669 ensure_backup_image || volume_manager_menu
16571670
16581671 mapfile -t all_volumes < <( $SUDO_CMD docker volume ls --format " {{.Name}}" ) ; local -a filtered_volumes=()
1659- for volume in " ${all_volumes[@]} " ; do if [[ ! " ${IGNORED_VOLUMES[*]-} " == * " ${ volume} " * ]] ; then filtered_volumes+=(" $volume " ); fi ; done
1672+ for volume in " ${all_volumes[@]} " ; do if ! _is_item_ignored " $ volume" IGNORED_VOLUMES ; then filtered_volumes+=(" $volume " ); fi ; done
16601673 if [[ ${# filtered_volumes[@]} -eq 0 ]]; then echo -e " ${C_YELLOW} No available volumes to back up.${C_RESET} " ; sleep 2; return ; fi
16611674
16621675 local -a selected_status=(); for (( i= 0 ; i< ${# filtered_volumes[@]} ; i++ )) ; do selected_status+=(" true" ); done
@@ -1747,6 +1760,14 @@ volume_smart_backup_main() {
17471760 done
17481761 fi
17491762
1763+ # Check for empty backup directory
1764+ if [[ -z " $( $SUDO_CMD ls -A " $backup_dir " ) " ]]; then
1765+ echo -e " \n${C_YELLOW} No volumes were successfully backed up. ${C_GRAY} (all were empty, skipped, or failed)${C_RESET} "
1766+ echo -e " ${C_GRAY} -> Cleaning up empty directory: ${backup_dir}${C_RESET} "
1767+ $SUDO_CMD rm -rf " $backup_dir "
1768+ return
1769+ fi
1770+
17501771 echo -e " \n${C_YELLOW} Changing ownership to '${CURRENT_USER} '...${C_RESET} "
17511772 $SUDO_CMD chown -R " ${CURRENT_USER} :${CURRENT_USER} " " $backup_dir "
17521773 echo -e " \n${C_GREEN}${TICKMARK} Backup tasks completed successfully!${C_RESET} "
@@ -2145,6 +2166,14 @@ quick_backup_handler() {
21452166 $SUDO_CMD docker run --rm -v " ${volume} :/volume:ro" -v " ${backup_dir} :/backup" " ${BACKUP_IMAGE} " tar -C /volume --zstd -cf " /backup/${volume} .tar.zst" .
21462167 done
21472168
2169+ # Check for empty backup directory
2170+ if [[ -z " $( $SUDO_CMD ls -A " $backup_dir " ) " ]]; then
2171+ echo -e " \n${C_YELLOW} No backups were created.${C_RESET} "
2172+ echo -e " ${C_GRAY} -> Cleaning up empty directory: ${backup_dir}${C_RESET} "
2173+ $SUDO_CMD rm -rf " $backup_dir "
2174+ exit 0
2175+ fi
2176+
21482177 # Set ownership of backup files to current user
21492178 $SUDO_CMD chown -R " ${CURRENT_USER} :${CURRENT_USER} " " $backup_dir "
21502179 echo -e " \n${C_GREEN}${TICKMARK} Quick backup complete!${C_RESET} "
@@ -2470,13 +2499,6 @@ update_unused_images_main() {
24702499 log " --- Starting unused image update script. ---" " ${C_GREEN} --- Starting Unused Docker Image Updater ---${C_RESET} "
24712500 if $DRY_RUN ; then log " --- Starting in Dry Run mode. No changes will be made. ---" " ${C_GRAY} [DRY RUN] No changes will be made.${C_RESET} " ; fi
24722501
2473- local ignored_pattern
2474- if [[ ${# IGNORED_IMAGES[@]} -gt 0 ]]; then
2475- ignored_pattern=$( IFS=" |" ; echo " ${IGNORED_IMAGES[*]} " )
2476- else
2477- ignored_pattern=" ^$"
2478- fi
2479-
24802502 local total_images_scanned=0 used_count=0 ignored_count=0 unpullable_count=0
24812503
24822504 log " Finding images used by existing containers (running or stopped)..." " ${C_GRAY} -> Finding images used by existing containers...${C_RESET} "
@@ -2495,7 +2517,7 @@ update_unused_images_main() {
24952517 continue
24962518 fi
24972519
2498- if [[ ${ # IGNORED_IMAGES[@]} -gt 0 ]] && echo " $image_name " | grep -qE " $ignored_pattern " ; then
2520+ if _is_item_ignored " $image_name " IGNORED_IMAGES ; then
24992521 log " Skipping ignored image: $image_name "
25002522 ignored_count=$(( ignored_count + 1 ))
25012523 continue
@@ -3002,7 +3024,7 @@ update_ignored_items() {
30023024
30033025 local -a selected_status=()
30043026 for item in " ${all_available_items[@]} " ; do
3005- if [[ " ${ignored_items_ref[*]} " == * " ${ item} " * ]] ; then
3027+ if _is_item_ignored " $ item" ignored_items_ref ; then
30063028 selected_status+=(" true" )
30073029 else
30083030 selected_status+=(" false" )
0 commit comments