使用Pipe来执行RMAN命令

RMAN Pipe接口是执行RMAN命令并接收命令输出的一种替代方法。使用这种接口,RMAN使用dbms_pipe包来获得命令并发送输出来代替操作系统shell。使用这种接口,可以编写可移值的RMAN程序接口。这个功能使用我们中心自己开发的数据库监控平台可以调用RMAN来执行备份,使用pipe接口编写RMAN备份脚本可以跨操作系统平台使用。
pipe接口是通过使用pipe命令行参数来进行调用的。RMAN使用两种私有管道:一个用来接收命令,一个用来发送输出。管道名是用pipe参数来决定的。例如,执行下面的命令 :

[oracle11@jingyong1 ~]$ rman PIPE abc TARGET /

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 10:38:53 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

通过管道连接执行多个RMAN命令
假设想要通过管道来连续执行多个命令。在每一个命令发送到管道执行并返回输出后,RMAN会暂停并等待下一个命令。

1.以pipe选项来启动RMAN并连接到目标数据库:

[oracle11@jingyong1 ~]$ rman PIPE abc TARGET /

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 10:38:53 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

也可以指定timeout选项,强制RMAN在指定的时间内没有从输入管道接收到命令就自动退出。

[oracle11@oracle11g ~]$ rman PIPE abc TARGET / TIMEOUT 60

Recovery Manager: Release 11.2.0.4.0 - Production on Fri Apr 17 14:51:12 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

[oracle11@oracle11g ~]$ date
Fri Apr 17 14:52:14 CST 2015

2.连接管目标数据库并通过dbms_pipe.pack_message与dbms_pipe.send_message在输入管道中输入要执行的命令。当RMAN使用管道来代替标准RMAN输入时会显示消息RMAN-00572,下面使用管道来显示当前的RMAN配置信息并对数据库执行备份。

