Automated Scripts RMAN
Automated Scripts RMAN
Infosys
INFOSYS LIMITED
Hyderabad
Date:
COPYRIGHT NOTICE
© 2013 Infosys Limited, Bangalore, India. All rights reserved. Infosys believes the
information in this document is accurate as of its publication date; such information is
subject to change without notice. Infosys acknowledges the proprietary rights of other
companies to the trademarks, product names and such other intellectual property rights
mentioned in this document. Except as expressly permitted, neither this document nor any
part of it may be reproduced, stored in a retrieval system, or transmitted in any form or by
any means, electronic, mechanical, printing, photocopying, recording or otherwise, without
the prior permission of Infosys Limited and/or any named intellectual property rights
holders under this document.
Infosys Limited
Hosur Road
Electronic City, 3rd Cross
Bangalore 560 100
India.
Telephone: (91) (80)28520 261-270
Fax: (91) (80) 8520 362
Website: http://www.infosys.com
Project Details
H/W Platform: NA
Application Type: NA
Target readers
Keywords
Introduction
Many DBAs have discovered how Oracle Recovery Manager (Oracle RMAN) can reliably back up, restore,
and recover Oracle Database instances. Among its many features is the ability to script the commands
for batch-centric, hands-off execution. This article discusses the ways to script Oracle RMAN commands
in Oracle Database 11g and how to pick the right approach for your specific needs.
Why Script?
1. Most Oracle RMAN activities are batch-oriented and can be automated. For instance, backing up a
database is a repetitive activity and not something you would want to execute interactively.
2. Scripts provide consistency. For tasks of an ad hoc nature, such as recovering a database from a
backup, automation is not strictly required. When a script is used to execute the activity, however, the
action will be the same, regardless of the experience and expertise of the DBA performing the recovery.
There are two ways to script Oracle RMAN commands in Oracle Database 11g:
1. Use a command file. A command file is a text file residing in the file system.
2. Use a stored script. This script is stored in the Oracle RMAN catalog database and executed from
the Oracle RMAN command prompt.
Command Files
Oracle RMAN command files are plain-text files and similar to shell scripts in UNIX or batch files in
Microsoft Windows. Listing 1 show a very simple example command file—named backup_ts_users.rman
—used to back up the USERS tablespace. The file extension .rman is not necessary, but it is helpful in
making the meaning of the file clear.
Connect target /
You can call a command file in several ways. From the Oracle RMAN prompt, you can call the example
command file as follows:
RMAN> @backup_ts_users.rman
Note that the command file is executed by the @ sign. It is important, however, to provide the full name
of the command file, including the extension. (The Oracle RMAN executable does not expect or apply a
default extension.)
You can also call the command file directly from the command line as
rman @backup_ts_users.rman
This approach for calling the script is highly useful in shell scripts or batch files for making backups. Also
note that instead of using the @ sign to call the command file, you can use the cmdfile parameter as
follows:
rman cmdfile=backup_ts_users.rman
Note that the CONNECT clauses are inside the backup_ts_users.rman command file, so there is no
reason to provide the password in the command line—meaning that you can eliminate the risk of
accidental exposure of the password. Had we not included the password of the catalog user rman inside
the command file, we would have had to call the Oracle RMAN executable like this:
rman/secretpass@rmancat
If this command were executed, someone on the server could easily get the password of the catalog
user by checking the process. When the command file contains the connection information—including
the password—for the catalog user, the sensitive information is not visible to anyone watching the
process. Note that you should also set the permissions of the command file in such a way that nonadmin
users will not be able to read it.
Passing parameters. The backup_ts_users.rman command file works well, but it’s too specific. It forces
the output of the backup to one specific directory and backs up only one tablespace (USERS). If you want
to back up to a different location or back up a different tablespace, you have to create a new script.
A better strategy is to make an Oracle RMAN command file parameter-driven. Rather than hard-coding
specific values in the script, you can include parameters whose values are passed at runtime. Listing 2
shows a modified version of the backup_ts_users .rman command file, named backup_ts_generic.rman.
Instead of actual values, the new command file includes the parameters (also known as placeholders or
substitution variables ) &1 and &2. With a parameter-driven command file, you can define any number
of parameters in this manner and pass the values at runtime.
run {
A shell script, named backup_ts_generic.sh, calls the backup_ts_generic.rman command file with the
values /tmp as the backup location (for parameter &1) and USERS as the tablespace name (for
parameter &2):
$ORACLE_HOME/bin/rman <<EOF
EOF
You can make this shell script even more generic, so that the parameters are passed from the command
line of the file system itself. For example, if you modify the second line in the backup_ts_generic.sh shell
script so it reads
@backup_ts_generic.rman "/tmp" $1
you will be able to call the backup_ts_generic.rman command file, provide /tmp as the backup location,
and pass the tablespace name in the command line. For instance, if you want to back up the MYTS1
tablespace, you can issue
backup_ts_generic.sh MYTS1
Logging. When you run Oracle RMAN scripts via an automated mechanism such as cron in UNIX or
Scheduler in Windows, you are not physically watching the command window, so how do you know the
output of the Oracle RMAN commands? The output is especially crucial when command execution
results in an error and you need to examine the output. To capture the output, you can use the log
parameter in the Oracle RMAN command line:
Now the output of the backup_ts_generic.rman command file will be recorded in a file named
backup_ts_users.log instead of appearing on the screen. You can view this file later to examine the
results of the Oracle RMAN run.
Stored Scripts
Although command files work pretty well in most cases, they have one huge drawback. A command file
should be available on the server where the Oracle RMAN backup is to be run. Otherwise, from within
the command file, you have to connect from the Oracle RMAN client to the server by using a connect
string:
There are several problems with this setup. First, this modified command file needs to store the
password of SYS or some other user with the SYSDBA privilege. In a security-conscious environment,
that may not be acceptable. Second, the Oracle RMAN client may be not be compatible with the Oracle
Database release. Finally, for performance reasons, you may very well want to run the Oracle RMAN
client on the same server as the database itself. But what if you have databases on different servers?
You will have to replicate a command file script to all servers. And when you modify the script, you will
have to make sure it is copied to all those servers again.
The solution? With Oracle RMAN stored scripts, you can create scripts that are stored inside the Oracle
RMAN catalog and not on the server itself. Listing 3 shows an example stored script called
backup_ts_users. Because it is stored inside the Oracle RMAN catalog, you will need to connect to the
catalog first, as shown in the listing. To execute this script, all you have to do is call it with the execute
command from the Oracle RMAN prompt:
backup_ts_users; }
Code Listing 3:
Stored script for backing up USERS tablespace
C:\> rman
3> {
6> }
The backup_ts_users stored script created in Listing 3 is available only to the target database to which it
is currently connected. It is a local stored script, and you can’t execute a local script created for one
database in another. To execute a script in multiple databases, create a global stored script by using the
keyword GLOBAL between CREATE and SCRIPT. For instance, to create the script shown in Listing 3 as a
global stored script, replace
with
Once created, this global stored script can be executed in any database connected to this catalog. If you
need to modify the script, there is no need to copy it to all servers or databases; it’s automatically
available for execution to all databases connecting to the catalog.
If the global stored script already exists and you want to update it, replace CREATE with REPLACE—
Parameterization. This backup_ts_users stored script has a very specific purpose: backing up the USERS
tablespace. What if you want to back up a different tablespace? Rather than creating multiple scripts,
you can create a generic stored script to back up any tablespace (as you did with the command files
earlier).
Listing 4 shows how to create a parameterized stored script. In place of the tablespace name, Listing 4
uses the &1 parameter, whose value is passed at runtime. When a parameter-driven stored script is
created, Oracle RMAN asks for an example value for any parameter used. When &1 is included as a
parameter in line 5, Oracle RMAN asks for an example value. Enter users or any other example
tablespace you may want to pass. Remember, the stored script merely asks for an example value; it
does not store the value you used in the script itself.
3> {
users;
6> }
7>
With the parameterized stored script created, pass the value of the parameter via a USING clause. For
example, to back up the SYSTEM tablespace by using this backup_ts_any stored script, use the following
Oracle RMAN command:
To display the list of stored scripts, use the list script names command as follows:
Script Name
Description
------------
backup_ts_any
backup_ts_users
This command displays the names of local as well as global stored scripts.
To display the contents of a specific stored script, such as backup_ts_any, use the following command:
Next Steps
backup_ts_level1_any;
If the stored script you want to print is local, omit the keyword GLOBAL in the command.
backup_ts_level1_any;
What if you want to create a stored script from a script file in the file system? You can import the file
into the catalog. Here is an example:
Conversely, you can create a file from a stored script (or export a stored script to a file). Here is an
example:
to file 'backup_ts_users.rman';
Reference(s)
None