第九章:Rman 完全恢复

1、recover 恢复:
   1)归档 :完全恢复和不完全恢复
   2)非归档:只能恢复到最后一次备份状态

2、完全恢复:


----先对数据库做冷备
RMAN> list backup;                                                                                                                      


RMAN> run {                                                                                                                             
2> shutdown immediate;                                                                                                                  
3> startup mount;                                                                                                                       
4> backup database format '/disk1/rman/prod/%d_%s.bak';                                                                                 
5> alter database open;                                                                                                                 
6> }      

RMAN> list backup;                                                                                                                      


List of Backup Sets
===================

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
28      Full    589.39M    DISK        00:00:50     18-AUG-11     
        BP Key: 28   Status: AVAILABLE  Compressed: NO  Tag: TAG20110818T071905
        Piece Name: /disk1/rman/prod/PROD_31.bak
  List of Datafiles in backup set 28
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/system01.dbf
  2       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/users01.dbf
  3       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/sysaux01.dbf
  4       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/index01.dbf
  5       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/example01.dbf
  6       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/test01.dbf
  7       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/undo_tbs01.dbf
  8       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/test02.dbf
  9       Full 1321672    18-AUG-11 /u01/app/oracle/oradata/prod/cuug01.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
29      Full    6.89M      DISK        00:00:01     18-AUG-11     
        BP Key: 29   Status: AVAILABLE  Compressed: NO  Tag: TAG20110818T072001
        Piece Name: /disk1/recovery/prod/PROD/autobackup/2011_08_18/o1_mf_s_759482322_74sxc274_.bkp
  Control File Included: Ckp SCN: 1321672      Ckp time: 18-AUG-11
  SPFILE Included: Modification time: 18-AUG-11
   

案例1:数据库大部分datafile丢失(recover database,必须在mount下)
 
1)测试环境
07:21:12 SQL> conn scott/tiger                                                                                                          
Connected.
07:21:16 SQL>
07:21:16 SQL> select * from tab;                                                                                                        

TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
DEPT                           TABLE
EMP                            TABLE
BONUS                          TABLE
SALGRADE                       TABLE
SYS_TEMP_FBT                   TABLE
TEST                           TABLE
EMP1                           TABLE

7 rows selected.

07:21:20 SQL> select * from test;                                                                                                       

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

16 rows selected.

07:21:27 SQL> insert into test select * from test;                                                                                      

16 rows created.

07:21:43 SQL> commit;                                                                                                                   

Commit complete.

07:21:45 SQL> select count(*) from test;                                                                                                

  COUNT(*)
----------
        32

07:21:50 SQL> conn /as sysdba                                                                                                           
Connected.
07:21:56 SQL>

07:22:03 SQL> select * from v$log;                                                                                                      

    GROUP#    THREAD#  SEQUENCE#      BYTES    MEMBERS ARC STATUS           FIRST_CHANGE# FIRST_TIM
---------- ---------- ---------- ---------- ---------- --- ---------------- ------------- ---------
         1          1          8   52428800          1 NO  CURRENT                1321561 18-AUG-11
         2          1          7   52428800          1 YES INACTIVE               1321559 18-AUG-11
         3          1          6   52428800          1 YES INACTIVE               1316062 18-AUG-11

07:22:09 SQL> alter system switch logfile;                                                                                              

System altered.

