Shell Script for RMAN Backup

Few days back  i devoted my time in creating a shell script to take RMAN backup for a 2-node RAC database on Sun Solaris.Both the datafiles and the archive logfiles(for both the instances) are on a shared SAN box. Below is the script :-

ORACLE_HOME=/projects/product/10.2.0/db_1
export ORACLE_HOME
ORACLE_SID=test1
export ORACLE_SID
PATH=$PATH:$ORACLE_HOME/bin
export PATH

set -x

RMAN_BACKUP=/projects/test_backup
Rman_Log=$RMAN_BACKUP/"$ORACLE_SID"_backup_log

touch $Rman_Log

echo "\n\n ****RMAN FULL BACKUP****" >> $Rman_Log
echo "\n rman full startup time: `date`" >> $Rman_Log
DD=`date +%d%m%y`
mkdir /backup/rman/datafiles_backup/bkp_${DD}
mkdir /backup/rman/controlfile_backup/bkp_${DD}
mkdir /backup/rman/arch_backup/bkp_${DD}

sqlplus -s "sys/xxxxxx@TEST1 as sysdba" <<EOF  >> $Rman_Log
set feedback off;
alter system archive log current
/
set serveroutput on
declare
x number;
y number;
begin
select max(SEQUENCE#) into x from gv\$archived_log where thread#=1;
dbms_output.put_line('The Strat Sequence number on instance 1 = '||x||'');
sys.dbms_system.ksdwrt(2,'Note to DBA : On Instance 1 RMAN Backup Starts at '||to_char(x)||' on '||to_char(sysdate,'DD/MON/YYYY HH:MM:MI'));
select max(SEQUENCE#) into y from gv\$archived_log where thread#=2;
dbms_output.put_line('The Strat Sequence number on instance 2 = '||y||'');
sys.dbms_system.ksdwrt(2,'Note to DBA : On Instance 2 RMAN Backup Starts at '||to_char(y)||' on '||to_char(sysdate,'DD/MON/YYYY HH:MM:MI'));
end;
/
exit
EOF
rman target / nocatalog log=$RMAN_BACKUP/rman_fullbackup_${DD}.log << EOF1
change archivelog all crosscheck;
run {
CONFIGURE RETENTION POLICY TO REDUNDANCY 1;
CONFIGURE DEVICE TYPE DISK PARALLELISM 2 BACKUP TYPE TO BACKUPSET;
backup as compressed backupset incremental level 0 database tag 'TEST_FULL_BACKUP' format '/backup/rman/datafiles_backup/bkp_${DD}/%d_%s_%p';
backup as compressed backupset archivelog all not backed up 1 times FORMAT '/backup/rman/arch_backup/bkp_${DD}/ARCH_%d_%s_%p';
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO  '/ora_backup/rman/controlfile_backup/bkp_${DD}/%F';
delete noprompt obsolete;
delete noprompt archivelog all completed before 'sysdate-2';
}
EXIT;
EOF1

sqlplus -s "sys/xxxxxxx@TEST1 as sysdba" <<EOF2 >> $Rman_Log
set feedback off;
alter system archive log current
/
set serveroutput on
declare
x number;
y number;
begin
select max(SEQUENCE#+1) into x from gv\$archived_log where thread#=1;
dbms_output.put_line('The Finished Sequence number on instance 1 = '||x||'');
sys.dbms_system.ksdwrt(2,'Note to DBA : On Instance 1 RMAN Backup Finished at '||to_char(x)||' on '||to_char(sysdate,'DD/MON/YYYY HH:MM:MI'));
select max(SEQUENCE#+1) into y from gv\$archived_log where thread#=2;
dbms_output.put_line('The Finished Sequence number on instance 2 = '||y||'');
sys.dbms_system.ksdwrt(2,'Note to DBA : On Instance 2 RMAN Backup Finished at '||to_char(y)||' on '||to_char(sysdate,'DD/MON/YYYY HH:MM:MI'));
end;
/
exit
EOF2

echo "\n rman fullbackup end time: `date`" >> $Rman_Log

Some Features:-

1. Two log files will be generated.One with the name “$ORACLE_SID_backup_log” and the other with “rman_fullbackup_${DD}.log” (DD will be the current date).The first log file ($ORACLE_SID_backup_log) will have the

a. The startup time of the script run

b. The maximum archive log sequence number – before the start of the backup and after the end of the backup

c. The end time of the backup.

The rman_fullbackp_${DD} will look like rman_fullbackp_030310,if run on 03rd March 2010.Every time the script runs it will generate a new logfile.It will contain the details of the RUN block in RMAN backup.

2. A line in the alert log of both the instances ,with the maximum sequence number (before the start of the backup and after the end of the backup) will be written.For example

Note to DBA : On Instance 2 RMAN Backup Starts at 2289 on 03/03/2010 02:40:00

3. All the archivelogs will be backed up as  “NOT BACKED UP 1 TIMES” is being used.Even if the crontab didn’t run a particular day or time , the next time it runs it will take the backup of all those archivelogs that haven’t been backed up even once.

4. The backup pieces goes to the current date folder which is created using

     mkdir /backup/rman/datafiles_backup/bkp_${DD}

Suppose the backup is run on 03rd March 2010, so it will create a folder bkp_030310 inside “/backup/rman/datafiles_backup” and place all the backup pieces inside it.Similar is the case for archivelogs and the controlfile.

5. The retention and the deletion policy varies as per the requirements .I take a full backups on Sunday and rest all the days its incremental, with the same script with few modifications and the most important one is

     backup as compressed backupset incremental level 1 database

Hope this helps someone and as always, suggestions are welcomed. 🙂

18 thoughts on “Shell Script for RMAN Backup

  1. Pingback: The HP SimpleSave
  2. How rman will understand the os level variables such as bkp_${DD}.
    Rest of the shell is fine perfect.
    But when I execute it, it backup pieces in Oracle_HOME/dbs. resulting in /u01 getting full and I had to face down time situation .
    please advice

    1. Hi Jayant,

      If you carefully at the script, you would see DD defined as `date +%d%m%y`.

      Assuming you are using the script as it do you have “/backup” location to create the directories which the script does?

      mkdir /backup/rman/datafiles_backup/bkp_${DD}
      mkdir /backup/rman/controlfile_backup/bkp_${DD}
      mkdir /backup/rman/arch_backup/bkp_${DD}
      

      If not then simply change the location to the preferred location in

      backup as compressed backupset incremental level 0 database tag 'TEST_FULL_BACKUP' format '/backup/rman/datafiles_backup/bkp_${DD}/%d_%s_%p';
      backup as compressed backupset archivelog all not backed up 1 times FORMAT '/backup/rman/arch_backup/bkp_${DD}/ARCH_%d_%s_%p';
      CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO  '/ora_backup/rman/controlfile_backup/bkp_${DD}/%F';
      

      Regards,
      Anand

Leave a comment