Skip to content

Instantly share code, notes, and snippets.

@garrettdieckmann
Last active November 8, 2021 03:16
Show Gist options
  • Save garrettdieckmann/9218543 to your computer and use it in GitHub Desktop.
Save garrettdieckmann/9218543 to your computer and use it in GitHub Desktop.
RMAN Incremental backup via BASH Cron job
#!/bin/bash
# Perform RMAN backup
# Expected usage: f_perform_backup DB LEVEL
# DB: Database SID to pass to RMAN
# LEVEL: full/inc - full is full incremental, inc is differential incremental
f_perform_backup() {
export LEVEL="$1"
echo "Performing backup on $ORACLE_SID."
echo "Performing $LEVEL backup."
# Run RMAN
$ORACLE_HOME/bin/rman <<RMAN
connect target /
run {
delete noprompt obsolete;
allocate channel d1 device type disk format '/oracle/data/backup/%d_%u';
backup
incremental level = $LEVEL
database include current controlfile;
backup archivelog all delete input;
}
exit;
RMAN
echo "backup completed on $ORACLE_SID, at: $(date)"
}
# Main
# Expected usage: ./backup_db DBNAME BACKUP_TYPE
# DBNAME: SID of Oracle database
# BACKUP_TYPE: Incremental backup, or full backup
f_main() {
echo "ORACLE_HOME set to: $ORACLE_HOME"
if [ -z "$ORACLE_HOME" ]; then
echo "\$ORACLE_HOME isn't set. Exiting."
exit 133
fi
# which database?
if [ -z "$1" ]; then
echo "\$ORACLE_SID not provided. Exiting."
exit 133
else
export ORACLE_SID=$1
fi
# incremental or full?
if [ -z "$2" ]; then
echo "No backup method specified. Exiting."
exit 133
else
if [ "$2" == "inc" ]; then
f_perform_backup "1"
elif [ "$2" == "full" ]; then
f_perform_backup "0"
else
echo "Invalid backup method specified. Exiting."
fi
fi
}
# Source environment
source $HOME/.bash_profile
# Colors
RESTORE='\033[0m'
PURPLE='\033[00;35m'
# Call main, pass parameters
echo "Running script on: $(date)."
f_main "$1" "$2"
[oracle@database scripts]$ crontab -l
# Perform full backup on database Sunday at 3:05 am
5 3 * * 0 /$HOME/scripts/backup_db database_name full > /$HOME/scripts/logs/full_backup.log
# Perform incremental backups on database Mon-Sat at 3:05 am
5 3 * * 1-6 /$HOME/scripts/backup_db database_name inc > /$HOME/scripts/logs/inc_backup.log
@cfriedli
Copy link

Thanks for sharing your script. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment