Here is one of my posts from last year’s IOUG Tip Booklet titled 10 Questions (and answers) about RMAN Stored Scripts. Enjoy!

Today, most Oracle shops are using RMAN for backups. Why? Because it powerful, resilient and not very difficult to learn. It is an excellent tool to have in your DBA toolbox. Let’s add an additional tool to that toolbox that will help to make your job as a DBA a bit easier, RMAN stored scripts.

  1. What are stored scripts?

Stored scripts are RMAN scripts that are centrally located so that all of your databases can access them. The scripts can be either global scripts that all registered databases can execute or they can be local scripts for a single database to execute.

 

  1. Where are they stored? 

Stored scripts reside in the RMAN catalog. Databases that are registered with the catalog can execute these scripts.

 

  1. Why should I use them?

Stored scripts reduce DBA maintenance. Even if you have packaged scripts that you put on every database server, you still have to make sure that you keep track of what version of a script is running on each server. That is not too difficult when you maintain 10 or 20 servers, but what happens when you have to maintain 50, 100 or 1000? Stored scripts give you the ability to update a script for all of the databases at once.

 

  1. How do I create them?

Creating the script is very easy. You can take your existing RMAN scripts, make a few small modifications and use that script to create a stored script.

Here we have an existing RMAN script:

backup database plus archivelog;

Add a few lines to the top and enclose our command with braces:

create global script simple_backup
comment 'Simple full backup with archivelogs'
{
backup database plus archivelog;
}

Now that we have our script, we have to run RMAN and connect to any database already registered in the catalog and of course, the catalog. We can then execute our script to create the global script:

RMAN> create global script simple_backup
2> comment 'Simple full backup with archivelogs'
3> {
4> backup database plus archivelog;
5> }
created global script simple_backup
RMAN> **end-of-file**

 

To make this a local script, remove the word global from the script and connect to the target database that will use this script and the catalog and run the script.

 

  1. How do I view them?

To see the script names, issue the list global script names command:

RMAN> list global script names;

List of Stored Scripts in Recovery Catalog

    Global Scripts

       Script Name

       Description
       ----------------------------------------------------------
       simple_backup

       Simple full backup with archivelogs

 

 

To see what is in your global script, issue the print command:

RMAN> print global script simple_backup;

printing stored global script: simple_backup
{
backup database plus archivelog;
}
  1. How do I use them?

Executing the script is very simple. You enter the command “execute global script script_name within a run block:

RMAN> run { execute global script simple_backup; }   

executing global script: simple_backup

Starting backup at 03-SEP-15 15:30
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=360 device type=DISK
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
.
. Output truncated
.
Finished backup at 03-SEP-15 15:31

Starting Control File and SPFILE Autobackup at 03-SEP-15 15:31
piece handle=/oracle/rman/DH12102A/DH12102A_c-615337328-20150903-01_autoctlfile.bck comment=NONE
Finished Control File and SPFILE Autobackup at 03-SEP-15 15:31

 

 

  1. How do I replace an existing script?

It is very simple to replace a script, substitute the replace command for the create command in your script.

 

  1. Can I pass variables to my script?

Oracle gives you the ability to use substitution variables within the scripts. If we make another small change to the script, we can add our substitution variables:

replace global script simple_backup
comment 'Simple full backup with archivelogs'
{
backup database plus archivelog tag '&1.';
}

 

Execute the script to replace the existing script, but you will notice that you are prompted for a value when it gets to your positional variable. You can just hit “Enter” to continue:

RMAN> @simple_backup.rcv

RMAN>
replace global script simple_backup
2> comment 'Simple full backup with archivelogs'
3> {
4> backup database plus archivelog tag '
Enter value for 1:
';
5> }
replaced global script simple_backup

RMAN> **end-of-file**

 

When we run the script, we can enter a value that will pass on to the positional variables. This gives you the ability to send custom information for a database backup:

RMAN> run { execute global script simple_backup; }

executing global script: simple_backup

Enter value for 1: Test_1

Starting backup at 03-SEP-15 15:39
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=360 device type=DISK
channel ORA_DISK_1: starting compressed archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
.
. Output truncated
.
Finished backup at 03-SEP-15 15:40

Starting Control File and SPFILE Autobackup at 03-SEP-15 15:40
piece handle=/oracle/rman/DH12102A/DH12102A_c-615337328-20150903-02_autoctlfile.bck comment=NONE
Finished Control File and SPFILE Autobackup at 03-SEP-15 15:40

 

 

And now we can see that the tag was passed to the backup:

RMAN> list backup summary tag 'Test_1';

List of Backups
===============
Key     TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
324     B  A  A DISK        03-SEP-15 15:39 1       1       YES        TEST_1
362     B  A  A DISK        03-SEP-15 15:40 1       1       YES        TEST_1

 

  1. How do I delete a script?

Very similar to the way that you created it, you issue the delete command:

RMAN> delete global script simple_backup;

deleted global script: simple_backup

RMAN> list global script names;

List of Stored Scripts in Recovery Catalog

        No scripts in recovery catalog

 

  1. Why aren’t I using them?

I will let you answer that one.