分享一下我老师大神的人工智能教程!零基础,通俗易懂!
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle
的相关工作,也是DBA必不可少的技能之一。本文针对Linux/Unix shell脚本调用sql, rman 脚本给出了相关示例。
一、由shell脚本调用sql,rman脚本
1、shell脚本调用sql脚本#首先编辑sql文件oracle@SZDB:~> more dept.sqlconnect scott/tigerspool /tmp/dept.lstset linesize 100 pagesize 80select * from dept;spool off;exit;#编辑shell脚本文件,在shell脚本内调用sql脚本oracle@SZDB:~> more get_dept.sh#!/bin/bash# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBOsqlplus -S /nolog @/users/oracle/dept.sql #注意此处执行sql脚本的方法 -S 表示以静默方式执行exit#授予脚本执行权限oracle@SZDB:~> chmod 775 get_dept.sh-->执行shell脚本oracle@SZDB:~> ./get_dept.sh DEPTNO DNAME LOC---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON2、shell脚本调用rman脚本#首先编辑RMAN脚本oracle@SZDB:~> more rman.rcvRUN {CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;CONFIGURE BACKUP OPTIMIZATION ON;CONFIGURE CONTROLFILE AUTOBACKUP ON;CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/users/oracle/bak/%d_%F';ALLOCATE CHANNEL CH1 TYPE DISK MAXPIECESIZE=4G;ALLOCATE CHANNEL CH2 TYPE DISK MAXPIECESIZE=4G;SET LIMIT CHANNEL CH1 READRATE=10240;SET LIMIT CHANNEL CH1 KBYTES=4096000;SET LIMIT CHANNEL CH2 READRATE=10240;SET LIMIT CHANNEL CH2 KBYTES=4096000;CROSSCHECK ARCHIVELOG ALL;DELETE NOPROMPT EXPIRED ARCHIVELOG ALL;BACKUP DATABASE FORMAT '/users/oracle/bak/%d_FULL__%U';SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';BACKUP ARCHIVELOG ALL FORMAT '/users/oracle/bak/%d_LF_%U' DELETE INPUT;DELETE NOPROMPT OBSOLETE;RELEASE CHANNEL CH1;RELEASE CHANNEL CH2;}#编辑shell脚本文件,在shell脚本内调用rman脚本oracle@SZDB:~> more rman_bak.sh#!/bin/bash# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBO$ORACLE_HOME/bin/rman target / cmdfile=/users/oracle/rman.rcv log=/users/oracle/bak/rman.logexit#授予脚本执行权限oracle@SZDB:~> chmod 775 rman_bak.sh#执行shell脚本oracle@SZDB:~> ./rman_bak.sh
二、嵌入sql语句及rman到shell脚本
1、直接将sql语句嵌入到shell脚本oracle@SZDB:~> more get_dept_2.sh#!/bin/bash# Author : Robinson Cheng # Blog : http://blog.csdn.net/robinson_0612# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBOsqlplus -S /nolog <<EOF #EOF在此表示当输入过程中碰到EOF后,整个sql脚本输入完毕connect scott/tigerspool /tmp/dept.lstset linesize 100 pagesize 80select * from dept;spool off;exit; #退出sqlplus 环境EOFexit #推出shell脚本 #授予脚本执行权限oracle@SZDB:~> chmod u+x get_dept_2.sh#执行shell脚本oracle@SZDB:~> ./get_dept_2.sh DEPTNO DNAME LOC---------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON2、直接将sql语句嵌入到shell脚本(方式二,使用管道符号>代替spool来输出日志)oracle@SZDB:~> more get_dept_3.sh#!/bin/bash# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBOsqlplus -S /nolog 1>/users/oracle/dept.log 2>&1 <<EOFconnect scott/tigerset linesize 80 pagesize 80select * from dept;exit;EOFcat /users/oracle/dept.logexit#另一种实现方式,将所有的sql语句输出来生成sql脚本后再调用oracle@SZDB:~> more get_dept_4.sh#!/bin/bash# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBOecho "conn scott/tiger select * from dept;exit;" >/users/oracle/get_dept.sqlsqlplus -silent /nolog @get_dept.sql 1>/users/oracle/get_dept.log 2>&1cat get_dept.logexit3、将rman脚本嵌入到shell脚本oracle@SZDB:~> more rman_bak_2.sh#!/bin/bash# set environment variableif [ -f ~/.bashrc ]; then . ~/.bashrcfiexport ORACLE_SID=CNMMBO$ORACLE_HOME/bin/rman log=/users/oracle/bak/rman.log <<EOFconnect target /RUN {CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;CONFIGURE BACKUP OPTIMIZATION ON;CONFIGURE CONTROLFILE AUTOBACKUP ON;CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/users/oracle/bak/%d_%F';ALLOCATE CHANNEL CH1 TYPE DISK MAXPIECESIZE=4G;ALLOCATE CHANNEL CH2 TYPE DISK MAXPIECESIZE=4G;SET LIMIT CHANNEL CH1 READRATE=10240;SET LIMIT CHANNEL CH1 KBYTES=4096000;SET LIMIT CHANNEL CH2 READRATE=10240;SET LIMIT CHANNEL CH2 KBYTES=4096000;CROSSCHECK ARCHIVELOG ALL;DELETE NOPROMPT EXPIRED ARCHIVELOG ALL;BACKUP DATABASE FORMAT '/users/oracle/bak/%d_FULL__%U';SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';BACKUP ARCHIVELOG ALL FORMAT '/users/oracle/bak/%d_LF_%U' DELETE INPUT;DELETE NOPROMPT OBSOLETE;RELEASE CHANNEL CH1;RELEASE CHANNEL CH2;}EXIT;EOFexit #授予脚本执行权限oracle@SZDB:~> chmod u+x rman_bak_2.sh #执行shell脚本 oracle@SZDB:~> ./rman_bak_2.shRMAN> RMAN> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15> 16> 17> 18> 19> 20> 21> RMAN> oracle@SZDB:~>
三、更多参考
有关基于用户管理的备份和备份恢复的概念请参考
Oracle 基于用户管理恢复的处理(详细描述了介质恢复及其处理)
Oracle 基于备份控制文件的恢复(unsing backup controlfile)
有关RMAN的备份恢复与管理请参考
RMAN 备份路径困惑(使用plus archivelog时)
有关ORACLE体系结构请参考
Oracle 联机重做日志文件(ONLINE LOG FILE)
Oracle 实例和Oracle数据库(Oracle体系结构)