07:22:15 SQL>
07:22:15 SQL> shutdown abort                                                                                                            
ORACLE instance shut down.
07:23:03 SQL> !                                                                                                                         
[oracle@work ~]$ rm /u01/app/oracle/oradata/prod/*.dbf

2)启动database
[oracle@work ~]$ !sql
sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Aug 18 07:23:17 2011

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

Connected to an idle instance.

07:23:17 SQL>
07:23:17 SQL> startup                                                                                                                   
ORACLE instance started.

Total System Global Area  314572800 bytes
Fixed Size                  1219184 bytes
Variable Size              79693200 bytes
Database Buffers          230686720 bytes
Redo Buffers                2973696 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: '/u01/app/oracle/oradata/prod/system01.dbf'


07:23:25 SQL> select file#,error from v$recover_file;                                                                                   

     FILE# ERROR
---------- -----------------------------------------------------------------
         1 FILE NOT FOUND
         2 FILE NOT FOUND
         3 FILE NOT FOUND
         4 FILE NOT FOUND
         5 FILE NOT FOUND
         6 FILE NOT FOUND
         7 FILE NOT FOUND
         8 FILE NOT FOUND
         9 FILE NOT FOUND

9 rows selected.

07:23:38 SQL>  
 
 3)利用rman 恢复database
 [oracle@work ~]$ rman target /

Recovery Manager: Release 10.2.0.1.0 - Production on Thu Aug 18 07:24:09 2011

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

connected to target database: PROD (DBID=170319990, not open)

RMAN> run {                                                                                                                             
2> startup force mount;                                                                                                                 
3> allocate channel c1 type disk;                                                                                                       
4> allocate channel c2 type disk;                                                                                                       
5> restore database;                                                                                                                    
6> recover database;                                                                                                                    
7> alter database open;                                                                                                                 
8> }                
Oracle instance started
database mounted

Total System Global Area     314572800 bytes

Fixed Size                     1219184 bytes
Variable Size                 79693200 bytes
Database Buffers             230686720 bytes
Redo Buffers                   2973696 bytes

using target database control file instead of recovery catalog
allocated channel: c1
channel c1: sid=157 devtype=DISK

allocated channel: c2
channel c2: sid=154 devtype=DISK

Starting restore at 18-AUG-11

channel c1: starting datafile backupset restore
channel c1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/app/oracle/oradata/prod/system01.dbf
restoring datafile 00002 to /u01/app/oracle/oradata/prod/users01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/prod/sysaux01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/prod/index01.dbf
restoring datafile 00005 to /u01/app/oracle/oradata/prod/example01.dbf
restoring datafile 00006 to /u01/app/oracle/oradata/prod/test01.dbf
restoring datafile 00007 to /u01/app/oracle/oradata/prod/undo_tbs01.dbf
restoring datafile 00008 to /u01/app/oracle/oradata/prod/test02.dbf
restoring datafile 00009 to /u01/app/oracle/oradata/prod/cuug01.dbf
channel c1: reading from backup piece /disk1/rman/prod/PROD_31.bak
channel c1: restored backup piece 1
piece handle=/disk1/rman/prod/PROD_31.bak tag=TAG20110818T071905
channel c1: restore complete, elapsed time: 00:00:57
Finished restore at 18-AUG-11

Starting recover at 18-AUG-11

starting media recovery
media recovery complete, elapsed time: 00:00:03

Finished recover at 18-AUG-11

database opened
released channel: c1
released channel: c2

RMAN>

告警日志信息:
Full restore complete of datafile 6 /u01/app/oracle/oradata/prod/test01.dbf.  Elapsed time: 0:00:01
  checkpoint is 1321672
Full restore complete of datafile 8 /u01/app/oracle/oradata/prod/test02.dbf.  Elapsed time: 0:00:02
  checkpoint is 1321672
Full restore complete of datafile 9 /u01/app/oracle/oradata/prod/cuug01.dbf.  Elapsed time: 0:00:01
  checkpoint is 1321672
Thu Aug 18 07:25:40 2011
Full restore complete of datafile 2 /u01/app/oracle/oradata/prod/users01.dbf.  Elapsed time: 0:00:16
  checkpoint is 1321672
Full restore complete of datafile 4 /u01/app/oracle/oradata/prod/index01.dbf.  Elapsed time: 0:00:18
  checkpoint is 1321672
Full restore complete of datafile 5 /u01/app/oracle/oradata/prod/example01.dbf.  Elapsed time: 0:00:20
  checkpoint is 1321672
Full restore complete of datafile 7 /u01/app/oracle/oradata/prod/undo_tbs01.dbf.  Elapsed time: 0:00:21
  checkpoint is 1321672
Thu Aug 18 07:26:00 2011
Full restore complete of datafile 3 /u01/app/oracle/oradata/prod/sysaux01.dbf.  Elapsed time: 0:00:35
  checkpoint is 1321672
Thu Aug 18 07:26:12 2011
Full restore complete of datafile 1 /u01/app/oracle/oradata/prod/system01.dbf.  Elapsed time: 0:00:47
  checkpoint is 1321672
Thu Aug 18 07:26:20 2011
alter database recover datafile list clear
Thu Aug 18 07:26:20 2011
Completed: alter database recover datafile list clear
Thu Aug 18 07:26:21 2011
alter database recover datafile list
 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
Completed: alter database recover datafile list
 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
Thu Aug 18 07:26:21 2011
alter database recover if needed
 start
Media Recovery Start
Thu Aug 18 07:26:21 2011
Recovery of Online Redo Log: Thread 1 Group 1 Seq 8 Reading mem 0
  Mem# 0 errs 0: /u01/app/oracle/oradata/prod/redo01.log
Thu Aug 18 07:26:22 2011
Recovery of Online Redo Log: Thread 1 Group 3 Seq 9 Reading mem 0
  Mem# 0 errs 0: /u01/app/oracle/oradata/prod/redo03.log
Thu Aug 18 07:26:22 2011
Media Recovery Complete (prod)

4)验证:

07:28:10 SQL> select count(*) from scott.test;                                                                                          

  COUNT(*)
----------
        32


案例2:
--------部分数据文件丢失,恢复数据文件(mount 或 open)
 
1)测试环境
07:30:40 SQL> create table test (id int) tablespace test;                                                                               

Table created.

07:30:52 SQL> conn /as sysdba                                                                                                           
Connected.
07:30:57 SQL>
07:30:57 SQL> insert into scott.test values (1);                                                                                        

1 row created.

07:31:02 SQL> commit;                                                                                                                   

Commit complete.

07:31:04 SQL> alter system archive log current;                                                                                         

System altered.

07:31:10 SQL> insert into scott.test values (2);                                                                                        

1 row created.

07:31:13 SQL> commit;                                                                                                                   

Commit complete.

07:31:15 SQL> alter system archive log current;                                                                                         

System altered.

07:31:16 SQL> insert into scott.test values (3);                                                                                        

1 row created.

07:31:19 SQL> commit;                                                                                                                   

Commit complete.

07:31:20 SQL> alter system archive log current;                                                                                         

System altered.

07:31:24 SQL> insert into scott.test values (4);                                                                                        

1 row created.

07:31:26 SQL> insert into scott.test values (5);                                                                                        

1 row created.

07:31:28 SQL> commit;                                                                                                                   

Commit complete.

07:31:30 SQL> select * from scott.test;                                                                                                 

        ID
----------
         1
         2
         3
         4
         5

07:31:34 SQL>

07:31:34 SQL> shutdown abort                                                                                                            
ORACLE instance shut down.
07:33:23 SQL> exit                                                                                                                      
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
[oracle@work ~]$ rm /u01/app/oracle/oradata/prod/test0*.dbf

2)启动database
07:34:07 SQL> startup                                                                                                                   
ORACLE instance started.

Total System Global Area  314572800 bytes
Fixed Size                  1219184 bytes
Variable Size              79693200 bytes
Database Buffers          230686720 bytes
Redo Buffers                2973696 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/u01/app/oracle/oradata/prod/test01.dbf'


07:34:15 SQL> select file#,error from v$recover_file;                                                                                   

     FILE# ERROR
---------- -----------------------------------------------------------------
         6 FILE NOT FOUND
         8 FILE NOT FOUND
        
  3)利用rman 恢复datafile
  RMAN> run {                                                                                                                             
2> startup force mount;                                                                                                                 
3> sql 'alter database datafile 6,8 offline';                                                                                           
4> alter database open;                                                                                                                 
5> restore datafile 6,8;                                                                                                                
6> recover datafile 6,8;                                                                                                                
7> sql 'alter database datafile 6,8 online';                                                                                            
8> }                                                                                                                                    

Oracle instance started
database mounted

Total System Global Area     314572800 bytes

Fixed Size                     1219184 bytes
Variable Size                 79693200 bytes
Database Buffers             230686720 bytes
Redo Buffers                   2973696 bytes

using target database control file instead of recovery catalog
sql statement: alter database datafile 6,8 offline

database opened

Starting restore at 18-AUG-11
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=146 devtype=DISK

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00006 to /u01/app/oracle/oradata/prod/test01.dbf
restoring datafile 00008 to /u01/app/oracle/oradata/prod/test02.dbf
channel ORA_DISK_1: reading from backup piece /disk1/rman/prod/PROD_31.bak
channel ORA_DISK_1: restored backup piece 1
piece handle=/disk1/rman/prod/PROD_31.bak tag=TAG20110818T071905
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
Finished restore at 18-AUG-11

Starting recover at 18-AUG-11
using channel ORA_DISK_1

starting media recovery

archive log thread 1 sequence 8 is already on disk as file /disk1/arch/prod/arch_8_1_759396736.log
archive log thread 1 sequence 9 is already on disk as file /disk1/arch/prod/arch_9_1_759396736.log
archive log thread 1 sequence 10 is already on disk as file /disk1/arch/prod/arch_10_1_759396736.log
archive log thread 1 sequence 11 is already on disk as file /disk1/arch/prod/arch_11_1_759396736.log
archive log thread 1 sequence 12 is already on disk as file /disk1/arch/prod/arch_12_1_759396736.log
archive log thread 1 sequence 13 is already on disk as file /disk1/arch/prod/arch_13_1_759396736.log
archive log filename=/disk1/arch/prod/arch_8_1_759396736.log thread=1 sequence=8
archive log filename=/disk1/arch/prod/arch_9_1_759396736.log thread=1 sequence=9
archive log filename=/disk1/arch/prod/arch_10_1_759396736.log thread=1 sequence=10
archive log filename=/disk1/arch/prod/arch_11_1_759396736.log thread=1 sequence=11
media recovery complete, elapsed time: 00:00:03
Finished recover at 18-AUG-11

sql statement: alter database datafile 6,8 online

RMAN>

4)验证:
[oracle@work ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Aug 18 07:37:17 2011

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


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

07:37:18 SQL>
07:37:18 SQL> select * from scott.test;                                                                                                 

        ID
----------
         1
         2
         3
         4
         5

07:37:23 SQL>

案例3:
----------表空间数据文件全部丢失,恢复数据文件(open)
1)测试环境
07:37:23 SQL> insert into scott.test values (6);                                                                                        

1 row created.

07:39:18 SQL> insert into scott.test values (7);                                                                                        

1 row created.

07:39:20 SQL> insert into scott.test values (8);                                                                                        

1 row created.

07:39:21 SQL> commit;                                                                                                                   

Commit complete.

07:39:23 SQL> select * from scott.test;                                                                                                 

        ID
----------
         6
         7
         8
         1
         2
         3
         4
         5

8 rows selected.

07:40:06 SQL>
----在open 状态下datafile 被破坏
[oracle@work ~]$ rm /u01/app/oracle/oradata/prod/test0*.dbf
[oracle@work ~]$

07:41:19 SQL> alter system flush buffer_cache;                                                                                          

System altered.

07:41:25 SQL> conn scott/tiger                                                                                                          
Connected.
07:41:28 SQL>
07:41:28 SQL> insert into scott.test values (10);                                                                                       
insert into scott.test values (10)
                  *
ERROR at line 1:
ORA-01116: error in opening database file 8
ORA-01110: data file 8: '/u01/app/oracle/oradata/prod/test02.dbf'
ORA-27041: unable to open file
Linux Error: 2: No such file or directory
Additional information: 3


07:41:31 SQL>

3)利用rman 恢复
RMAN> run {                                                                                                                             
2> sql 'alter database datafile 6,8 offline';                                                                                           
3> restore datafile 6,8;                                                                                                                
4> recover datafile 6,8;                                                                                                                
5> sql 'alter database datafile 6,8 online';                                                                                            
6> }

4)验证
07:41:31 SQL> insert into scott.test values (10);                                                                                       

1 row created.

07:45:58 SQL> commit;                                                                                                                   

Commit complete.

07:46:01 SQL> select * from test;                                                                                                       

        ID
----------
        10
         6
         7
         8
         9
         1
         2
         3
         4
         5

10 rows selected.

07:46:04 SQL>