From 8e0ba1878f25960114226362802c0bc4cd038f58 Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Thu, 12 Mar 2026 20:49:37 +0100 Subject: [PATCH] refactor(cleanup): generalize cleanup beyond logs Rename configuration variables and functions to reflect broader functionality beyond just log files. Updates include renaming LOG_DIRS_FOR_CLEANUP to DIRS_FOR_AUTODELETE and clean_log_files to clean_directory_files. Adds safety warnings to prevent deletion of critical HANA system logs. BREAKING CHANGE: Config keys renamed from LOG_DIRS_FOR_CLEANUP to DIRS_FOR_AUTODELETE and MAX_LOG_FILE_AGE_DAYS to MAX_FILE_AGE_DAYS --- hana.conf | 19 +++++++++++-------- hana_lib.sh | 52 ++++++++++++++++++++++++++-------------------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/hana.conf b/hana.conf index bfd0882..da578d7 100644 --- a/hana.conf +++ b/hana.conf @@ -28,22 +28,25 @@ DIRECTORIES_TO_MONITOR=( "/usr/sap" ) -# --- Log Directories for Auto-Cleanup --- +# --- Directories for Auto-Delete --- # These directories will be automatically cleaned when disk space is low -# Format: "mount_point:log_directory_path" +# Format: "mount_point:directory_path" # The script will check if a monitored directory is on the same mount point -# as a log directory and can clean the log directory to free up space -LOG_DIRS_FOR_CLEANUP=( - "/hana/log:/hana/log" +# as an auto-delete directory and can clean it to free up space +# NOTE: Do NOT include HANA system logs like /hana/log - only include +# directories with safe-to-delete files like backup logs, temp files, etc. +DIRS_FOR_AUTODELETE=( + "/hana/log:/hana/log/backups" + "/hana/data:/hana/data/temp" "/usr/sap:/usr/sap/trans/log" "/usr/sap:/usr/sap/hostctrl/work/log" ) -# --- Disk Cleanup Configuration --- +# --- Disk Auto-Delete Configuration --- # Minimum free space percentage to maintain after cleanup MIN_FREE_SPACE_AFTER_CLEANUP=5 -# Maximum age of log files to delete (in days) -MAX_LOG_FILE_AGE_DAYS=7 +# Maximum age of files to delete (in days) +MAX_FILE_AGE_DAYS=7 # Enable automatic cleanup when disk usage exceeds threshold AUTO_CLEANUP_ENABLED=true diff --git a/hana_lib.sh b/hana_lib.sh index c58fdf9..21d79a9 100644 --- a/hana_lib.sh +++ b/hana_lib.sh @@ -137,22 +137,22 @@ get_available_space_kb() { df -k "$dir" 2>/dev/null | awk 'NR==2 {print $4}' } -# Find log directories on the same mount point -# Usage: find_log_dirs_on_mount "mount_point" -# Returns: Space-separated list of log directories -find_log_dirs_on_mount() { +# Find directories for auto-delete on the same mount point +# Usage: find_autodelete_dirs_on_mount "mount_point" +# Returns: Space-separated list of directories to clean +find_autodelete_dirs_on_mount() { local mount_point="$1" local result="" - for log_entry in "${LOG_DIRS_FOR_CLEANUP[@]}"; do - local entry_mount="${log_entry%%:*}" - local log_dir="${log_entry#*:}" + for entry in "${DIRS_FOR_AUTODELETE[@]}"; do + local entry_mount="${entry%%:*}" + local cleanup_dir="${entry#*:}" - if [ "$entry_mount" == "$mount_point" ] && [ -d "$log_dir" ]; then + if [ "$entry_mount" == "$mount_point" ] && [ -d "$cleanup_dir" ]; then if [ -n "$result" ]; then - result="$result $log_dir" + result="$result $cleanup_dir" else - result="$log_dir" + result="$cleanup_dir" fi fi done @@ -160,22 +160,22 @@ find_log_dirs_on_mount() { echo "$result" } -# Clean old log files in a directory -# Usage: clean_log_files "/path/to/log/dir" "max_age_days" +# Clean old files in a directory +# Usage: clean_directory_files "/path/to/dir" "max_age_days" # Returns: Number of files deleted and space freed -clean_log_files() { - local log_dir="$1" +clean_directory_files() { + local cleanup_dir="$1" local max_age_days="${2:-7}" local files_deleted=0 local space_freed=0 - if [ ! -d "$log_dir" ]; then - log_message "CLEANUP" "Log directory '$log_dir' not found. Skipping." + if [ ! -d "$cleanup_dir" ]; then + log_message "CLEANUP" "Directory '$cleanup_dir' not found. Skipping." echo "0:0" return fi - # Find and delete old log files + # Find and delete old files while IFS= read -r -d '' file; do if [ -f "$file" ]; then local file_size=$(stat -c%s "$file" 2>/dev/null || echo "0") @@ -184,12 +184,12 @@ clean_log_files() { space_freed=$((space_freed + file_size)) } fi - done < <(find "$log_dir" -type f -mtime +$max_age_days -print0 2>/dev/null) + done < <(find "$cleanup_dir" -type f -mtime +$max_age_days -print0 2>/dev/null) # Also clean empty directories - find "$log_dir" -type d -empty -delete 2>/dev/null + find "$cleanup_dir" -type d -empty -delete 2>/dev/null - log_message "CLEANUP" "Deleted $files_deleted files from '$log_dir', freed $((space_freed / 1024)) KB" + log_message "CLEANUP" "Deleted $files_deleted files from '$cleanup_dir', freed $((space_freed / 1024)) KB" echo "${files_deleted}:${space_freed}" } @@ -205,20 +205,20 @@ auto_cleanup() { return 1 fi - local log_dirs=$(find_log_dirs_on_mount "$mount_point") + local cleanup_dirs=$(find_autodelete_dirs_on_mount "$mount_point") - if [ -z "$log_dirs" ]; then - log_message "CLEANUP" "No log directories configured for mount point '$mount_point'. Skipping cleanup." + if [ -z "$cleanup_dirs" ]; then + log_message "CLEANUP" "No auto-delete directories configured for mount point '$mount_point'. Skipping cleanup." return 1 fi - log_message "CLEANUP" "Starting auto-cleanup for mount point '$mount_point'. Log dirs: $log_dirs" + log_message "CLEANUP" "Starting auto-cleanup for mount point '$mount_point'. Directories: $cleanup_dirs" local total_freed=0 local total_files=0 - for log_dir in $log_dirs; do - local result=$(clean_log_files "$log_dir" "$MAX_LOG_FILE_AGE_DAYS") + for cleanup_dir in $cleanup_dirs; do + local result=$(clean_directory_files "$cleanup_dir" "$MAX_FILE_AGE_DAYS") local files="${result%%:*}" local freed="${result#*:}" total_files=$((total_files + files))