Oracle Shell Scripting
This article presents some basic techniques for creating Windows batch files and UNIX/Linux shell scripts that connect to SQL*Plus and RMAN.Windows
To run an SQL script using SQL*Plus, place the SQL along with any SQL*Plus commands in a file and save it on your operating system. For example, save the following script in a file called "C:emp.sql".Next, create a batch file called "C:get_emp.bat" containing the following command.CONNECT scott/tiger SPOOL C:emp.lst SET LINESIZE 100 SET PAGESIZE 50 SELECT * FROM emp; SPOOL OFF EXIT;
The resulting batch file can be run manually, by double-clicking on it, or scheduled using the Scheduled Tasks Wizard (Start > Programs > Accessories > System Tools > Scheduled Tasks) or the AT scheduler.sqlplus /nolog @C:emp.sql
The method is very similar when using Recovery Manager (RMAN). As an example, place the following RMAN commands in a file called "C:cmdfile.txt".
RUN {
ALLOCATE CHANNEL ch1 TYPE
DISK FORMAT 'C:oraclebackupDB10G%d_DB_%u_%s_%p';
BACKUP DATABASE PLUS ARCHIVELOG;
RELEASE CHANNEL ch1;
}
EXIT;
Next create a batch file called "C:backup.bat" containing the following command.This command can include arman target=/ @cmdfile.txt
catalog= entry if a recovery catalog is used. Once again, resulting batch file can be run manually or scheduled.UNIX and Linux (Method 1)
The previous methods works equally well in UNIX and Linux environments. For example, save the following script in a file called "/u01/emp.sql".Next, create a shell script called "/u01/get_emp.ksh" containing the following lines.CONNECT scott/tiger SPOOL /u01/emp.lst SET LINESIZE 100 SET PAGESIZE 50 SELECT * FROM emp; SPOOL OFF EXIT;
The following command makes the file executable for the file owner.#!/bin/ksh sqlplus /nolog @/u01/emp.sql
The resulting shell script can be run manually from the command line, or scheduled using CRON.chmod u+x /u01/get_emp.ksh
For RMAN, place the following RMAN commands in a file called "/u01/cmdfile.txt".
RUN {
ALLOCATE CHANNEL ch1 TYPE
DISK FORMAT '/u01/backup/DB10G/%d_DB_%u_%s_%p';
BACKUP DATABASE PLUS ARCHIVELOG;
RELEASE CHANNEL ch1;
}
EXIT;
Next create a batch file called "/u01/backup.ksh" containing the following lines.This command can include a#!/bin/ksh rman target=/ @/u01/cmdfile.txt
catalog= entry if a recovery catalog is used. Once again, resulting shell script must be made
executable using the following command.The shell script is now ready to run.chmod u+x /u01/backup.ksh
UNIX and Linux (Method 2)
UNIX and Linux environments also allow the SQL*Plus and RMAN commands to be piped directly from the command line. For example, save the following commands in a file called "/u01/get_emp.ksh".Notice the "<< EOF" and "EOF" tags, indicating the start and end of the command being piped into the SQL*Plus executable. The shell script is made executable using the following command.#!/bin/ksh sqlplus /nolog << EOF CONNECT scott/tiger SPOOL /u01/emp.lst SET LINESIZE 100 SET PAGESIZE 50 SELECT * FROM emp; SPOOL OFF EXIT; EOF
The shell script is ready to be run manually from the command line or scheduled using CRON.chmod u+x /u01/get_emp.ksh
The following example shows how RMAN can use the same method. Create a file called "/u01/backup.ksh" with the following contents.
#!/bin/ksh
rman target=/ << EOF
RUN {
ALLOCATE CHANNEL ch1 TYPE
DISK FORMAT '/u01/backup/DB10G/%d_DB_%u_%s_%p';
BACKUP DATABASE PLUS ARCHIVELOG;
RELEASE CHANNEL ch1;
}
EXIT;
EOF
Once again, the script can be made executable using the following command.The shell script is now ready to run.chmod u+x /u01/backup.ksh
For more information see:
- SQL*Plus User's Guide and Reference
- Oracle Database Backup and Recovery Reference
- UNIX Commands for DBAs
Back to the Top.
