rewrite aurora

This commit is contained in:
2025-09-24 21:40:55 +02:00
parent 05bd96e8b2
commit d3d7307076
3 changed files with 125 additions and 120 deletions

View File

@@ -1,31 +1,39 @@
# Configuration for the HANA Aurora Refresh Script # Configuration for the Aurora Refresh Script (aurora_refresh.sh)
# Place this file in the same directory as the aurora.sh script. # Place this file in the same directory as the script.
# --- Main Settings --- # --- Main Settings ---
# The source production schema to be copied. # The source production schema to be copied.
SCHEMA="SBO_DEMO" # Example: "SBO_COMPANY_PROD"
SOURCE_SCHEMA="SBODEMOHU"
# The user who will be granted privileges on the new Aurora schema. # The HANA user that will be granted read/write access to the new Aurora schema.
AURORA_SCHEMA_USER="B1_53424F5F4348494D5045585F4155524F5241_RW" # This is typically a technical user for the application.
# Example: "B1_..._RW"
AURORA_USER="B1_XXXXXXXXX_RW"
# The database user for performing backup and administrative tasks. # The secure user store key for the HANA database user with privileges to
BACKOP_USER="CRONKEY" # perform EXPORT, IMPORT, DROP SCHEMA, and GRANT commands (e.g., SYSTEM).
# Using a key (hdbuserstore) is more secure than hardcoding a password.
# Example: "CRONKEY"
DB_ADMIN_KEY="CRONKEY"
# --- Paths and Files --- # --- Paths ---
# The base directory for storing the temporary schema export. # The base directory where the temporary schema export folder will be created.
BACKUP_DIR="/hana/shared/backup/schema" # Ensure the <sid>adm user has write permissions here.
BACKUP_BASE_DIR="/hana/shared/backup/schema"
# The full path to the HANA hdbsql executable. # The full path to the HANA hdbsql executable.
HDBSQL="/usr/sap/NDB/HDB00/exe/hdbsql" 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"
# --- Post-Import Scripts --- # --- Post-Import Scripts (Optional) ---
# The root directory where the SQL script and its associated files are located. # A space-separated list of SQL script filenames to run after the import is complete.
SQL_ROOT="/usr/sap/NDB/home/tools" # The script will look for these files inside the SQL_SCRIPTS_ROOT directory.
# Leave empty ("") if no scripts are needed.
# A space-separated list of SQL script files to run after the import is complete. # Example: "update_user_emails.sql cleanup_tables.sql"
# These scripts should be located in the SCRIPT_ROOT directory. POST_IMPORT_SQL=""
POST_SQL=""

View File

@@ -1,115 +1,112 @@
#!/bin/sh #!/bin/sh
# Version: 1.2.6 # Version: 2.0.0
# Exit immediately if a command exits with a non-zero status. #
set -e # Aurora Refresh Script v2.1
#
# 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.
#
# -----------------------------------------------------------------------------
# === SETUP === # --- Basic Setup ---
# Determine script's directory and source the configuration file. # Exit immediately if any command fails or if an unset variable is used.
set -eu
# --- Configuration ---
# Load the configuration file located in the same directory as the script.
SCRIPT_DIR=$(dirname "$0") SCRIPT_DIR=$(dirname "$0")
CONFIG_FILE="${SCRIPT_DIR}/aurora.conf" CONFIG_FILE="${SCRIPT_DIR}/aurora.conf"
if [ ! -f "$CONFIG_FILE" ]; then if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Configuration file not found at '${CONFIG_FILE}'" >&2 echo "FATAL: Configuration file not found at '${CONFIG_FILE}'" >&2
exit 1 exit 1
fi fi
# shellcheck source=aurora.conf # shellcheck source=aurora.conf
. "$CONFIG_FILE" . "$CONFIG_FILE"
# === DERIVED VARIABLES === # --- Validate Configuration ---
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") if [ ! -x "$HDBSQL" ]; then
AURORA="${SCHEMA}_AURORA" echo "❌ FATAL: hdbsql is not found or not executable at '${HDBSQL}'" >&2
AURORA_TEMP_DIR="${BACKUP_DIR}/${AURORA}" exit 1
COMPNYNAME="${SCHEMA#SBO_}"
[[ "$COMPNYNAME" == *_PROD ]] && COMPNYNAME="${COMPNYNAME%_PROD}" # Remove _PROD suffix if it exists
# === FUNCTIONS ===
run_sql() {
echo " Executing: $1"
"$HDBSQL" -U "${BACKOP_USER}" "$1" >/dev/null
}
show_info() {
echo "Source Schema: ${SCHEMA}"
echo "Target Schema: ${AURORA}"
echo "Target Schema User: ${AURORA_SCHEMA_USER}"
echo "Company Name: ${COMPNYNAME}"
echo "Export Directory: ${AURORA_TEMP_DIR}"
}
usage() {
echo "Usage: $0 [--info]"
echo " --info : Show configuration information."
echo " (No argument) : Drop, export, import, grant privileges, and run post-scripts."
}
export_schema() {
echo "⬇️ Starting schema export for '${SCHEMA}'..."
mkdir -p "$AURORA_TEMP_DIR"
run_sql "EXPORT \"${SCHEMA}\".\"*\" AS BINARY INTO '$AURORA_TEMP_DIR' WITH REPLACE;"
echo "✅ Schema export completed."
}
import_and_rename() {
echo "⬆️ Starting import and rename to '${AURORA}'..."
run_sql "IMPORT \"${SCHEMA}\".\"*\" FROM '$AURORA_TEMP_DIR' WITH IGNORE EXISTING RENAME SCHEMA \"${SCHEMA}\" TO \"${AURORA}\";"
echo " Updating company name fields..."
"$HDBSQL" -U "${BACKOP_USER}" -c ";" -I - <<EOF
UPDATE \"${AURORA}\".CINF SET \"CompnyName\"='AURORA ${COMPNYNAME} ${TIMESTAMP}';
UPDATE \"${AURORA}\".OADM SET \"CompnyName\"='AURORA ${COMPNYNAME} ${TIMESTAMP}';
UPDATE \"${AURORA}\".OADM SET \"PrintHeadr\"='AURORA ${COMPNYNAME} ${TIMESTAMP}';
EOF
echo "✅ Import and rename completed."
}
grant_privileges() {
echo "🔑 Granting privileges on '${AURORA}' to '${AURORA_SCHEMA_USER}'..."
run_sql "GRANT ALL PRIVILEGES ON SCHEMA \"${AURORA}\" TO \"${AURORA_SCHEMA_USER}\";"
echo "✅ Privileges granted."
}
drop_aurora_schema() {
echo "🗑️ Dropping existing '${AURORA}' schema..."
run_sql "DROP SCHEMA \"${AURORA}\" CASCADE;" 2>/dev/null || echo "⚠️ Could not drop schema '${AURORA}'. It might not exist." >&2
echo "✅ Old schema dropped."
}
run_post_scripts() {
echo "⚙️ Running post-import SQL scripts: ${POST_SQL}..."
for sql_file in $POST_SQL; do
echo " - Running script: ${sql_file}"
"$HDBSQL" -U "${BACKOP_USER}" -I "${SCRIPT_ROOT}/${sql_file}"
done
echo "✅ All post-import scripts completed."
}
cleanup_exported_files() {
echo "🧹 Cleaning up exported files from '${AURORA_TEMP_DIR}'..."
rm -rf "$AURORA_TEMP_DIR"
echo "✅ Exported files cleaned up."
}
# === SCRIPT EXECUTION ===
if [ $# -eq 0 ]; then
echo "🚀 Starting 'complete' operation (default)..."
drop_aurora_schema
export_schema
import_and_rename
grant_privileges
run_post_scripts
cleanup_exported_files
echo "🎉 'Complete' operation finished successfully!"
exit 0
fi fi
case "$1" in # --- Derived Variables (Do Not Edit) ---
--info) TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
show_info 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$//')
echo "❌ Error: Invalid argument '$1'." >&2
usage # --- Main Execution ---
exit 1 echo
;; echo "🚀 [$(date "+%T")] Starting Aurora Refresh for '${SOURCE_SCHEMA}'"
esac echo "--------------------------------------------------------"
echo " Source Schema: ${SOURCE_SCHEMA}"
echo " Target Aurora Schema: ${AURORA_SCHEMA}"
echo " Temp Export Path: ${EXPORT_DIR}"
echo "--------------------------------------------------------"
# 1. Drop the old Aurora schema if it exists.
echo "🗑️ Dropping old schema '${AURORA_SCHEMA}' (if it exists)..."
"$HDBSQL" -U "$DB_ADMIN_KEY" "DROP SCHEMA \"${AURORA_SCHEMA}\" CASCADE" >/dev/null 2>&1 || echo " -> Schema did not exist. Continuing."
# 2. Prepare the temporary export directory.
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."
# 4. Import the data into the new Aurora schema.
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."
# 5. Update company name in CINF and OADM tables.
echo "✍️ Updating company name fields in the new schema..."
echo " -> Updating CINF table..."
"$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".CINF SET \"CompnyName\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}';" >/dev/null
echo " -> Updating OADM table..."
"$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".OADM SET \"CompnyName\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}', \"PrintHeadr\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}';" >/dev/null
echo " -> Company info updated."
# 6. Grant privileges to the read/write user.
echo "🔑 Granting ALL privileges on '${AURORA_SCHEMA}' to '${AURORA_USER}'..."
"$HDBSQL" -U "$DB_ADMIN_KEY" "GRANT ALL PRIVILEGES ON SCHEMA \"${AURORA_SCHEMA}\" TO \"${AURORA_USER}\";" >/dev/null
echo " -> Privileges granted."
# 7. Run post-import SQL scripts, if any are defined.
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
echo " -> Executing: ${sql_file}"
"$HDBSQL" -U "$DB_ADMIN_KEY" -I "$full_path"
else
echo " -> ⚠️ WARNING: Script not found: ${full_path}" >&2
fi
done
else
echo " No post-import SQL scripts to run."
fi
# 8. Clean up the temporary export files.
echo "🧹 Cleaning up temporary directory '${EXPORT_DIR}'..."
rm -rf "$EXPORT_DIR"
echo " -> Cleanup complete."
echo "--------------------------------------------------------"
echo "✅ [$(date "+%T")] Aurora Refresh finished successfully!"
echo
exit 0

View File

@@ -7,7 +7,7 @@
declare -A SCRIPT_PACKAGES declare -A SCRIPT_PACKAGES
# The version should match the "# Version: x.x.x" line in the main script file. # The version should match the "# Version: x.x.x" line in the main script file.
SCRIPT_PACKAGES["Aurora Suite"]="1.2.6|https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.conf" SCRIPT_PACKAGES["Aurora Suite"]="2.0.0|https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.conf"
SCRIPT_PACKAGES["Backup Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.conf" SCRIPT_PACKAGES["Backup Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.conf"
SCRIPT_PACKAGES["Monitor Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.conf" SCRIPT_PACKAGES["Monitor Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.conf"
SCRIPT_PACKAGES["Key Manager"]="1.2.1|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh" SCRIPT_PACKAGES["Key Manager"]="1.2.1|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh"