ITPub博客

首页 > Linux操作系统 > Linux操作系统 > RMAN恢复操作-转自Robinson_0612的专栏

RMAN恢复操作-转自Robinson_0612的专栏

原创 Linux操作系统 作者:shilei1 时间:2011-07-28 14:33:38 0 删除 编辑
恢复操作       
    1.非系统表空间的恢复步骤
        a. alter datafile n offline immediate; | alter tablespace tbs_name offline immediate;
        b. restore
        c. recover
        d. alter datafile n online; | alter tablespace tbs_name online
       
        --删除非系统表空间users的数据文件(数据库位于open 状态) 
            [oracle@oradb orcl]$ pwd
            /u01/oracle/oradata/orcl
            [oracle@oradb orcl]$ rm users01.dbf    
 
            lion@ORCL> select * from tb2;
            select * from tb2
                          *
            ERROR at line 1:
            ORA-01116: error in opening database file 4
            ORA-01110: data file 4: '/u01/oracle/oradata/orcl/users01.dbf'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> sql " alter tablespace users offline immediate "; 
            4> restore tablespace users;
            5> recover tablespace users;
            6> sql " alter tablespace users online ";
            7> }
 
            lion@ORCL> select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     1 Robinson
 
            也可以使用下面的恢复方式来完成恢复     
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> sql " alter database datafile 4 offline ";
            4> restore datafile 4;
            5> recover datafile 4;
            6> sql " alter database datafile 4 online ";
            7> }
 
        如果介质恢复时,需要用的日志尚未归档,需要指定联机重做日志文件所在的位置       
        注意
        表空间几种不同的脱机方式
            offline normal :
                表空间脱机的缺省方式,将位于SGA中且与该表空间所有的数据文件相关的数据块(blocks)写入到数据文件之后再进行脱机
                再次联机时不需要做介质恢复。
            offline temporary :
                实施检查点进程,即同样将SGA中且与该表空间所有的数据文件相关的数据块(blocks)写入到数据文件之后再进行脱机
                不保证所有的数据能够写入到数据文件。再次联机时要做介质恢复。
            offline immediate :
                不保证表空间的数据可用,也不实施检查点进程而直接脱机,再次联机时要做介质恢复。
               
            offline temporary ,offline immediate 脱机方式不适用于数据文件
       
            lion@ORCL> alter database datfile 4 offline immediate;
            alter database datfile 4 offline immediate
                                   *
            ERROR at line 1:
            ORA-02231: missing or invalid option to ALTER DATABASE
 
 
            lion@ORCL> alter database datfile 4 offline temporary;
            alter database datfile 4 offline temporary
                                   *
            ERROR at line 1:
            ORA-02231: missing or invalid option to ALTER DATABAS
       
            更多关于表空间与数据文件的管理,请参考:Oracle 表空间与数据文件
        对于表空间存在多个数据文件的情况,而单个或较少的数据文件受损,应尽可能使用第二种方式来恢复.即resotre & recover datafile n
 
    2.UNDO表空间的恢复
        关于UNDO表空间的管理请参考:Oracle 回滚(ROLLBACK)和撤销(UNDO)
        恢复步骤(undo丢失后)
            alter database datafile 2 online;
            alter database datafile 2 offline;
            select * from v$recover_file;
            restore datafile 2;   --用RMAN 完成
            
            recover datafile 2 ; 
            alter database datafile 2 online;
           
           
        --首先做一些操作,将数据填充到undo表空间
            lion@ORCL> select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     1 Jack
 
            lion@ORCL> insert into tb2 select 2,'Jackson' from dual;
 
            lion@ORCL> commit;
 
            lion@ORCL> delete from tb2 where id=1;
 
            lion@ORCL> select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
                    
            lion@ORCL> ho rm $ORACLE_BASE/oradata/orcl/undotbs01.dbf
 
        注意:undo表空间不能被offline,也不能被readonly
            使用alter database datafile 2 online | offline强制执行该步骤,以告知oracle undo表空间被损坏,执行后会话被终止,如下
 
            lion@ORCL> alter database datafile 2 online;
            alter database datafile 2 online
            *
            ERROR at line 1:
            ORA-01116: error in opening database file 2
            ORA-01110: data file 2: '/u01/oracle/oradata/orcl/undotbs01.dbf'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
 
            lion@ORCL> alter database datafile 2 offline;
            ERROR:
            ORA-03114: not connected to ORACLE
 
            alter database datafile 2 offline
            *
            ERROR at line 1:
            ORA-00603: ORACLE server session terminated by fatal error
   
        --接下面再来进行恢复
 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore datafile 2;
            4> recover datafile 2;}
 
            starting media recovery            --还原被成功执行,介质恢复失败,且会话被终止
            media recovery failed
            ORA-00603: ORACLE server session terminated by fatal error
 
        --重新登录到数据库服务器并查看v$recover_file视图以及进行介质恢复
            sys@ORCL> select * from v$recover_file;
 
                 FILE# ONLINE  ONLINE_ ERROR              CHANGE# TIME
            ---------- ------- ------- --------------- ---------- ---------
                     2 OFFLINE OFFLINE UNKNOWN ERROR       493982 23-NOV-10
        
            sys@ORCL> recover datafile 2;    --进行介质恢复,恢复后可以看到提示回话被终止
            ORA-00603: ORACLE server session terminated by fatal error
 
            sys@ORCL> conn / as sysdba   --再次连接
            Connected.
            sys@ORCL> select name,status from v$datafile where name like '%undo%';  --undo表空间已经处于offline状态
 
            NAME                                          STATUS
            --------------------------------------------- -------
            /u01/oracle/oradata/orcl/undotbs01.dbf        OFFLINE
 
            sys@ORCL> alter database datafile 2 online;
 
            Database altered.
 
            sys@ORCL> select * from lion.tb2;
                    ID NAME
            ---------- ---------------------------------------------
                     2 Jackson
 
        可以看到表中ID为的记录已经丢失,事实上在undo的数据文件丢失前,该事务并没有提交,由此可以推断,该事务进行了隐式提交.
                    
    3.系统表空间的恢复(system ,sysaux)
        系统表空间只能在Mount状态下来完成恢复,步骤如下
            startup mount;
            restore datafile 1;
            recover datafile 1;
            alter database open;
        --创建新表tb3,并插入记录。其数据字典信息则位于system表空间,数据内容位于users表空间
            lion@ORCL> create table tb3 tablespace users as select * from tb2;
 
            lion@ORCL> insert into tb3 select 1,'Johnson' from dual;
 
            lion@ORCL> commit;
 
            lion@ORCL> select * from tb3 order by id;
 
                    ID NAME
            ---------- ---------------------------------------------
                     1 Johnson
                     2 Jackson
        --删除表空间system01.dbf,  sysaux01.dbf           
            sys@ORCL> ho rm $ORACLE_BASE/oradata/orcl/system01.dbf
            sys@ORCL> ho rm $ORACLE_BASE/oradata/orcl/sysaux01.dbf     
            sys@ORCL> startup mount force; 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore  datafile 1,3;
            4> recover  datafile 1,3;
            5> alter database open;
            6> release channel ch1;}
 
            sys@ORCL> select * from lion.tb3 order by id;  --成功恢复后表tb3也被恢复
 
                    ID NAME
            ---------- ---------------------------------------------
                     1 Johnson
                     2 Jackson
                    
    4.控制文件的恢复
        步骤
            connect to target db and catalog(nocatalog) db
            startup nomount
            restore controlfile [from autobackup]
            alter database mount
            recover database
            alter database open resetlogs
           
        由于控制文件采取了自动备份策略,因此在每次备份或重大系统结果发生变化时,控制文件将被自动备份
           
            sys@ORCL> ho rm $ORACLE_BASE/oradata/orcl/*.ctl                --删除所有的控制文件*/
           
            sys@ORCL> select file#,status from v$datafile;   --查看v$datafile视图时,系统已检测到错误发生
            select file#,status from v$datafile
                                     *
            ERROR at line 1:
            ORA-00210: cannot open the specified control file
            ORA-00202: control file: '/u01/oracle/oradata/orcl/control01.ctl'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
           
            sys@ORCL> shutdown abort;
           
            --重新连接到RMAN,注意连接target时使用/,否则提示TNS无法解析
            [oracle@oradb dbs]$ uniread rman target / catalog rman/rman@asmdb  
 
            connected to target database: orcl (not started)
            connected to recovery catalog database     
           
            RMAN> startup nomount;
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore controlfile;
            4> sql " alter database mount ";
            5> recover database;
            6> sql " alter database open resetlogs ";
            7> release channel ch1;}
            
        注:在此处有可能不要介质恢复,如果提示需要介质恢复,直接在RMAN或SQLPlus下执行recover database即可
            使用open resetlogs之后,一个新的incarnation将被生成,再此建议立即全备数据库。
       
        更多关于控制文件
            Oracle 控制文件(CONTROLFILE)
            Oracle 控制文件的备份与恢复
   
    5.联机重做日志文件的恢复(online redo log )
        当数据库置为mount状态,且将要转换为open状态时,数据文件,联机日志文件被打开,因此联机日志的丢失可以在mount状态完成
        恢复步骤
            a. 启动到mount状态(startup mount force)
            b. 还原数据库(restore database)
            c. 恢复数据库(recover database)
       
        下面对删除日志并进行恢复
            lion@ORCL> select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
 
            lion@ORCL> select current_scn from v$database;   --查看数据库当前的SCN
 
            CURRENT_SCN
            -----------
                1020638
 
            lion@ORCL> insert into tb2 select 1,'Johnson' from dual;   --为表tb2新增一条记录
 
            lion@ORCL> commit;
 
            lion@ORCL> select current_scn from v$database;             --数据库当前的SCN发生了变化为
 
            CURRENT_SCN
            -----------
                1020685
               
            lion@ORCL> select file#, checkpoint_change# from v$datafile_header;  --数据文件头部的checkpoint_change
 
                 FILE# CHECKPOINT_CHANGE#
            ---------- ------------------
                     1            1020368
                     2            1020368
                     3            1020368
                     4            1020368
                     5            1020368
                     6            1020368      
                    
            lion@ORCL> ho rm -f $ORACLE_BASE/oradata/orcl/*.log      --删除所有的日志文件  */
                       
            lion@ORCL> insert into tb2 select 2,'wilson' from dual;    --为表插入新记录
 
            lion@ORCL> commit;                                        
 
            lion@ORCL> select current_scn from v$database;            --数据库当前的SCN发生了变化为
 
            CURRENT_SCN
            -----------
                1020708        
 
            lion@ORCL> alter system archive log current;              --对日志进行归档时提示错误发生
            alter system archive log current
            *
            ERROR at line 1:
            ORA-16038: log 1 sequence# 1 cannot be archived
            ORA-00312: online log 1 thread 1: '/u01/oracle/oradata/orcl/redo01.log'
               
            lion@ORCL> conn / as sysdba
 
            sys@ORCL> startup mount force;
 
            [oracle@oradb ~]$ uniread rman target / catalog rman/rman@asmdb    --退出RMAN后并重新连接
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore database;
            4> recover database;
            5> release channel ch1;}
 
            RMAN-06054: media recovery requesting unknown log: thread 1 seq 1 lowscn 1020365
 
            sys@ORCL> recover database until cancel;                          --回到SQLPlus直接使用until cancel来进行恢复
 
            sys@ORCL> alter database open resetlogs;                          --执行opensetlogs打开数据库
 
            sys@ORCL> select * from lion.tb2;                          --在日志未完成自动归档前,删除日志的后数据全部丢失
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
                    
        关于单个日志文件丢失或日志文件组受损,请参考:Oracle 联机重做日志文件(ONLINE LOG FILE)     
       
    6.所有数据全部丢失的处理
        步骤
            a.启动到nomount状态(startup nomount)
            b.还原控制文件(restore controlfile from autobackup)
            c.还原数据(restore database)
            d.将数据切换到mount状态(alter database mount)
            e.恢复数据库(recover database using backup controlfile until cancel)
            f.使用open resetlogs打开数据库(alter database open resetlogs)
           
        --下面演示数据文件、日志文件、控制文件全部丢失的处理
            sys@ORCL> ho rm $ORACLE_BASE/oradata/orcl/*           --删除所有的数据文件、日志文件、控制文件*/
 
            sys@ORCL> startup nomount ;
           
            [oracle@oradb ~]$ uniread rman target / catalog rman/rman@asmdb
            RMAN> startup nomount;     
            RMAN> restore controlfile from autobackup; 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore database ;
            4> sql " alter database mount ";
            5> recover database ;}
 
            RMAN-06004: ORACLE error from recovery catalog database: RMAN-20003: target database incarnation not found in
                recovery catalog
 
            idle> recover database using backup controlfile until cancel;    --使用SQLPlus来完成恢复操作
 
            idle> alter database open resetlogs;

 

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/196700/viewspace-703369/,如需转载,请注明出处,否则将追究法律责任。

上一篇: RMAN简明使用手册
请登录后发表评论 登录
全部评论

注册时间:2018-10-10

  • 博文量
    548
  • 访问量
    39020