From 20a632eca743be0321a2d67240ac184ad5e9320f Mon Sep 17 00:00:00 2001 From: TomiEckert Date: Thu, 16 Apr 2026 15:38:47 +0200 Subject: [PATCH] add hanamgr option to copy schemas --- hanamgr.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++----- packages.conf | 2 +- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/hanamgr.sh b/hanamgr.sh index 72a6f9c..20811a3 100755 --- a/hanamgr.sh +++ b/hanamgr.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Version: 1.3.1 +# Version: 1.4.0 # Author: Tomi Eckert # ============================================================================== # HANA Database Manager Menu (hanamgr.sh) @@ -286,6 +286,69 @@ do_drop() { fi } +do_copy() { + echo -e "\n\033[1m=== Copy Schema ===\033[0m" + select_schema "$GLOBAL_USER_KEY" + local source_schema="$SELECTED_SCHEMA" + + if [[ -z "$source_schema" ]]; then + echo -e "\033[31m[❌] Error: Source Schema is required.\033[0m" + return + fi + + read -p "Enter Target Schema Name: " target_schema + read -p "Enter Temporary Export Directory Path: " export_path + + local max_threads=$(nproc --all 2>/dev/null || echo 1) + local half_threads=$((max_threads / 2)) + [[ $half_threads -lt 1 ]] && half_threads=1 + + read -p "Number of threads (default: $half_threads, max: $max_threads): " threads + threads=${threads:-$half_threads} + + read -p "Replace existing objects in target schema? (y/N): " replace_opt + + if [[ -z "$GLOBAL_USER_KEY" || -z "$target_schema" || -z "$export_path" ]]; then + echo -e "\033[31m[❌] Error: Key, Target Schema, and Path are required.\033[0m" + return + fi + + # 1. Export + echo -e "\n[⬇️] Step 1/2: Exporting source schema '${source_schema}' to '${export_path}'..." + mkdir -p "$export_path" + local temp_export_dir=$(mktemp -d "${export_path}/copy_export_${source_schema}_XXXXXXXX") + echo "[ℹ️] Using temporary export directory: ${temp_export_dir}" + + local export_query="EXPORT \"${source_schema}\".\"*\" AS BINARY INTO '${temp_export_dir}' WITH REPLACE THREADS ${threads} NO DEPENDENCIES;" + + if ! execute_sql "$GLOBAL_USER_KEY" "$export_query"; then + echo -e "\033[31m[❌] Error: Export phase failed. Aborting copy.\033[0m" + rm -rf "$temp_export_dir" + return + fi + echo -e "\033[32m[✅] Export phase completed successfully.\033[0m" + + # 2. Import-Rename + echo -e "\n[⬆️] Step 2/2: Importing and renaming to '${target_schema}'..." + local import_options="IGNORE EXISTING" + if [[ "$replace_opt" =~ ^[Yy]$ ]]; then + import_options="REPLACE" + fi + import_options="${import_options} RENAME SCHEMA \"${source_schema}\" TO \"${target_schema}\"" + + local import_query="IMPORT \"${source_schema}\".\"*\" AS BINARY FROM '${temp_export_dir}' WITH ${import_options} THREADS ${threads};" + + if execute_sql "$GLOBAL_USER_KEY" "$import_query"; then + echo -e "\033[32m[✅] Successfully copied schema '${source_schema}' to '${target_schema}'.\033[0m" + else + echo -e "\033[31m[❌] Error: Import phase failed.\033[0m" + fi + + # Cleanup + echo "[🧹] Cleaning up temporary files..." + rm -rf "$temp_export_dir" +} + do_rename_db() { echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m" select_schema "$GLOBAL_USER_KEY" @@ -334,11 +397,12 @@ while true; do echo "1) Export Schema" echo "2) Import Schema" echo "3) Import-Rename Schema" - echo "4) Drop Schema" - echo "5) Rename Database (Update Company Name)" - echo "6) Quit" + echo "4) Copy Schema" + echo "5) Drop Schema" + echo "6) Rename Database (Update Company Name)" + echo "7) Quit" echo - read -p "Please select an option (1-6): " choice + read -p "Please select an option (1-7): " choice case $choice in 1) @@ -351,12 +415,15 @@ while true; do do_import "true" ;; 4) - do_drop + do_copy ;; 5) - do_rename_db + do_drop ;; 6) + do_rename_db + ;; + 7) echo "[👋] Exiting." exit 0 ;; diff --git a/packages.conf b/packages.conf index 8bbe5c1..cdc1b83 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.3.1|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.4.0|An interactive command-line menu for managing SAP HANA schemas and databases.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanamgr.sh"