update backup, add systemdb backup

This commit is contained in:
2025-09-22 14:20:07 +02:00
parent aa7dfd7fe0
commit 95e86f3e60
3 changed files with 39 additions and 16 deletions

View File

@@ -11,6 +11,9 @@ HDBSQL_PATH="/usr/sap/hdbclient/hdbsql"
# This key should be configured to connect to the target tenant database.
USER_KEY="CRONKEY"
# hdbuserstore key for the SYSTEMDB user
SYSTEMDB_USER_KEY="SYSTEMDB_KEY"
# --- Backup Settings ---
# The base directory where all backup files and directories will be stored.
@@ -25,6 +28,9 @@ BACKUP_BASE_DIR="/hana/backups/automated"
# 'all' - Performs both the schema export and the tenant backup.
BACKUP_TYPE="all"
# Set to 'true' to also perform a backup of the SYSTEMDB
BACKUP_SYSTEMDB=true
# Schema can be compressed after exporting, decreasing it's size.
COMPRESS_SCHEMA=true

View File

@@ -1,5 +1,5 @@
#!/bin/bash
# Version: 1.0.0
# Version: 1.0.5
# ==============================================================================
# SAP HANA Backup Script
#
@@ -52,7 +52,7 @@ perform_schema_export() {
timestamp=$(date +%Y%m%d_%H%M%S)
local export_base_dir="${BACKUP_BASE_DIR}/schema"
local export_path="${export_base_dir}/${schema_name}_${timestamp}"
local query_export_path="$export_path" # Default path for the EXPORT query
local query_export_path="$export_path"
if [[ "$COMPRESS_SCHEMA" == "true" ]]; then
export_path="${export_base_dir}/tmp/${schema_name}_${timestamp}"
@@ -94,7 +94,7 @@ perform_schema_export() {
fi
}
# NEW: Loops through the schemas in the config file and runs an export for each.
# Loops through the schemas in the config file and runs an export for each.
run_all_schema_exports() {
if [[ -z "$SCHEMA_NAMES" ]]; then
echo " ⚠️ Warning: SCHEMA_NAMES variable is not set in config. Skipping schema export."
@@ -108,17 +108,22 @@ run_all_schema_exports() {
done
}
# Performs a full backup of the tenant database.
perform_tenant_backup() {
echo "⬇️ Starting tenant backup..."
# REFACTORED: Generic function to back up any database (Tenant or SYSTEMDB).
# Arguments: 1:Backup Name (for logging), 2:User Key, 3:Base Directory, 4:Compression Flag
perform_database_backup() {
local backup_name="$1"
local user_key="$2"
local backup_base_dir="$3"
local compress_enabled="$4"
echo "⬇️ Starting ${backup_name} backup..."
local timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
local backup_base_dir="${BACKUP_BASE_DIR}/tenant"
local backup_path_prefix
local backup_target_dir
if [[ "$COMPRESS_TENANT" == "true" ]]; then
if [[ "$compress_enabled" == "true" ]]; then
backup_target_dir="${backup_base_dir}/tmp"
backup_path_prefix="${backup_target_dir}/backup_${timestamp}"
echo " Compression enabled. Using temporary backup path: ${backup_path_prefix}"
@@ -131,13 +136,13 @@ perform_tenant_backup() {
local query="BACKUP DATA USING FILE ('${backup_path_prefix}')"
"$HDBSQL_PATH" -U "$USER_KEY" "$query" > /dev/null 2>&1
"$HDBSQL_PATH" -U "$user_key" "$query" > /dev/null 2>&1
local exit_code=$?
if [[ "$exit_code" -eq 0 ]]; then
echo " ✅ Successfully initiated tenant backup with prefix '${backup_path_prefix}'."
echo " ✅ Successfully initiated ${backup_name} backup with prefix '${backup_path_prefix}'."
if [[ "$COMPRESS_TENANT" == "true" ]]; then
if [[ "$compress_enabled" == "true" ]]; then
local archive_file="${backup_base_dir}/backup_${timestamp}.tar.gz"
echo " 🗜️ Compressing backup files..."
tar -czf "$archive_file" -C "$backup_target_dir" .
@@ -153,7 +158,7 @@ perform_tenant_backup() {
fi
fi
else
echo " ❌ Error: Failed to initiate tenant backup (hdbsql exit code: ${exit_code})."
echo " ❌ Error: Failed to initiate ${backup_name} backup (hdbsql exit code: ${exit_code})."
fi
}
@@ -168,16 +173,28 @@ case "$BACKUP_TYPE" in
run_all_schema_exports
;;
tenant)
perform_tenant_backup
perform_database_backup "Tenant" "$USER_KEY" "${BACKUP_BASE_DIR}/tenant" "$COMPRESS_TENANT"
;;
all)
run_all_schema_exports
perform_tenant_backup
perform_database_backup "Tenant" "$USER_KEY" "${BACKUP_BASE_DIR}/tenant" "$COMPRESS_TENANT"
;;
*)
echo " ❌ Error: Invalid BACKUP_TYPE '${BACKUP_TYPE}' in config. Use 'schema', 'tenant', or 'all'."
;;
esac
# NEW: Check if SYSTEMDB backup is enabled, regardless of BACKUP_TYPE (as long as it's not 'schema' only)
if [[ "$BACKUP_TYPE" == "tenant" || "$BACKUP_TYPE" == "all" ]]; then
if [[ "$BACKUP_SYSTEMDB" == "true" ]]; then
echo "--------------------------------------------------"
if [[ -z "$SYSTEMDB_USER_KEY" ]]; then
echo " ❌ Error: BACKUP_SYSTEMDB is true, but SYSTEMDB_USER_KEY is not set in config."
else
perform_database_backup "SYSTEMDB" "$SYSTEMDB_USER_KEY" "${BACKUP_BASE_DIR}/systemdb" "$COMPRESS_TENANT"
fi
fi
fi
echo "📦 Backup process complete."
echo "👋 Exiting."

View File

@@ -8,7 +8,7 @@ declare -A SCRIPT_PACKAGES
# The version should match the "# Version: x.x.x" line in the main script file.
SCRIPT_PACKAGES["Aurora Suite"]="1.1.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.0|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["Key Manager"]="1.2.1|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh"
SCRIPT_PACKAGES["File Cleaner"]="1.1.0|https://git.technopunk.space/tomi/Scripts/raw/branch/main/clean.sh"
SCRIPT_PACKAGES["HANA Tool"]="1.0.4|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanatool.sh"