How to Extract Controlfiles, Datafiles, and Archived Logs from RMAN Backupsets

作者: Maclean Liu , post on June 9th, 2008 , English Version
【本站文章除注明转载外,均为本站原创编译】
转载请注明:文章转载自: Oracle Clinic – Maclean Liu的个人技术博客 [http://www.oracledatabase12g.com/]
本文标题: How to Extract Controlfiles, Datafiles, and Archived Logs from RMAN Backupsets
本文永久地址: http://www.oracledatabase12g.com/archives/how-to-extract-controlfiles-datafiles-and-archived-logs-from-rman-backupsets.html
Introduction:

  When using RMAN to restore objects (datafiles, controlfiles, or archivelogs)
  from backupsets, the object restore can be driven from the recovery catalog
  or the target database controlfile. This note explains how to extract
  objects from backupsets when the recovery catalog and controlfiles have been
  lost. In this scenario, you effectively perform the RMAN functions through
  PL/SQL procedure calls

Contents:

  1. Prerequisites
  2. Extracting the controlfile from a backupset
  3. Extracting datafiles from a backupset
  4. Applying incrementals
  5. Extracting archivelogs from a backupset
  6. A typical scenario
  7. Errors
  8. Things to be done

1. Prerequisites

  The customer must have a knowledge of the contents of backupsets i.e. what
  they contain, when the backups were created, and the type of backups. Ideally
  they should have logs of the RMAN backup sessions that produced the
  backupsets.

  Note that the following anonymous PL/SQL blocks are run on the instance of
  the database being recovered (the 'target'). The instance must be at least
  started to nomount mode(once the controlfile has been restored the database
  can also be mounted). Anonymous blocks can be executed in this manner as long
  as they call only 'fixed' packages. The DBMS_BACKUP_RESTORE packages are
  fixed.

  IMPORTANT: All the anonymous blocks must be executed by SYS or a user
             who has execute privilege on SYS.DBMS_BACKUP_RESTORE

2. Extracting the controlfile from a backupset

  The first stage is to extract the controlfile from a backupset. This is
  achieved by making use of the following SYS.DBMS_BACKUP_RESTORE packaged
  functions & procedures:

    FUNCTION deviceAllocate		- allocates a device for sequential I/O
    PROCEDURE restoreSetDataFile	- begins a restore conversation
    PROCEDURE restoreControlfileTo	- specifies the controlfile destination
    PROCEDURE restoreBackupPiece 	- performs the restore
    PROCEDURE deviceDeallocate		- deallocates the I/O device

  The following anonymous block can be created and executed to restore a
  controlfile from a backupset. Before executing it, you MUST edit the block
  as follows:

    a. The filetable PL/SQL table entries must reflect the backuppieces
       comprising the backupset
    b. The v_maxPieces variable must reflect the number of backuppieces
       comprising the backupset
    c. The call to restoreControlfileTo must specify the correct controlfile
       path & filename

  IMPORTANT: The latest backup of the controlfile should be restored. Because
             recovery (using backup controlfile) will be performed manually,
             the recovering session will need to start applying redo from
             the current log sequence AT THE TIME OF THE CONTROLFILE BACKUP.
             Thus, to take advantage of incremental backups, restore a
             controlfile taken along with the incremental backups, thus
             reducing the amount of redo required during recovery.

DECLARE
  v_dev 	varchar2(50);	-- device type allocated for restore
  v_done 	boolean;	-- has the controlfile been fully extracted yet
  type t_fileTable is table of varchar2(255)
    index by binary_integer;
  v_fileTable 	t_fileTable;	-- Stores the backuppiece names pertaining to a single backupset like wise one backupset is processed at at time
  v_maxPieces 	number:=1;	-- Number of backuppieces in backupset
BEGIN

-- Initialise the filetable and number of backup pieces in the backupset
-- This section of code MUST be edited to reflect the customer's available
-- backupset before the procedure is compiled and run. In this example, the
-- backupset consists of 4 pieces:

  v_fileTable(1):='fulldb_s15_p1';
  v_fileTable(2):='fulldb_s15_p2';
  v_fileTable(3):='fulldb_s15_p3';
  v_fileTable(4):='fulldb_s15_p4';
  v_maxPieces:=4;

-- Allocate a device. In this example, I have specified 'sbt_tape' as I am
-- reading backuppieces from the media manager. If the backuppiece is on disk,
-- specify type=>null

  v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
   					        ident=>'t1');

-- Here is an example when parameters are needed to be defined to restore
-- from tape:

   v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
     ident=>'t1',params=>'BLKSIZE=262144,ENV=(CvOraSID=TEST)');

-- Begin the restore conversation

  sys.dbms_backup_restore.restoreSetDatafile;

-- Specify where the controlfile is to be recreated

  sys.dbms_backup_restore.restoreControlfileTo(cfname=>'/support2/OFA_V804/u1/oradata/dbs/ctrl1V804.ctl');

-- Restore the controlfile

  FOR i IN 1..v_maxPieces LOOP
    sys.dbms_backup_restore.restoreBackupPiece(done=>v_done,
					       handle=>v_fileTable(i),
					       params=>null);
    IF v_done THEN
      GOTO all_done;
    END IF;
  END LOOP;

<<all_done>>
-- Deallocate the device

  sys.dbms_backup_restore.deviceDeallocate;

END;
/

3. Extracting datafiles from a backupset

  The second stage is to extract the datafiles from a backupset. This is
  achieved by making use of the following SYS.DBMS_BACKUP_RESTORE packaged
  functions & procedures:

    FUNCTION deviceAllocate             - allocates a device for sequential I/O
    PROCEDURE restoreSetDataFile        - begins a restore conversation
    PROCEDURE restoreDataFileTo      	- datafile number & destination
    PROCEDURE restoreBackupPiece        - performs the restore
    PROCEDURE deviceDeallocate          - deallocates the I/O device

  The following anonymous block can be created and executed to restore a
  datafile from a backupset. Before executing it, you MUST edit the block
  as follows:

    a. The filetable PL/SQL table entries must reflect the backuppieces
       comprising the backupset
    b. The v_maxPieces variable must reflect the number of backuppieces
       comprising the backupset
    c. The call to restoreDataFileTo must specify the correct datafile number,
       and datafile path & filename

DECLARE
  v_dev		varchar2(50);		-- device type allocated for restore
  v_done	boolean:=false; -- has the datafile been fully extracted yet
  type t_fileTable is table of varchar2(255)
    index by binary_integer;
  v_fileTable 	t_fileTable;		-- Stores the backuppiece names
  v_maxPieces 	number:=1;		-- Number of backuppieces in backupset
BEGIN

-- Initialise the filetable and number of backup pieces in the backupset
-- This section of code MUST be edited to reflect the customer's available
-- backupset before the procedure is compiled and run. In this example, the
-- backupset consists of 4 pieces:

  v_fileTable(1):='fulldb_s15_p1';
  v_fileTable(2):='fulldb_s15_p2';
  v_fileTable(3):='fulldb_s15_p3';
  v_fileTable(4):='fulldb_s15_p4';
  v_maxPieces:=4;

-- Allocate a device. In this example, I have specified 'sbt_tape' as I am
-- reading backuppieces from the media manager. If the backuppiece is on disk,
-- specify type=>null

  v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
					        ident=>'t1');

-- Here is an example when parameters are needed to be defined to restore
-- from tape:

   v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
     ident=>'t1',params=>'BLKSIZE=262144,ENV=(CvOraSID=TEST)');

-- Begin the restore conversation

  sys.dbms_backup_restore.restoreSetDatafile;

-- Specify where the datafile is to be recreated

  sys.dbms_backup_restore.restoreDataFileTo(dfnumber=>1,
		toname=>'/support2/OFA_V804/u1/oradata/dbs/sysV804.dbf');

-- Restore the datafile

  FOR i IN 1..v_maxPieces LOOP
    sys.dbms_backup_restore.restoreBackupPiece(done=>v_done,
					       handle=>v_fileTable(i),
					       params=>null);
    IF v_done THEN
      GOTO all_done;
    END IF;
  END LOOP;

<<all_done>>
-- Deallocate the device
  sys.dbms_backup_restore.deviceDeallocate;

END;
/

4. Applying incrementals

  If incrementals are to be applied, you must execute this anonymous block
  for each incremental datafile backup. The following SYS.DBMS_BACKUP_RESTORE
  packaged functions & procedures are called:

    FUNCTION deviceAllocate             - allocates a device for sequential I/O
    PROCEDURE applySetDataFile          - begins a restore conversation
    PROCEDURE applyDataFileTo           - datafile number & destination
    PROCEDURE applyBackupPiece          - performs the restore
    PROCEDURE deviceDeallocate          - deallocates the I/O device

  The following anonymous block can be created and executed to restore a
  datafile from a backupset. Before executing it, you MUST edit the block
  as follows:

    a. The filetable PL/SQL table entries must reflect the backuppieces
       comprising the backupset
    b. The v_maxPieces variable must reflect the number of backuppieces
       comprising the backupset
    c. The call to applyDataFileTo must specify the correct datafile number,
       and datafile path & filename

DECLARE
  v_dev           varchar2(50);           -- device type allocated for restore
  v_done          boolean:=false;  -- has the datafile been fully extracted yet
  type t_fileTable is table of varchar2(255)
    index by binary_integer;
  v_fileTable     t_fileTable;            -- Stores the backuppiece name
  v_maxPieces     number:=1;              -- Number of backuppieces in backupset
BEGIN

-- Initialise the filetable and number of backup pieces in the backupset
-- This section of code MUST be edited to reflect the customer's available
-- backupset before the procedure is compiled and run. In this example, the
-- backupset consists of 1 piece, a level 2 backupset:

  v_fileTable(1):='fulldb_level2_s18_p1';
  v_maxPieces:=1;

-- Allocate a device. In this example, I have specified 'sbt_tape' as I am
-- reading backuppieces from the media manager. If the backuppiece is on disk,
-- specify type=>null

  v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
                                                ident=>'t1');

-- Here is an example when parameters are needed to be defined to restore
-- from tape:

   v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
     ident=>'t1',params=>'BLKSIZE=262144,ENV=(CvOraSID=TEST)');

-- Begin the restore conversation

  sys.dbms_backup_restore.applySetDataFile;

-- Specify where the datafile is to be recreated

  sys.dbms_backup_restore.applyDataFileTo(dfnumber=>1,
                toname=>'/support2/OFA_V804/u1/oradata/dbs/sysV804.dbf');

-- Restore the datafile
  FOR i IN 1..v_maxPieces LOOP
    sys.dbms_backup_restore.applyBackupPiece(done=>v_done,
                                             handle=>v_fileTable(i),
                                             params=>null);
    IF v_done THEN
      GOTO all_done;
    END IF;
  END LOOP;
<<all_done>>
-- Deallocate the device
  sys.dbms_backup_restore.deviceDeallocate;

END;
/

5. Extracting archivelogs from a backupset

  The last restore stage is to extract the archivelogs from a backupset. This is
  achieved by making use of the following SYS.DBMS_BACKUP_RESTORE packaged
  functions & procedures:

    FUNCTION deviceAllocate             - allocates a device for sequential I/O
    PROCEDURE restoreSetArchivedLog     - begins a restore conversation
    PROCEDURE restoreArchivedLog        - archivelog sequence & thread numbers
    PROCEDURE restoreBackupPiece        - performs the restore
    PROCEDURE deviceDeallocate          - deallocates the I/O device

  The following anonymous block can be created and executed to restore
  an archivelog from a backupset. Before executing it, you MUST edit the block
  as follows:

    a. The filetable PL/SQL table entries must reflect the backuppieces
       comprising the backupset
    b. The v_maxPieces variable must reflect the number of backuppieces
       comprising the backupset
    c. The call to restoreSetArchivedLog must specify the destination
       where the archivelog is to be restored. Ideally the destination string
       should be the same as init.ora:log_archive_dest
    d. The call to restoreArchivedLog must specify the log sequence number
       and thread number of the archivelog

DECLARE
  v_dev  	varchar2(50);	-- device type allocated for restore
  v_done 	boolean:=false; -- has the log been fully extracted yet
  type t_fileTable is table of varchar2(255)
    index by binary_integer;
  v_fileTable 	t_fileTable;	-- Stores the backuppiece names
  v_maxPieces 	number:=1;	-- Number of backuppieces in backupset
BEGIN

-- Initialise the filetable and number of backup pieces in the backupset
-- This section of code MUST be edited to reflect the customer's available
-- backupset before the procedure is compiled and run. In this example, the
-- archivelog backupset consists of 2 pieces:

   v_fileTable(1):='al_s20_p1';
   v_fileTable(2):='al_s20_p2';
   v_maxPieces:=2;

-- Allocate a device. In this example, I have specified 'sbt_tape' as I am
-- reading backuppieces from the media manager. If the backuppiece is on disk,
-- specify type=>null

  v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
					        ident=>'t1');

-- Here is an example when parameters are needed to be defined to restore
-- from tape:

   v_dev:=sys.dbms_backup_restore.deviceAllocate(type=>'sbt_tape',
     ident=>'t1',params=>'BLKSIZE=262144,ENV=(CvOraSID=TEST)');

-- Begin the restore conversation

  sys.dbms_backup_restore.restoreSetArchivedLog(destination=>'/support2/OFA_V804/app/oracle/admin/arch/arch_');

-- Specify where the archivelog is to be recreated

  sys.dbms_backup_restore.restoreArchivedLog(thread=>1,
					     sequence=>100);

-- Restore the archivelog

  FOR i IN 1..v_maxPieces LOOP
    sys.dbms_backup_restore.restoreBackupPiece(done=>v_done,
					       handle=>v_fileTable(i),
					       params=>null);
    IF v_done THEN
      GOTO all_done;
    END IF;
  END LOOP;

<<all_done>>
-- Deallocate the device
  sys.dbms_backup_restore.deviceDeallocate;

END;
/

  For restoring multiple archives from a backupset, add a loop
  around  sys.dbms_backup_restore.restoreArchivedLog()

  for seq in <min seq#>..<max seq#> loop
    sys.dbms_backup_restore.restoreArchivedLog(thread=>1,
                                             sequence=>seq);
  end loop

6. A typical scenario

  A customer has backupsets consisting of:
    o. an incremental level 0 database backup
    o. an incremental level 2 database backup
    o. archivelogs from the time of the level 2 backup to the current time
  The target database and recovery catalog have been irretrievably lost.

  In this situation, the following steps should be followed (using the
  above anonymous blocks):
    1. Start the target instance (nomount)
    2. Restore the latest controlfile, ideally from the same backupset as
       the last incremental to be restored (make further copies if necessary
       as per the init.ora)
    3. Mount the database
    4. Restore the datafiles from the level 0 backupset
    5. Restore (apply) the datafiles from the level 2 backupset
    6. Restore the archivelogs from the archivelog backupset
    7. Using tradtional v7 recovery techniques, recover the database
       (until cancel using backup controlfile)
    8. Open the database (resetlogs)
    9. Rebuild the recovery catalog & re-register the target database
   10. Make backups of the target database and recovery catalog database

8. Errors

  8.1 ORA-19615 & ORA-19613 when attempting to extract files

     Errorstack:
       ORA-19583: conversation terminated due to error
       ORA-19615: some files not found in backup set
       ORA-19613: datafile <file#> not found in backup set
       ORA-06512: at "SYS.X$DBMS_BACKUP_RESTORE", line 1043
       ORA-06512: at line 40

     The problem is that one or more backup pieces specified in the
     v_fileTable table contain NO blocks for the datafile that you are
     trying to extract.

     For example, I may have run an RMAN backup and allocated 2 channels to
     backup the (4 datafile) database. This will create 2 backupsets.

                                                 +- Backup piece 1a
                 +- (Backupset 1) Datafiles 1,2 -+
                 |                               +- Backup piece 2a
       Database -+
                 |                               +- Backup piece 1b
                 +- (Backupset 2) Datafiles 3,4 -+
                                                 +- Backup piece 2b

     Although the backup pieces may contain blocks from all datafiles
     associated with their backupset, they will not contain blocks from a
     different backupset i.e. pieces 1a and 1b will NOT contain blocks from
     datafiles 3 or 4.

     If I want to restore datafile 1, and include either backup pieces 1b or
     2b in v_fileTable, I will get the errorstack above.

     This is why it is important to know what files are in what backupset.
     The original RMAN backup log will help here.

8. Things to be done

  8.1. Error handling
       If the procedures fail with an unhandled exception (quite likely, as
       no exception handlers have been set up), the allocated device does not
       get deallocated. This is unfriendly (the user must exit & restart the
       session) and will be addressed

© 2008, www.oracledatabase12g.com. 版权所有.文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.

相关文章 | Related posts:

  1. RMAN Performance Tuning Using Buffer Memory Parameters
  2. Lost Redo Logs And Archive Logs And Need To Apply Media Recovery ** INTERNAL ONLY **
  3. How to Minimise the impact of an Online RMAN Backup on an OLTP system
  4. How we created a clone database from the datafiles image copies of a physical standby database
  5. REDO LOG CORRUPTION – DROPPING REDO LOGS NOT POSSIBLE – CLEAR LOGFILE
  6. Collecting Diagnostic information for Oracle block corruption
  7. Understanding Bootstrap Of Oracle Database
  8. Debugging OCFS on Linux
  9. THE GAINS AND PAINS OF NOLOGGING OPERATIONS
  10. Oracle Block Cache Layer And Its Corresponding Corruption Error Messages.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>