Files
Scripts/hanamgr.sh
T
2026-04-16 15:20:17 +02:00

343 lines
11 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Version: 1.1.0
# Author: Tomi Eckert
# ==============================================================================
# HANA Database Manager Menu (hanamgr.sh)
#
# An interactive command-line menu for managing SAP HANA schemas.
# Provides functionality to Export, Import, Import-Rename, Drop schemas,
# and Rename the B1 company name within a database.
# ==============================================================================
# --- Configuration ---
# Assuming hdbsql is in the environment PATH
HDBSQL_CMD="hdbsql"
# --- Helper Functions ---
# Function to print a separator
print_separator() {
echo "--------------------------------------------------------"
}
# Function to check if hdbsql is available
check_hdbsql() {
if ! command -v "$HDBSQL_CMD" &> /dev/null; then
echo -e "\033[31m[❌] Error: '$HDBSQL_CMD' command not found. Ensure it is in your PATH.\033[0m"
exit 1
fi
}
# Function to execute a SQL query and capture the exit code
execute_sql() {
local key="$1"
local query="$2"
local output
output=$("$HDBSQL_CMD" -U "$key" "$query" 2>&1)
local exit_code=$?
if [ $exit_code -eq 0 ]; then
return 0
else
echo "$output"
return $exit_code
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
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}
read -p "Compress output as tar.gz? (y/N): " compress
if [[ -z "$user_key" || -z "$schema_name" || -z "$target_path" ]]; then
echo -e "\033[31m[❌] Error: Key, Schema, and Path are required.\033[0m"
return
fi
echo -e "\n[⬇️] Starting export of schema '${schema_name}'..."
local export_dir="$target_path"
if [[ "$compress" =~ ^[Yy]$ ]]; then
export_dir=$(mktemp -d "${target_path}/export_${schema_name}_XXXXXXXX")
echo "[️] Using temporary export directory: ${export_dir}"
else
mkdir -p "$export_dir"
fi
local query="EXPORT \"${schema_name}\".\"*\" AS BINARY INTO '${export_dir}' WITH REPLACE THREADS ${threads} NO DEPENDENCIES;"
if execute_sql "$user_key" "$query"; then
echo -e "\033[32m[✅] Successfully exported schema '${schema_name}'.\033[0m"
if [[ "$compress" =~ ^[Yy]$ ]]; then
local archive_file="${target_path}/${schema_name}_export_$(date +%Y%m%d_%H%M%S).tar.gz"
echo "[📦] Compressing export to ${archive_file}..."
if command -v pigz &> /dev/null; then
tar -I "pigz -p $threads" -cf "$archive_file" -C "$(dirname "$export_dir")" "$(basename "$export_dir")"
else
tar -czf "$archive_file" -C "$(dirname "$export_dir")" "$(basename "$export_dir")"
fi
if [ $? -eq 0 ]; then
echo -e "\033[32m[✅] Compression successful.\033[0m"
rm -rf "$export_dir"
else
echo -e "\033[31m[❌] Error: Compression failed.\033[0m"
fi
fi
else
echo -e "\033[31m[❌] Error: Export failed.\033[0m"
if [[ "$compress" =~ ^[Yy]$ ]]; then rm -rf "$export_dir"; fi
fi
}
do_import() {
local rename_mode="$1"
if [[ "$rename_mode" == "true" ]]; then
echo -e "\n\033[1m=== Import & Rename Schema ===\033[0m"
else
echo -e "\n\033[1m=== Import Schema ===\033[0m"
fi
read -p "Enter HDBUSERSTORE Key: " user_key
read -p "Enter Source Schema Name (as it was exported): " schema_name
local new_schema_name=""
if [[ "$rename_mode" == "true" ]]; then
read -p "Enter NEW Target Schema Name: " new_schema_name
if [[ -z "$new_schema_name" ]]; then
echo -e "\033[31m[❌] Error: New Schema Name is required for renaming.\033[0m"
return
fi
fi
read -p "Enter Source Path (Directory or .tar.gz): " source_path
read -p "Number of threads (default: 1): " threads
threads=${threads:-1}
read -p "Replace existing objects? (y/N): " replace_opt
if [[ -z "$user_key" || -z "$schema_name" || -z "$source_path" ]]; then
echo -e "\033[31m[❌] Error: Key, Schema, and Path are required.\033[0m"
return
fi
echo -e "\n[⬆️] Starting schema import..."
local import_dir="$source_path"
local cleanup_temp=false
# Handle compressed archives
if [[ -f "$source_path" && "$source_path" == *.tar.gz ]]; then
import_dir=$(mktemp -d "/tmp/import_${schema_name}_XXXXXXXX")
cleanup_temp=true
echo "[️] Decompressing archive to ${import_dir}..."
if command -v pigz &> /dev/null; then
tar -I "pigz -p $threads" -xf "$source_path" -C "$import_dir" --strip-components=1
else
tar -xzf "$source_path" -C "$import_dir" --strip-components=1
fi
if [ $? -ne 0 ]; then
echo -e "\033[31m[❌] Error: Failed to decompress archive.\033[0m"
rm -rf "$import_dir"
return
fi
elif [[ ! -d "$import_dir" ]]; then
echo -e "\033[31m[❌] Error: Path is neither a directory nor a valid .tar.gz archive.\033[0m"
return
fi
local import_options="IGNORE EXISTING"
if [[ "$replace_opt" =~ ^[Yy]$ ]]; then
import_options="REPLACE"
fi
if [[ "$rename_mode" == "true" ]]; then
import_options="${import_options} RENAME SCHEMA \"${schema_name}\" TO \"${new_schema_name}\""
fi
local query="IMPORT \"${schema_name}\".\"*\" AS BINARY FROM '${import_dir}' WITH ${import_options} THREADS ${threads};"
if execute_sql "$user_key" "$query"; then
echo -e "\033[32m[✅] Successfully imported schema.\033[0m"
else
echo -e "\033[31m[❌] Error: Import failed.\033[0m"
fi
if [[ "$cleanup_temp" == "true" ]]; then
echo "[🧹] Cleaning up temporary files..."
rm -rf "$import_dir"
fi
}
do_drop() {
echo -e "\n\033[1m=== Drop Schema ===\033[0m"
read -p "Enter HDBUSERSTORE Key: " user_key
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"
return
fi
echo -e "\033[31m[⚠️] WARNING: You are about to completely drop the schema '${schema_name}'.\033[0m"
read -p "Are you absolutely sure? Type 'YES' to confirm: " confirm
if [[ "$confirm" == "YES" ]]; then
echo "[🗑️] Dropping schema '${schema_name}'..."
local query="DROP SCHEMA \"${schema_name}\" CASCADE"
if execute_sql "$user_key" "$query"; then
echo -e "\033[32m[✅] Schema successfully dropped.\033[0m"
else
echo -e "\033[31m[❌] Error: Failed to drop schema.\033[0m"
fi
else
echo "[🤷] Operation cancelled."
fi
}
do_rename_db() {
echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m"
read -p "Enter HDBUSERSTORE Key: " user_key
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
echo -e "\033[31m[❌] Error: Key, Schema Name, and New Company Name are required.\033[0m"
return
fi
echo -e "\n[✍️] Updating company name for '${schema_name}' to '${new_compny_name}'..."
# Update CINF
echo " -> Updating CINF table..."
local q_cinf="UPDATE \"${schema_name}\".CINF SET \"CompnyName\" = '${new_compny_name}';"
execute_sql "$user_key" "$q_cinf" > /dev/null
# Update OADM
echo " -> Updating OADM table..."
local q_oadm="UPDATE \"${schema_name}\".OADM SET \"CompnyName\" = '${new_compny_name}', \"PrintHeadr\" = '${new_compny_name}';"
execute_sql "$user_key" "$q_oadm" > /dev/null
echo -e "\033[32m[✅] Database renamed successfully.\033[0m"
}
# --- Main Menu Loop ---
check_hdbsql
while true; do
echo
echo "========================================================"
echo " HANA Database Manager Menu "
echo "========================================================"
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
read -p "Please select an option (1-6): " choice
case $choice in
1)
do_export
;;
2)
do_import "false"
;;
3)
do_import "true"
;;
4)
do_drop
;;
5)
do_rename_db
;;
6)
echo "[👋] Exiting."
exit 0
;;
*)
echo -e "\033[31m[⚠️] Invalid option. Please try again.\033[0m"
;;
esac
echo
read -n 1 -s -r -p "Press any key to continue..."
done