利用dbms_backup_restore恢复数据库

测试环境:OEL+11.2.0.1

实例名:orcl2
DBID:1336959433
场景:Oracle数据库的存储坏了,数据文件和控制文件全部丢失,只有数据文件的备份集,且备份集中无控制文件。(当然了,oracle10g和11g中是自动备份数据文件的)

备份数据库

首先将数据库进行全备

[oracle@DBA2 backup]$ rman target /
 
Recovery Manager: Release 11.2.0.1.0 - Production on Sun Sep 22 00:05:52 2013
 
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
 
connected to target database: ORCL2 (DBID=1336959433)
 
RMAN> backup database format '/backup/orcl_%U' plus archivelog;
 
 
Starting backup at 22-SEP-13
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=38 device type=DISK
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=1 RECID=16 STAMP=826760691
input archived log thread=1 sequence=2 RECID=9 STAMP=826760691
input archived log thread=1 sequence=3 RECID=8 STAMP=826760691
input archived log thread=1 sequence=4 RECID=10 STAMP=826760691
input archived log thread=1 sequence=5 RECID=11 STAMP=826760691
input archived log thread=1 sequence=6 RECID=17 STAMP=826760691
input archived log thread=1 sequence=7 RECID=19 STAMP=826760691
input archived log thread=1 sequence=8 RECID=18 STAMP=826760691
input archived log thread=1 sequence=9 RECID=13 STAMP=826760691
input archived log thread=1 sequence=10 RECID=12 STAMP=826760691
input archived log thread=1 sequence=11 RECID=15 STAMP=826760691
input archived log thread=1 sequence=12 RECID=14 STAMP=826760691
input archived log thread=1 sequence=13 RECID=21 STAMP=826760691
input archived log thread=1 sequence=14 RECID=22 STAMP=826760691
input archived log thread=1 sequence=15 RECID=20 STAMP=826760691
input archived log thread=1 sequence=16 RECID=7 STAMP=826760691
input archived log thread=1 sequence=17 RECID=4 STAMP=826760691
input archived log thread=1 sequence=18 RECID=5 STAMP=826760691
input archived log thread=1 sequence=19 RECID=1 STAMP=826760691
input archived log thread=1 sequence=20 RECID=2 STAMP=826760691
channel ORA_DISK_1: starting piece 1 at 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/u/oracle/flash_recovery_area/ORCL2/backupset/2013_09_22/o1_mf_annnn_TAG20130922T000600_93vjz8nh_.bkp tag=TAG20130922T000600 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:35
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=1 RECID=6 STAMP=826760691
input archived log thread=1 sequence=2 RECID=3 STAMP=826760691
channel ORA_DISK_1: starting piece 1 at 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/u/oracle/flash_recovery_area/ORCL2/backupset/2013_09_22/o1_mf_annnn_TAG20130922T000600_93vk288l_.bkp tag=TAG20130922T000600 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
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=1 RECID=23 STAMP=826761959
channel ORA_DISK_1: starting piece 1 at 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/u/oracle/flash_recovery_area/ORCL2/backupset/2013_09_22/o1_mf_annnn_TAG20130922T000600_93vk29cw_.bkp tag=TAG20130922T000600 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 22-SEP-13
 
Starting backup at 22-SEP-13
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=/oradata/orcl2/system01.dbf
input datafile file number=00002 name=/oradata/orcl2/undotbs01.dbf
input datafile file number=00003 name=/oradata/orcl2/sysaux01.dbf
input datafile file number=00005 name=/oradata/orcl2/seven.dbf
input datafile file number=00004 name=/oradata/orcl2/users01.dbf
channel ORA_DISK_1: starting piece 1 at 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/backup/orcl_04okeoqa_1_1 tag=TAG20130922T000738 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:02:05
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 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/backup/orcl_05okeou7_1_1 tag=TAG20130922T000738 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 22-SEP-13
 
Starting backup at 22-SEP-13
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=2 RECID=24 STAMP=826762186
channel ORA_DISK_1: starting piece 1 at 22-SEP-13
channel ORA_DISK_1: finished piece 1 at 22-SEP-13
piece handle=/u/oracle/flash_recovery_area/ORCL2/backupset/2013_09_22/o1_mf_annnn_TAG20130922T000946_93vk6b6o_.bkp tag=TAG20130922T000946 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 22-SEP-13
 
RMAN>
查看备份文件:
[oracle@DBA2 backup]$ ls
-rw-r-----  1 oracle oinstall 1182695424 Sep 22 00:09 orcl_04okeoqa_1_1
-rw-r-----  1 oracle oinstall   10158080 Sep 22 00:09 orcl_05okeou7_1_1

orcl_04okeoqa_1_1是数据文件的备份集,orcl_05okeou7_1_1是控制文件和参数文件的备份集。

制造实验场景

删除数据文件和控制文件:
[oracle@DBA2 oradata]$ cd /oradata/orcl2/
[oracle@DBA2 orcl2]$ ls
control01.ctl  redo02.log  seven.dbf     system01.dbf  temp02.dbf     users01.dbf
redo01.log     redo03.log  sysaux01.dbf  temp01.dbf    undotbs01.dbf
[oracle@DBA2 orcl2]$ rm *
[oracle@DBA2 orcl2]$ ls
[oracle@DBA2 orcl2]$ cd /u/oracle/flash_recovery_area/orcl2/
[oracle@DBA2 orcl2]$ ls
control02.ctl
[oracle@DBA2 orcl2]$ rm *
[oracle@DBA2 orcl2]$
数据文件和控制文件现已全部删除,数据库也会随之崩溃;
 

开始恢复

如果之前有参数文件,就用之前的参数文件,如果没有就自己手动修改init.ora文件,先将实例启动到nomount状态;
[oracle@DBA2 dbs]$ sql
 
SQL*Plus: Release 11.2.0.1.0 Production on Sun Sep 22 00:18:50 2013
 
Copyright (c) 1982, 2009, Oracle.  All rights reserved.
 
Connected to an idle instance.
 
SQL> startup nomount pfile='/u/oracle/product/11.2/dbs/initorcl2.ora'
ORACLE instance started.
 
Total System Global Area  418484224 bytes
Fixed Size                  1336932 bytes
Variable Size             327158172 bytes
Database Buffers           83886080 bytes
Redo Buffers                6103040 bytes
SQL>

备份集中有控制文件

如果备份集中有控制文件,可以先还原控制文件再还原数据文件;

还原控制文件

使用dbms_backup_restore将控制文件从备份集中构造出来:
SQL> declare
  2  devtype varchar2(256);
  3  done boolean;
  4  begin
  5  devtype:=sys.dbms_backup_restore.deviceallocate(type=>'',ident=>'t1');
  6  sys.dbms_backup_restore.restoresetdatafile;
  7  sys.dbms_backup_restore.restorecontrolfileto(cfname=>'/oradata/orcl2/control01.ctl');
  8  sys.dbms_backup_restore.restorebackuppiece(done=>done,handle=>'/backup/orcl_05okeou7_1_1',params=>null);
  9  sys.dbms_backup_restore.devicedeallocate;
 10  end;
 11  /
 
PL/SQL procedure successfully completed.
SQL>
 
可以在路径下看到控制文件被构造出来了。
[oracle@DBA2 orcl2]$ ls -al
-rw-r----- 1 oracle oinstall  10076160 Sep 22 00:53 control01.ctl
 
现在可以将实例启动到mount状态了。
SQL> alter database mount;
Database altered.
 
查看数据文件的file#号和绝对路径:
SQL> select file#,name from v$datafile;
 
     FILE# NAME
---------- ----------------------------------------
         1 /oradata/orcl2/system01.dbf
         2 /oradata/orcl2/undotbs01.dbf
         3 /oradata/orcl2/sysaux01.dbf
         4 /oradata/orcl2/users01.dbf
         5 /oradata/orcl2/seven.dbf
 
OK!控制文件恢复成功,且知道数据文件的file#和name。

还原数据文件

现在知道数据文件的file#和name后,还是利用dbms_backup_restore从备份集中还原数据文件:
SQL> declare
  2    devtype varchar2(256);
  3    done boolean;
  4    begin
  5    devtype:=sys.dbms_backup_restore.deviceAllocate (type=>'',ident=>'t1');
  6    sys.dbms_backup_restore.restoreSetDatafile;
  7    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>01,
  8    toname=>'/oradata/orcl2/system01.dbf');
  9    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>02,
 10    toname=>'/oradata/orcl2/undotbs01.dbf');
 11    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>03,
 12    toname=>'/oradata/orcl2/sysaux01.dbf');
 13    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>04,
 14    toname=>'/oradata/orcl2/users01.dbf');
 15    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>05,
 16    toname=>'/oradata/orcl2/seven.dbf');
 17    sys.dbms_backup_restore.restoreBackupPiece(done=>done,
 18    handle=>'/backup/orcl_04okeoqa_1_1',params=>null);
 19    sys.dbms_backup_restore.deviceDeallocate;
 20    end;
 21    /
 
PL/SQL procedure successfully completed.
查看数据文件路径下是否已经将数据文件还原到指定目录:
[oracle@DBA2 orcl2]$ ls -al
total 1480808
drwxrwxr-x 2 oracle oinstall      4096 Sep 22 00:52 .
drwxrwxr-x 4 oracle oinstall      4096 Sep 21 23:14 ..
-rw-r----- 1 oracle oinstall  10076160 Sep 22 00:54 control01.ctl
-rw-r----- 1 oracle oinstall  52436992 Sep 22 00:52 seven.dbf
-rw-r----- 1 oracle oinstall  94380032 Sep 22 00:52 sysaux01.dbf
-rw-r----- 1 oracle oinstall 723525632 Sep 22 00:53 system01.dbf
-rw-r----- 1 oracle oinstall 629153792 Sep 22 00:53 undotbs01.dbf
-rw-r----- 1 oracle oinstall   5251072 Sep 22 00:52 users01.dbf
确实,数据文件已经被还原至指定目录(没有临时表空间,因为rman备份的时候不会备份temp临时表空间);
 

备份集中无控制文件

还原数据文件

利用dbms_backup_restore从备份集中还原数据文件,当然这里并不知道数据文件的file#和name,一般name都会知道,只是file#无法确定,一般而言system是01号,undotbs是02号,sysaux是03号,users是04号,其他的就只能通过猜或者一个一个试了。当然之前能保存这些信息当然很好。
SQL> declare
  2    devtype varchar2(256);
  3    done boolean;
  4    begin
  5    devtype:=sys.dbms_backup_restore.deviceAllocate (type=>'',ident=>'t1');
  6    sys.dbms_backup_restore.restoreSetDatafile;
  7    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>01,
  8    toname=>'/oradata/orcl2/system01.dbf');
  9    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>02,
 10    toname=>'/oradata/orcl2/undotbs01.dbf');
 11    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>03,
 12    toname=>'/oradata/orcl2/sysaux01.dbf');
 13    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>04,
 14    toname=>'/oradata/orcl2/users01.dbf');
 15    sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>05,
 16    toname=>'/oradata/orcl2/seven.dbf');
 17    sys.dbms_backup_restore.restoreBackupPiece(done=>done,
 18    handle=>'/backup/ orcl_04okeoqa_1_1',params=>null);
 19    sys.dbms_backup_restore.deviceDeallocate;
 20    end;
 21    /
 
PL/SQL procedure successfully completed.
查看数据文件路径下是否已经将数据文件还原到指定目录:
[oracle@DBA2 orcl2]$ ls -al
total 1480808
drwxrwxr-x 2 oracle oinstall      4096 Sep 22 00:52 .
drwxrwxr-x 4 oracle oinstall      4096 Sep 21 23:14 ..
-rw-r----- 1 oracle oinstall  10076160 Sep 22 00:54 control01.ctl
-rw-r----- 1 oracle oinstall  52436992 Sep 22 00:52 seven.dbf
-rw-r----- 1 oracle oinstall  94380032 Sep 22 00:52 sysaux01.dbf
-rw-r----- 1 oracle oinstall 723525632 Sep 22 00:53 system01.dbf
-rw-r----- 1 oracle oinstall 629153792 Sep 22 00:53 undotbs01.dbf
-rw-r----- 1 oracle oinstall   5251072 Sep 22 00:52 users01.dbf
确实,数据文件已经被还原至指定目录(没有临时表空间,因为rman备份的时候不会备份temp临时表空间);

手动创建控制文件

接下来要手动创建控制文件,这个环节很简单,但前提是要知道有哪些数据文件
CREATE CONTROLFILE REUSE DATABASE "ORCL2" RESETLOGS  ARCHIVELOG
    MAXLOGFILES 16
    MAXLOGMEMBERS 3
    MAXDATAFILES 100
    MAXINSTANCES 8
    MAXLOGHISTORY 292
LOGFILE
  GROUP 1 '/oradata/orcl2/redo01.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 2 '/oradata/orcl2/redo02.log'  SIZE 50M BLOCKSIZE 512,
  GROUP 3 '/oradata/orcl2/redo03.log'  SIZE 50M BLOCKSIZE 512
DATAFILE
  '/oradata/orcl2/system01.dbf',
  '/oradata/orcl2/sysaux01.dbf',
  '/oradata/orcl2/undotbs01.dbf',
  '/oradata/orcl2/users01.dbf',
  '/oradata/orcl2/seven.dbf'
CHARACTER SET AL32UTF8
;
SQL> @/home/oracle/controlfile.txt


Control file created.

SQL>
控制文件恢复完成后,将数据库启动到mount状态,再执行不完全恢复。

不完全恢复

现在执行不完全恢复:
SQL> recover database using backup controlfile until cancel;
ORA-00279: change 1246394 generated at 09/22/2013 00:07:38 needed for thread 1
ORA-00289: suggestion :
/u/oracle/flash_recovery_area/ORCL2/archivelog/2013_09_22/o1_mf_1_2_93vk6b0w_.ar
c
ORA-00280: change 1246394 for thread 1 is in sequence #2
 
 
Specify log: {=suggested | filename | AUTO | CANCEL}
cancel
Media recovery cancelled.
SQL>
恢复完成,之后将实例打开,因为redo文件也丢失了,执行不完全恢复,要加resetlogs参数:
SQL> alter database open resetlogs;
 
Database altered.
 
SQL>
看看redo文件是否被重建?
[oracle@DBA2 orcl2]$ ls -al
total 1636644
drwxrwxr-x 2 oracle oinstall      4096 Sep 22 00:57 .
drwxrwxr-x 4 oracle oinstall      4096 Sep 21 23:14 ..
-rw-r----- 1 oracle oinstall  10076160 Sep 22 00:58 control01.ctl
-rw-r----- 1 oracle oinstall  52429312 Sep 22 00:58 redo01.log
-rw-r----- 1 oracle oinstall  52429312 Sep 22 00:57 redo02.log
-rw-r----- 1 oracle oinstall  52429312 Sep 22 00:57 redo03.log
-rw-r----- 1 oracle oinstall  52436992 Sep 22 00:57 seven.dbf
-rw-r----- 1 oracle oinstall  94380032 Sep 22 00:57 sysaux01.dbf
-rw-r----- 1 oracle oinstall 723525632 Sep 22 00:57 system01.dbf
-rw-r----- 1 oracle oinstall  10493952 Sep 22 00:57 temp01.dbf
-rw-r----- 1 oracle oinstall  10493952 Sep 22 00:57 temp02.dbf
-rw-r----- 1 oracle oinstall 629153792 Sep 22 00:57 undotbs01.dbf
-rw-r----- 1 oracle oinstall   5251072 Sep 22 00:57 users01.dbf
 
很好,redo文件也被重建,实例也启动起来了。但是实验的时候只用了一个控制文件,考虑到控制文件要多路复用,需要将实例关闭,修改参数文件,copy一个控制文件,这里就不再赘述了。




--------------------------------------------------------------------------------------------

版权所有,转载请注明作者及原文链接,否则追究法律责任!

QQ:      584307876

作者:    Seven

原文链接:http://blog.csdn.net/sunjiapeng/article/details/11890707

邮箱:     [email protected]

转载于:https://www.cnblogs.com/seven-dba/p/4354896.html

你可能感兴趣的:(利用dbms_backup_restore恢复数据库)