diff --git a/aurora/aurora.conf b/aurora/aurora.conf index 8ff280b..64c2dc5 100644 --- a/aurora/aurora.conf +++ b/aurora/aurora.conf @@ -31,6 +31,9 @@ HDBSQL="/usr/sap/NDB/HDB00/exe/hdbsql" # The root directory where post-import SQL scripts are located. SQL_SCRIPTS_ROOT="/usr/sap/NDB/home/tools/sql" +# Number of threads to use for schema export and import. +THREADS=1 + # --- Post-Import Scripts (Optional) --- # A space-separated list of SQL script filenames to run after the import is complete. diff --git a/aurora/aurora.hook.sh b/aurora/aurora.hook.sh new file mode 100644 index 0000000..c8c0cf6 --- /dev/null +++ b/aurora/aurora.hook.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Author: Tomi Eckert +# This script helps to configure aurora.conf + +# Source the aurora.conf to get current values +source aurora.conf + +HDBSQL_PATH_INPUT=$(which hdbsql) + +# Default values if not found +HDBSQL_PATH_INPUT=${HDBSQL_PATH_INPUT:-"/usr/sap/hdbclient/hdbsql"} + +# Calculate default threads (half of available) +TOTAL_THREADS=$(nproc --all) +THREADS_DEFAULT=$((TOTAL_THREADS / 2)) + +# Update aurora.conf +sed -i "s#^HDBSQL_PATH=\".*\"#HDBSQL_PATH=\"$HDBSQL_PATH_INPUT\"#" aurora.conf +sed -i "s#^THREADS=.\"*\"#THREADS=\"$THREADS_DEFAULT\"#" aurora.conf + +echo "aurora.conf updated successfully!" \ No newline at end of file diff --git a/aurora/aurora.sh b/aurora/aurora.sh index 6aa98bc..2af64d6 100644 --- a/aurora/aurora.sh +++ b/aurora/aurora.sh @@ -1,50 +1,41 @@ -#!/bin/sh -# Version: 2.1.0 +#!/bin/bash +# Version: 2.5.0 # Author: Tomi Eckert +# ============================================================================== +# Aurora Refresh Script # -# Purpose: Performs an automated refresh of a SAP HANA schema. It exports a -# production schema and re-imports it under a new name ("Aurora") -# to create an up-to-date, non-production environment for testing. -# Designed to be run via cron, typically in the early morning. -# -# ----------------------------------------------------------------------------- +# Performs an automated refresh of a SAP HANA schema using hanatool.sh. +# It exports a production schema and re-imports it under a new name ("Aurora") +# to create an up-to-date, non-production environment for testing. +# ============================================================================== -# --- Basic Setup --- -# Exit immediately if any command fails or if an unset variable is used. -set -eu +# --- Configuration and Setup --- -# --- Configuration --- -# Load the configuration file located in the same directory as the script. -SCRIPT_DIR=$(dirname "$0") +# Find the script's own directory to locate the config file and hanatool.sh +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) CONFIG_FILE="${SCRIPT_DIR}/aurora.conf" +HANATOOL_PATH="${SCRIPT_DIR}/../hanatool.sh" -if [ ! -f "$CONFIG_FILE" ]; then - echo "❌ FATAL: Configuration file not found at '${CONFIG_FILE}'" >&2 - exit 1 -fi -# shellcheck source=aurora.conf -. "$CONFIG_FILE" - -# --- Validate Configuration --- -if [ ! -x "$HDBSQL" ]; then - echo "❌ FATAL: hdbsql is not found or not executable at '${HDBSQL}'" >&2 +# Check for config file and source it +if [[ -f "$CONFIG_FILE" ]]; then + source "$CONFIG_FILE" +else + echo "❌ Error: Configuration file not found at '${CONFIG_FILE}'" exit 1 fi -# --- Derived Variables (Do Not Edit) --- -TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") +# Check if hanatool.sh executable exists +if [[ ! -x "$HANATOOL_PATH" ]]; then + echo "❌ Error: hanatool.sh not found or not executable at '${HANATOOL_PATH}'" + exit 1 +fi + +# --- Derived Variables --- AURORA_SCHEMA="${SOURCE_SCHEMA}_AURORA" EXPORT_DIR="${BACKUP_BASE_DIR}/${AURORA_SCHEMA}_TEMP_EXPORT" -COMPANY_NAME_BASE=$(echo "${SOURCE_SCHEMA}" | sed 's/^SBO_//' | sed 's/_PROD$//') # --- Main Execution --- -echo -echo "🚀 [$(date "+%T")] Starting Aurora Refresh for '${SOURCE_SCHEMA}'" -echo "--------------------------------------------------------" -echo " Source Schema: ${SOURCE_SCHEMA}" -echo " Target Aurora Schema: ${AURORA_SCHEMA}" -echo " Temp Export Path: ${EXPORT_DIR}" -echo "--------------------------------------------------------" +echo "🚀 Starting Aurora Refresh for '${SOURCE_SCHEMA}' using hanatool.sh..." # 1. Drop the old Aurora schema if it exists. echo "🗑️ Dropping old schema '${AURORA_SCHEMA}' (if it exists)..." @@ -55,21 +46,26 @@ echo "📁 Preparing temporary export directory..." rm -rf "$EXPORT_DIR" mkdir -p "$EXPORT_DIR" -# 3. Export the source schema. -echo "⬇️ Exporting source schema '${SOURCE_SCHEMA}' to binary files..." -"$HDBSQL" -U "$DB_ADMIN_KEY" "EXPORT \"${SOURCE_SCHEMA}\".\"*\" AS BINARY INTO '${EXPORT_DIR}' WITH REPLACE;" >/dev/null -echo " -> Export complete." +# 3. Export the source schema using hanatool.sh +echo "⬇️ Exporting source schema '${SOURCE_SCHEMA}'..." +"$HANATOOL_PATH" "$DB_ADMIN_KEY" export "$SOURCE_SCHEMA" "$EXPORT_DIR" -t "$THREADS" +if [[ $? -ne 0 ]]; then + echo "❌ Error: Export failed." + exit 1 +fi -# 4. Import the data into the new Aurora schema. +# 4. Import the data into the new Aurora schema using hanatool.sh echo "⬆️ Importing data and renaming schema to '${AURORA_SCHEMA}'..." -"$HDBSQL" -U "$DB_ADMIN_KEY" "IMPORT \"${SOURCE_SCHEMA}\".\"*\" FROM '${EXPORT_DIR}' WITH IGNORE EXISTING RENAME SCHEMA \"${SOURCE_SCHEMA}\" TO \"${AURORA_SCHEMA}\";" >/dev/null -echo " -> Import complete." +"$HANATOOL_PATH" "$DB_ADMIN_KEY" import-rename "$SOURCE_SCHEMA" "$AURORA_SCHEMA" "$EXPORT_DIR" -t "$THREADS" +if [[ $? -ne 0 ]]; then + echo "❌ Error: Import failed." + exit 1 +fi # 5. Update company name in CINF and OADM tables. echo "✍️ Updating company name fields in the new schema..." # First, get the original company name from the source schema. -# The query returns a header and the name in quotes. sed gets the second line, tr removes the quotes, xargs trims whitespace. echo " -> Fetching original company name from '${SOURCE_SCHEMA}'..." ORIGINAL_COMPNY_NAME=$("$HDBSQL" -U "$DB_ADMIN_KEY" "SELECT \"CompnyName\" FROM \"${SOURCE_SCHEMA}\".\"CINF\"" | sed -n '2p' | tr -d '"' | xargs) @@ -91,13 +87,11 @@ echo "🔑 Granting ALL privileges on '${AURORA_SCHEMA}' to '${AURORA_USER}'..." echo " -> Privileges granted." # 7. Run post-import SQL scripts, if any are defined. -if [ -n "$POST_IMPORT_SQL" ]; then +if [[ -n "$POST_IMPORT_SQL" ]]; then echo "⚙️ Running post-import SQL scripts..." - # Use word splitting intentionally here - # shellcheck disable=SC2086 for sql_file in $POST_IMPORT_SQL; do full_path="${SQL_SCRIPTS_ROOT}/${sql_file}" - if [ -f "$full_path" ]; then + if [[ -f "$full_path" ]]; then echo " -> Executing: ${sql_file}" "$HDBSQL" -U "$DB_ADMIN_KEY" -I "$full_path" else @@ -114,7 +108,7 @@ rm -rf "$EXPORT_DIR" echo " -> Cleanup complete." echo "--------------------------------------------------------" -echo "✅ [$(date "+%T")] Aurora Refresh finished successfully!" +echo "✅ Aurora Refresh finished successfully!" echo exit 0