Data Pump Export (expdp) job setup in cron

To set up a full database export using Oracle's Data Pump Export (expdp) utility, you need to:

Here's a step-by-step guide for setting up a full database export and scheduling it with cron.


Step 1: Create a Directory for Data Pump

Before running the expdp command, you need to ensure that the Oracle database has a directory object for storing the backup.

a. Create a Directory in Oracle

Run the following SQL commands to create a directory for the export files and grant permissions to the user who will run the export.

sql

Copy code

SQL> CREATE DIRECTORY datapump_dir AS '/path/to/export/dir';

SQL> GRANT READ, WRITE ON DIRECTORY datapump_dir TO system;


Replace '/path/to/export/dir' with the actual directory path on your system where you want the dump files to be saved.


Step 2: Create the Export Script

Create a shell script that will perform the full database export using the expdp command.

b. Sample Export Script

Create a script (e.g., full_db_export.sh) using your preferred text editor (like vi, nano, etc.).

bash

Copy code

#!/bin/bash


# Define variables

ORACLE_SID=your_sid

ORACLE_HOME=/path/to/oracle/home

EXP_DIR=/path/to/export/dir

DATE=$(date +%F)

LOG_FILE=${EXP_DIR}/expdp_full_${DATE}.log

DUMP_FILE=${EXP_DIR}/full_db_${DATE}.dmp

USER=system

PASS=your_password

DIRECTORY=datapump_dir


# Export environment variables

export ORACLE_SID

export ORACLE_HOME

export PATH=$ORACLE_HOME/bin:$PATH


# Run the Data Pump export

expdp ${USER}/${PASS} FULL=Y DIRECTORY=${DIRECTORY} DUMPFILE=full_db_${DATE}.dmp LOGFILE=expdp_full_${DATE}.log


# Clean up older dump files (optional)

# Find and remove dump files older than 7 days

find ${EXP_DIR} -name "*.dmp" -type f -mtime +7 -exec rm {} \;

find ${EXP_DIR} -name "*.log" -type f -mtime +7 -exec rm {} \;


Explanation:

c. Make the Script Executable

Give the script execute permissions:

bash

Copy code

$ chmod +x /path/to/script/full_db_export.sh



Step 3: Set Up a Cron Job to Automate the Backup

Now that you have the export script ready, you can set up a cron job to run it at a scheduled time.

d. Edit the Crontab

To schedule the script to run, say every day at 2:00 AM, follow these steps:

Open the crontab editor for the user:
bash
Copy code
$ crontab -e


Add the following cron job:
bash
Copy code
0 2 * * * /path/to/script/full_db_export.sh >> /path/to/script/cron_log.txt 2>&1


e. Save and Exit

Save the crontab file and exit. The script will now run automatically at the specified time.


Step 4: Verify the Setup

After setting up the cron job, verify that it’s working properly by checking the following:


Optional: Monitor the Script with Email Alerts

If you want to receive email alerts in case of failures, you can configure the system to send emails from cron jobs.

Set the environment variable for email notifications by adding the following to the top of your script:
bash
Copy code
MAILTO=your_email@example.com


Summary

This setup ensures that your Oracle database is regularly backed up using expdp, and the script is automated using cron. You can customize the script to suit your retention policy, adjust the frequency in cron, and monitor the job through logs or email alerts.