diff --git a/hanamgr.sh b/hanamgr.sh index 0a04740..af76732 100755 --- a/hanamgr.sh +++ b/hanamgr.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Version: 1.0.0 +# Version: 1.1.0 # Author: Tomi Eckert # ============================================================================== # HANA Database Manager Menu (hanamgr.sh) @@ -45,12 +45,73 @@ execute_sql() { fi } +# Function to interactively select a schema from the database +# Sets the global variable SELECTED_SCHEMA +select_schema() { + local user_key="$1" + SELECTED_SCHEMA="" + + echo "[🔎] Fetching available schemas..." + local query="SELECT SCHEMA_NAME FROM SCHEMAS WHERE SCHEMA_OWNER = 'SYSTEM' AND SCHEMA_NAME NOT IN ('_SYS_SECURITY', 'IFSERV', 'B1if', 'SYSTEM', 'RSP');" + + local raw_output + raw_output=$("$HDBSQL_CMD" -U "$user_key" "$query" 2>/dev/null) + + if [ $? -ne 0 ]; then + echo -e "\033[31m[❌] Error: Failed to fetch schemas. Check your HDBUSERSTORE key.\033[0m" + # Fallback to manual entry + read -p "Enter Schema Name manually: " SELECTED_SCHEMA + return 1 + fi + + local schemas=() + while IFS= read -r line; do + if [[ -z "$line" || "$line" == SCHEMA_NAME* || "$line" == *"rows selected"* || "$line" == *"row selected"* || "$line" == *"overall time"* || "$line" == -* ]]; then + continue + fi + local clean_name + clean_name=$(echo "$line" | tr -d '"' | xargs) + if [[ -n "$clean_name" ]]; then + schemas+=("$clean_name") + fi + done <<< "$raw_output" + + if [ ${#schemas[@]} -eq 0 ]; then + echo -e "\033[33m[⚠️] No eligible schemas found in the database.\033[0m" + # Fallback to manual + read -p "Enter Schema Name manually: " SELECTED_SCHEMA + return 0 + fi + + echo -e "\n\033[1mAvailable Schemas:\033[0m" + for i in "${!schemas[@]}"; do + echo "$((i+1))) ${schemas[$i]}" + fi + echo "0) Enter manually" + echo + + local choice + while true; do + read -p "Select a schema (0-${#schemas[@]}): " choice + if [[ "$choice" == "0" ]]; then + read -p "Enter Schema Name manually: " SELECTED_SCHEMA + break + elif [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#schemas[@]}" ]; then + SELECTED_SCHEMA="${schemas[$((choice-1))]}" + break + else + echo -e "\033[31m[❌] Invalid selection. Please try again.\033[0m" + fi + done +} + # --- Operation Functions --- do_export() { echo -e "\n\033[1m=== Export Schema ===\033[0m" read -p "Enter HDBUSERSTORE Key: " user_key - read -p "Enter Schema Name to export: " schema_name + select_schema "$user_key" + local schema_name="$SELECTED_SCHEMA" read -p "Enter Target Directory Path: " target_path read -p "Number of threads (default: 1): " threads threads=${threads:-1} @@ -182,7 +243,8 @@ do_import() { do_drop() { echo -e "\n\033[1m=== Drop Schema ===\033[0m" read -p "Enter HDBUSERSTORE Key: " user_key - read -p "Enter Schema Name to DROP: " schema_name + select_schema "$user_key" + local schema_name="$SELECTED_SCHEMA" if [[ -z "$user_key" || -z "$schema_name" ]]; then echo -e "\033[31m[❌] Error: Key and Schema Name are required.\033[0m" @@ -208,7 +270,8 @@ do_drop() { do_rename_db() { echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m" read -p "Enter HDBUSERSTORE Key: " user_key - read -p "Enter Schema Name to update: " schema_name + select_schema "$user_key" + local schema_name="$SELECTED_SCHEMA" read -p "Enter NEW Company Name: " new_compny_name if [[ -z "$user_key" || -z "$schema_name" || -z "$new_compny_name" ]]; then diff --git a/packages.conf b/packages.conf index 335579d..fe79408 100644 --- a/packages.conf +++ b/packages.conf @@ -16,4 +16,4 @@ SCRIPT_PACKAGES["monitor"]="Monitor Suite|1.3.1|Scripts for monitoring system he SCRIPT_PACKAGES["keymanager"]="Key Manager|1.2.3|A utility for managing HDB user keys for SAP HANA.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/keymanager.sh" SCRIPT_PACKAGES["cleaner"]="File Cleaner|1.1.0|A simple script to clean up temporary files and logs.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/cleaner.sh" SCRIPT_PACKAGES["hanatool"]="HANA Tool|1.6.0|A command-line tool for various SAP HANA administration tasks.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanatool.sh" -SCRIPT_PACKAGES["hanamgr"]="HANA Manager UI|1.0.0|An interactive command-line menu for managing SAP HANA schemas and databases.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanamgr.sh" +SCRIPT_PACKAGES["hanamgr"]="HANA Manager UI|1.1.0|An interactive command-line menu for managing SAP HANA schemas and databases.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanamgr.sh"