[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Apr 17 11:49:25 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set serverout on size 100000
SQL> declare
  2  i_v varchar2(2000):='show all;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /
0

PL/SQL procedure successfully completed.




[oracle11@oracle11g ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Fri Apr 17 11:49:30 2015

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set serverout on size 100000
SQL> declare
  2  i_v varchar2(2000):='backup as backupset database plus archivelog;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /
0

PL/SQL procedure successfully completed.

3.使用dbms_pipe.receive_message和dbms_pipe.unpack_message来读取RMAN的输出信息:

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',5);
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
connected to target database: DB (DBID=1640573015)
RMAN-00572: waiting for DBMS_PIPE input
using target database control file instead of recovery catalog
RMAN configuration parameters for database with db_unique_name DB are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR
LOAD TRUE ; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO
'/u03/app/oracle/11.2.0/db/dbs/snapcf_db.f'; # default
RMAN-00572: waiting for DBMS_PIPE input

PL/SQL procedure successfully completed.

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',600);
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
Starting backup at 2015-04-17 12:21:43
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=18 RECID=11 STAMP=877175019
input archived log thread=1 sequence=19 RECID=12 STAMP=877175163
input archived log thread=1 sequence=20 RECID=13 STAMP=877212065
input archived log thread=1 sequence=21 RECID=14 STAMP=877253311
input archived log thread=1 sequence=22 RECID=15 STAMP=877262007
input archived log thread=1 sequence=23 RECID=16 STAMP=877262176
input archived log thread=1 sequence=24 RECID=17 STAMP=877262429
input archived log thread=1 sequence=25 RECID=18 STAMP=877262605
input archived log thread=1 sequence=26 RECID=19 STAMP=877262607
input archived log thread=1 sequence=27 RECID=20 STAMP=877262777
input archived log thread=1 sequence=28 RECID=21 STAMP=877263703
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:21:44
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:21:59
piece handle=/u03/app/oracle/11.2.0/db/dbs/1fq4juqn_1_1 tag=TAG20150417T122143
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 2015-04-17 12:21:59
Starting backup at 2015-04-17 12:21:59
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
input datafile file number=00005 name=/u03/app/oracle/oradata/db/example01.dbf
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:21:59
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:34
piece handle=/u03/app/oracle/11.2.0/db/dbs/1gq4jur7_1_1 tag=TAG20150417T122159
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:36
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:24:42
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:43
piece handle=/u03/app/oracle/11.2.0/db/dbs/1hq4jv03_1_1 tag=TAG20150417T122159
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:24:43
Starting backup at 2015-04-17 12:24:43
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=29 RECID=22 STAMP=877263883
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:24:44
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:24:45
piece handle=/u03/app/oracle/11.2.0/db/dbs/1iq4jv0c_1_1 tag=TAG20150417T122444
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:24:45
RMAN-00572: waiting for DBMS_PIPE input
Starting backup at 2015-04-17 12:26:09
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=18 RECID=11 STAMP=877175019
input archived log thread=1 sequence=19 RECID=12 STAMP=877175163
input archived log thread=1 sequence=20 RECID=13 STAMP=877212065
input archived log thread=1 sequence=21 RECID=14 STAMP=877253311
input archived log thread=1 sequence=22 RECID=15 STAMP=877262007
input archived log thread=1 sequence=23 RECID=16 STAMP=877262176
input archived log thread=1 sequence=24 RECID=17 STAMP=877262429
input archived log thread=1 sequence=25 RECID=18 STAMP=877262605
input archived log thread=1 sequence=26 RECID=19 STAMP=877262607
input archived log thread=1 sequence=27 RECID=20 STAMP=877262777
input archived log thread=1 sequence=28 RECID=21 STAMP=877263703
input archived log thread=1 sequence=29 RECID=22 STAMP=877263883
input archived log thread=1 sequence=30 RECID=23 STAMP=877263969
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:26:10
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:26:25
piece handle=/u03/app/oracle/11.2.0/db/dbs/1jq4jv32_1_1 tag=TAG20150417T122610
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 2015-04-17 12:26:25
Starting backup at 2015-04-17 12:26:25
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00001 name=/u03/app/oracle/oradata/db/system01.dbf
input datafile file number=00002 name=/u03/app/oracle/oradata/db/sysaux01.dbf
input datafile file number=00005 name=/u03/app/oracle/oradata/db/example01.dbf
input datafile file number=00003 name=/u03/app/oracle/oradata/db/undotbs01.dbf
input datafile file number=00004 name=/u03/app/oracle/oradata/db/users01.dbf
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:26:26
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:21
piece handle=/u03/app/oracle/11.2.0/db/dbs/1kq4jv3i_1_1 tag=TAG20150417T122625
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:55
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:29:24
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:27
piece handle=/u03/app/oracle/11.2.0/db/dbs/1lq4jv91_1_1 tag=TAG20150417T122625
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 2015-04-17 12:29:27
Starting backup at 2015-04-17 12:29:27
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=31 RECID=24 STAMP=877264168
channel ORA_DISK_1: starting piece 1 at 2015-04-17 12:29:28
channel ORA_DISK_1: finished piece 1 at 2015-04-17 12:29:29
piece handle=/u03/app/oracle/11.2.0/db/dbs/1mq4jv98_1_1 tag=TAG20150417T122928
comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2015-04-17 12:29:29
RMAN-00572: waiting for DBMS_PIPE input

PL/SQL procedure successfully completed.

4.如果启动RMAN时使用了timeout选项,那么RMAN在指定时间间隔内没有接收到任何命令就会自动终止RMAN会话。为了强制RMAN立即终止RMAN会话可以发送exit命令。

SQL> declare
  2  i_v varchar2(2000):='exit;';
  3  o_v number;
  4  begin
  5  dbms_pipe.pack_message(i_v);
  6  o_v:=dbms_pipe.send_message('ORA$RMAN_ABC_IN');
  7  dbms_output.put_line(o_v);
  8  commit;
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL> declare
  2  output_v varchar2(4000);
  3  o_v number:=0;
  4  begin
  5  while(o_v=0) loop
  6   o_v:=dbms_pipe.receive_message('ORA$RMAN_ABC_OUT',10);--10是超过时间为10秒
  7   if o_v=0 then
  8    dbms_pipe.unpack_message(output_v);
  9     dbms_output.put_line(output_v);
 10   end if;
 11  end loop;
 12  commit;
 13  end;
 14  /
Recovery Manager complete.
PL/SQL procedure successfully completed.

2 thoughts on “使用Pipe来执行RMAN命令

发表评论

电子邮件地址不会被公开。