Friday, 24 January 2025

Script to take backup of multiple schemas.

#!/bin/bash

# Prompt for necessary inputs
read -p "Enter schemas to backup (comma-separated): " schema_name
read -p "Enter ORACLE_SID: " ORACLE_SID
read -p "Enter ORACLE_HOME path: " ORACLE_HOME

# Input for transferring files to the target server
read -p "Enter target server IP: " DEST_IP
read -p "Enter target directory path: " DEST_PATH

# Date format for filenames
DATEFORMAT=$(date +%Y%m%d%H%M%S)

# Loop through schemas and perform export
IFS=',' read -r -a SCHEMA_ARRAY <<< "$schema_name"
for schema in "${SCHEMA_ARRAY[@]}"; do
  echo "Starting backup for schema $schema at $(date)" >> nohup.out

  # Run expdp command with %U option for multiple dump files
  expdp \"/ as sysdba\" directory=OBACKUP dumpfile=expdp_${schema}_$DATEFORMAT_%U.dmp logfile=expdp_${schema}_$DATEFORMAT.log schemas=$schema parallel=4 cluster=n >> nohup.out 2>&1

  # Check for successful export
  if [ $? -eq 0 ]; then
    echo "Export for $schema completed." >> nohup.out
    # Transfer dump and log files
    scp OBACKUP/expdp_${schema}_$DATEFORMAT_* oracle@$DEST_IP:$DEST_PATH >> nohup.out 2>&1
    if [ $? -eq 0 ]; then
      echo "Files for $schema transferred successfully." >> nohup.out
    else
      echo "File transfer failed for $schema." >> nohup.out
    fi
  else
    echo "Export failed for $schema." >> nohup.out
  fi
done

echo "Backup script completed at $(date)" >> nohup.out

No comments:

Post a Comment