Tuesday, February 25, 2025

datapump

 Automating Oracle Data Pump Exports and Imports Using Shell Scripts

######################################################
Oracle Data Pump (expdp and impdp) is a powerful utility for high-speed data export and import operations.

1️⃣ Shell Script for Automating Data Pump Exports (expdp)
####################################################
#!/bin/bash

# Set environment variables
export ORACLE_SID=VLAX1DB
export ORACLE_HOME=/u01/app/oracle/product/19.3.0.0
export PATH=$ORACLE_HOME/bin:$PATH
export BACKUP_DIR=/home/oracle/dpump_backup
export LOG_DIR=/home/oracle/dpump_logs
export EMAIL="your_email@example.com"

# Ensure backup and log directories exist
mkdir -p $BACKUP_DIR
mkdir -p $LOG_DIR

# Generate timestamp
DATE=$(date +%Y%m%d_%H%M%S)

# Set dump file and log file names
DUMPFILE=expdp_backup_${DATE}.dmp
LOGFILE=$LOG_DIR/expdp_backup_${DATE}.log

# Run Data Pump export
expdp system/password@VLAX1DB schemas=HR directory=DPUMP_DIR dumpfile=$DUMPFILE logfile=$(basename $LOGFILE)

# Check for success or failure
if grep -q "successfully completed" "$LOGFILE"; then
SUBJECT="Oracle Data Pump Export SUCCESS - $DATE"
MESSAGE="Oracle Data Pump export completed successfully. Dump file: $DUMPFILE"
else
SUBJECT="Oracle Data Pump Export FAILED - $DATE"
MESSAGE="Oracle Data Pump export encountered an error. Check log file: $LOGFILE"
fi

# Send email notification
echo "$MESSAGE" | mailx -s "$SUBJECT" -a "$LOGFILE" "$EMAIL"

# Clean up old backups (older than 7 days)
find $BACKUP_DIR -name "expdp_backup_*.dmp" -mtime +7 -exec rm {} \;
find $LOG_DIR -name "expdp_backup_*.log" -mtime +7 -exec rm {} \;

echo "Export process completed."
#########################################################
2️⃣ Shell Script for Automating Data Pump Imports (impdp)
######################################################
#!/bin/bash

# Set environment variables
export ORACLE_SID=VLAX1DB
export ORACLE_HOME=/u01/app/oracle/product/19.3.0.0
export PATH=$ORACLE_HOME/bin:$PATH
export BACKUP_DIR=/home/oracle/dpump_backup

# Find the latest dump file
LATEST_DUMP=$(ls -t $BACKUP_DIR/expdp_backup_*.dmp | head -n 1)

# Check if a dump file exists
if [ -z "$LATEST_DUMP" ]; then
echo "No dump file found!"
exit 1
fi

# Generate log file name
LOGFILE=impdp_restore_$(date +%Y%m%d_%H%M%S).log

# Run Data Pump import
impdp system/password@VLAX1DB schemas=HR directory=DPUMP_DIR dumpfile=$(basename $LATEST_DUMP) logfile=$LOGFILE remap_schema=HR:HR_NEW

echo "Import completed successfully!"
######################################################
3️⃣ Sending Email Notifications
If your system does not have mail, install and configure mailx:
For Oracle Linux/RHEL:
sudo yum install mailx -y
####################################################


Thanks for your post it was so creative but you can managed it with Ansible so easier.

No comments:

Post a Comment

 https://www.linkedin.com/pulse/building-real-time-database-monitoring-dashboard-oracle-khaleeq-tpwxf/