[Oracle Note] Oracle Shell Scripting[from forum]

本文介绍如何使用Windows批处理文件和UNIX/Linux Shell脚本连接到SQL*Plus和RMAN,包括运行SQL脚本和备份数据库的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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".

CONNECT scott/tiger
SPOOL C:\emp.lst
SET LINESIZE 100
SET PAGESIZE 50
SELECT *
FROM emp;
SPOOL OFF
EXIT;

Next, create a batch file called "C:\get_emp.bat" containing the following command.

sqlplus /nolog @C:\emp.sql

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.

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:\oracle\backup\DB10G%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.

rman target=/ @cmdfile.txt

This command can include a 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".

CONNECT scott/tiger
SPOOL /u01/emp.lst
SET LINESIZE 100
SET PAGESIZE 50
SELECT *
FROM emp;
SPOOL OFF
EXIT;

Next, create a shell script called "/u01/get_emp.ksh" containing the following lines.

#!/bin/ksh
sqlplus /nolog @/u01/emp.sql

The following command makes the file executable for the file owner.

chmod u+x /u01/get_emp.ksh

The resulting shell script can be run manually from the command line, or scheduled using CRON.

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.

#!/bin/ksh
rman target=/ @/u01/cmdfile.txt

This command can include a catalog= entry if a recovery catalog is used. Once again, resulting shell script must be made executable using the following command.

chmod u+x /u01/backup.ksh

The shell script is now ready to run.

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".

#!/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

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.

chmod u+x /u01/get_emp.ksh

The shell script is ready to be run manually from the command line or scheduled using CRON.

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.

chmod u+x /u01/backup.ksh

The shell script is now ready to run.

UNIX and Linux (Returning values from SQL)

The following code show a script to pull the output of a query into a shell script variable.

#!/bin/bash
RETVAL=`sqlplus -silent scott/tiger <<EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT * FROM emp;
EXIT;
EOF`
if [ -z "$RETVAL" ]; then
  echo "No rows returned from database"
  exit 0
else
  echo $RETVAL
fi

If you are returning a single value, this method works well. If you are returning multiple rows of multiple columns it gets a bit messy and forces you to parse the return value.

转载于:https://www.cnblogs.com/jefflu2012/archive/2012/08/28/2661052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值