You can use a stored script as an alternative to a command file for managing frequently used sequences of RMAN commands. The script is stored in the recovery catalog rather than on the file system.

create a stored script:

1.Start RMAN and connect to a target database and recovery catalog (if used).

2.Run the CREATE SCRIPT command.
CREATE SCRIPT full_backup
{    
BACKUP DATABASE PLUS ARCHIVELOG;
DELETE OBSOLETE;
}

For a global script, the syntax is similar:
CREATE GLOBAL SCRIPT global_full_backup
{    
BACKUP DATABASE PLUS ARCHIVELOG;
DELETE OBSOLETE;
}

Optionally, you can provide a COMMENT with descriptive information:
CREATE GLOBAL SCRIPT global_full_backup
COMMENT ‘use only with ARCHIVELOG mode databases’
{    
BACKUP DATABASE PLUS ARCHIVELOG;
DELETE OBSOLETE;
}

You can also create a script by reading its contents from a text file. The file must begin with a left brace ({) character, contain a series of commands valid within a RUN block, and end with a right brace (}) character. Otherwise, a syntax error is signalled, just as if the commands were entered at the keyboard.
CREATE SCRIPT full_backup FROM FILE ‘/tmp/my_script_file.txt’;

3.Examine the output.
If no errors are displayed, then RMAN successfully created the script and stored in the recovery catalog.

Replacing Stored Scripts

To update stored scripts, use the REPLACE SCRIPT command. If you are replacing a local script, then you must be connected to the target database that you connected to when you created the script. If the script does not exist, then RMAN creates it.

To replace a stored script:
REPLACE SCRIPT full_backup
{
BACKUP DATABASE PLUS ARCHIVELOG;
}

You can update global scripts by specifying the GLOBAL keyword as follows:
REPLACE GLOBAL SCRIPT global_full_backup
COMMENT ‘A script for full backup to be used with any database’
{
BACKUP AS BACKUPSET DATABASE PLUS ARCHIVELOG;
}

As with CREATE SCRIPT, you can update a local or global stored script from a text file with the following form of the command:
REPLACE GLOBAL SCRIPT global_full_backup FROM FILE ‘/tmp/my_script_file.txt’;

Executing Stored Scripts

Use the EXECUTE SCRIPT command to run a stored script. If GLOBAL is specified, then a global script with this name must exist in the recovery catalog; otherwise, RMAN returns error RMAN-06004. If GLOBAL is not specified, then RMAN searches for a local stored script defined for the current target database. If no local script with this name is found, then RMAN searches for a global script by the same name and executes it if one is found.

If needed, use SHOW to examine your configured channels.
Your script uses the automatic channels configured at the time you execute the script. Use ALLOCATE CHANNEL commands in the script if you must override the configured channels. Because of the RUN block, if an RMAN command in the script fails, subsequent RMAN commands in the script do not execute.

RUN
{
EXECUTE SCRIPT full_backup;
}

The preceding command invokes a local script if one exists with the name specified. If no local script is found, but there is a global script with the name specified, then RMAN executes the global script.

You can also use EXECUTE GLOBAL SCRIPT to control which script is invoked if a local and a global script have the same name. If there is no local script called global_full_backup, the following two commands have the same effect:
RUN
{
EXECUTE GLOBAL SCRIPT global_full_backup;
}

RUN
{
EXECUTE SCRIPT global_full_backup;
}

Creating and Executing Dynamic Stored Scripts

You can specify substitution variables in the CREATE SCRIPT command. When you start RMAN on the command line, the USING clause specifies one or more values for use in substitution variables in a command file. As in SQL*Plus, &1 indicates where to place the first value, &2 indicates where to place the second value, and so on.

Create a command file that contains a CREATE SCRIPT statement with substitution variables for values that must be dynamically updated.
The following example uses substitution variables for the name of the tape set, for a string in the FORMAT specification, and for the name of the restore point.
CREATE SCRIPT quarterly {
ALLOCATE CHANNEL c1
DEVICE TYPE sbt
PARMS ‘ENV=(OB_MEDIA_FAMILY=&1)’;
BACKUP
TAG &2
FORMAT ‘/disk2/bck/&1%U.bck’
KEEP FOREVER
RESTORE POINT &3
DATABASE;
}

Connect RMAN to a target database (which must be mounted or open) and recovery catalog, specifying the initial values for the recovery catalog script.
For example, enter the following command:
# rman TARGET / CATALOG rco@catdb USING arc_backup bck0906 FY06Q3

A recovery catalog is required for KEEP FOREVER, but is not required for any other KEEP option.

Run the command file created in the first step to create the stored script.
For example, run the /tmp/catscript.rman command file as follows:
RMAN> @/tmp/catscript.rman
This step creates but does not execute the stored script.

Every quarter, execute the stored script, passing values for the substitution variables.
The following example executes the recovery catalog script named quarterly. The example specifies arc_backup as the name of the media family (set of tapes), bck1206 as part of the FORMAT string and FY06Q4 as the name of the restore point.
RUN
{
EXECUTE SCRIPT quarterly
USING arc_backup
bck1206
FY06Q4;
}

Printing Stored Scripts

PRINT SCRIPT full_backup;
PRINT SCRIPT full_backup TO FILE ‘/tmp/my_script_file.txt’;
RINT GLOBAL SCRIPT global_full_backup;
PRINT GLOBAL SCRIPT global_full_backup TO FILE ‘/tmp/my_script_file.txt’;

Listing Stored Script Names

Use the LIST … SCRIPT NAMES command to display the names of scripts defined in the recovery catalog. LIST GLOBAL SCRIPT NAMES and LIST ALL SCRIPT NAMES are the only commands that work when RMAN is connected to a recovery catalog without connecting to a target instance; the other forms of the LIST … SCRIPT NAMES command require a recovery catalog connection.

LIST SCRIPT NAMES;
LIST GLOBAL SCRIPT NAMES;
LIST ALL SCRIPT NAMES;
For each script listed, the output indicates which target database the script is defined for (or whether a script is global).

Deleting Stored Scripts

If you use DELETE SCRIPT without GLOBAL, and there is no stored script for the target database with the specified name, then RMAN looks for a global stored script by the specified name and deletes the global script if it exists. For example, suppose you enter the following command:
DELETE SCRIPT ‘global_full_backup’;
In this case, RMAN looks for a script global_full_backup defined for the connected target database, and if it did not find one, it searches the global scripts for a script called global_full_backup and delete that script.
To delete a global stored script, use DELETE GLOBAL SCRIPT:
DELETE GLOBAL SCRIPT ‘global_full_backup’;

Executing a Stored Script at RMAN Startup

To run the RMAN client and start a stored script in the recovery catalog on startup, use the SCRIPT argument when starting the RMAN client. For example, you could enter the following command to execute script /tmp/fbkp.cmd:
% rman TARGET / CATALOG rco@catdb SCRIPT ‘/tmp/fbkp.cmd’;
You must connect to the recovery catalog, which contains the stored script, and the target database, to which the script applies, when starting the RMAN client.
If local and global stored scripts are defined with the same name, then RMAN always executes the local script.