Oracle RMAN Example Backup Script

oracle rmanThis example is by no means meant to substitute a well-thought out backup and recovery strategy for an Oracle Database, but it will show you how simple it is to backup an 11g Database using Oracle RMAN. At the very least, it will point you in the right direction and give you a working example from which to base your testing from. Also, I feel silly saying this, but please don’t try this on an operational database!

Let’s change a couple of the default settings within the RMAN console. We want to make sure the control file is always backed up, and we want to change how many backup sets will be retained.

To open an RMAN console type the following in a system command window:

RMAN target=/

To ensure the control file is backed up by default, type the following at the RMAN prompt:

CONFIGURE CONTROLFILE AUTOBACKUP ON;

To change how many backup sets you’d like to retain, type the following at the RMAN prompt:

CONFIGURE RETENTION POLICY REDUNDANCY 2;

To verify the new settings, type the following at the RMAN command prompt:

show all;

Exit the RMAN command prompt by typing the following at the RMAN prompt:

exit;

Next we want to create a file that contains the scripting RMAN will use to backup the database, archive logs, and SPFILE. Create a file called oraBackup.cmd, copy the below text into that file, then save the file:


RUN {
  DELETE noprompt OBSOLETE RECOVERY WINDOWS 2 DAYS;
  ALLOCATE CHANNEL ch1 TYPE DISK 
  BACKUP
     full
     filesperset 5
     FORMAT L:\orabackup\dbname_full_%t_%s.bkp' (database);
  CHANGE ARCHIVELOG ALL CROSSCHECK;
  BACKUP
     format "L:\orabackup\dbname_archivelogs_%t_%s.bkp' (archivelog all delete input);
  BACKUP
     spfile;
  BACKUP
     format "L:\orabackup\dbname_archivelogs_%t_%s.bkp' (current controlfile);
  RELEASE CHANNEL ch1
}

NOTE: Make sure you change the DISK FORMAT portion of the script to a valid directory on your system.

Let’s talk a little about what this script does. The ALLOCATE CHANNEL TYPE DISK tells RMAN we are writing to a hard disk and not to something like a tape drive. The FORMAT command instructs RMAN to write the data to the drive and directory specified. The BACKUP AS BACKUPSET indicates the files will be grouped together. The PLUS ARCHIVELOG says collect the archive logs. The DELETE ALL INPUT instructs RMAN to delete the archive logs once they have been backed up. The DELETE OBSOLETE command will delete old backup sets from the disk and remove any references from the RMAN repository. The RELEASE CHANNEL command frees up the resources used to create the RMAN backup set.

Next, we can test the backup script by issuing the following command from a system command prompt:

rman target=/ cmdfile=oraBackup.cmd

This will create three RMAN backup set files in the L:\orabackup directory. One of the .BKP files will contain a copy of the database datafiles, another will contain the archive logs, and the third will be a copy of the SPFILE. All three are needed to recover your database in the event of a lost or corrupt file. That’s pretty much it!

For recovery, RMAN will look in the L:\orabackup directory for the *.BKP files it needs to perform a restore. So if you move them to tape or some other device, you’ll need to copy them back in that same directory if a restore is required.

Leave a Comment

Specify Facebook App ID in Super Socializer > Social Login section in admin panel for Facebook Login to work