使用Pipe來執行RMAN命令

eric0435發表於2015-04-17

RMAN Pipe介面是執行RMAN命令並接收命令輸出的一種替代方法。使用這種介面,RMAN使用dbms_pipe包來獲得命令併傳送輸出來代替作業系統shell。使用這種介面,可以編寫可移值的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.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-1583347/,如需轉載,請註明出處,否則將追究法律責任。

相關文章