rman 搭建,備份,恢復基礎練習

chenoracle發表於2014-08-29

一:Rman的搭建
二:Rman的備份
三:Rman的恢復
四:Rman的級別
五:Rman備份的刪除
=========================================================================
                                                                                                      一:Rman的搭建
=========================================================================
========================================
les 09 oracle recover manager  ---rman
========================================
1:物理備份
2:只備份有資料的部分,不備份沒有分配的exents    ----而如果用作業系統命令備份會將整個段(包括段裡所有的區)都備份
3:可以增量備份
4:並行
5:可以檢查塊(如果有壞塊能檢測出來)
6:打包
7:支援磁帶(帶庫)  (如果不用rman,想把資料備份到磁帶上,需要先備份到磁碟上,然後在透過tar命令把磁碟上的資料導到磁帶上)
8:可以不用知道結構和備份資訊(如果用作業系統命令備份,需要知道結構和備份資訊)

為什麼有這麼多優點?

rman備份是block級別的備份(rman是oracle生產的工具,所以oracle公司知道oracle資料庫的block結構)
rman備份是伺服器的備份,用oracle server程式可以知道block的格式


Rman在oracle8.0就已經存在;

                           Rman
                            |
                            |
                            |
目標DB(裝資料)------------------------------&gt 恢復目錄DB(裝目標DB的結構,裝目標DB的備份資訊)
app_data                 |                   :app_data在/u01/app/oracle/oradata/db02
                             |                    
                             |
                             |
                   磁碟(磁帶)(裝備份資訊)


不建議將目標DB和恢復目錄DB放在同一個資料庫裡,同一個機器裡;
可以將放在另一個庫裡
可以放在目標資料庫的控制檔案裡(省錢),目標資料庫只要能mount就能檢視恢復目錄資訊;                     
資料在控制檔案中儲存的時間由引數 control_file_record_keep_time
SQL> show parameter control_file_record_keep_time
NAME     TYPE VALUE
------------------------------------ ----------- ----------
control_file_record_keep_time     integer 7       ----------------只能儲存7天;

一週做一次全備
每天備一次增量備份

如果某一天資料庫損壞了,需要用一週前的全備和一週以後每一天的增量備份;

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

11g Enterprise Edition  ------企業版oracle預設有Rman

連線:
Rman 連線目標資料庫時資料庫必須是啟動的,因為是用oracle server程式連線的;控制檔案必須是開啟的

rman target sys/oracle nocatalog       -------------目標資料庫的控制檔案作為恢復目錄資料庫;
RMAN>list backup;
RMAN>report schema;   --------結構資訊
filesperset 5 ---每5個檔案打成一個包
run(要成功一起成功,要失敗都失敗,相當與一個事務)


恢復目錄DB:結構資訊
          備份資訊

管道:手工分配(8.0,8i)  allocate channel 1 type disk[會啟動一個server程式,配多個管道會多個程式共同完成,並行]
     自動分配(9i以後)   configure channel device type disk format '/db01/backup/%u';

恢復目錄DB位置:1 目標資料庫的控制檔案裡
             2 另一個資料庫

=======================
1 目標資料庫的控制檔案裡
=======================
RMAN> show all
2> ;
RMAN configuration parameters for database with db_unique_name DB02 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 '/u01/app/oracle/product/11.2.0/dbs/snapcf_db02.f'; # default

RMAN> CONFIGURE RETENTION POLICY TO REDUNDANCY 2;    

new RMAN configuration parameters:
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;
new RMAN configuration parameters are successfully stored

RMAN> show all;

RMAN configuration parameters for database with db_unique_name DB02 are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;                               ------------冗餘的改為兩份
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 '/u01/app/oracle/product/11.2.0/dbs/snapcf_db02.f'; # default

==================================================
les 17 Recovery Catalog Creation and Maintenance
==================================================
=========================
2 恢復目錄資料庫為另一個資料庫
=========================
恢復目錄資料庫儲存的檔案

1 備份資訊:資料檔案和歸檔重做日誌的備份
2 結構   :目標資料庫的結構資訊
3 常用的指令碼

一個恢復目錄資料庫可以管多個資料庫
目標資料庫和恢復目錄資料庫可以互相做


                           Rman
                            |
       DB1   ------         |
                  |         |
目標DB(裝資料)------------------------------&gt 恢復目錄DB(裝目標DB的結構,裝目標DB的備份資訊)
app_data          |      |                   :app_data在/u01/app/oracle/oradata/db02
        DB3  ------      |                    
                         |
                         |
                   磁碟(磁帶)(裝備份資訊)

1:建立資料庫 orcl(恢復目錄資料庫)
2:建立表空間Rmantbs
3:建立使用者Rman
4:對使用者授權
5:用Rman連線  RMAN>
6:建立表(儲存目標資料庫的資訊)
7:註冊
8:維護

2:Create tablespace rmantbs datafile '/u01/oracle/oradata/orcl/rmantbs01.dbf' size 200M;
3:create user rman identified by rman 
  default tablespace rmantbs 
  temporary tablespace temp;
4:grant connect,resource to rman;
grant recovery_catalog_owner to rman;
5:lsnrctl status
sqlplus
scott/tiger@orcl
6:
export ORACLE_HOME=db02

rman target sys/oracle catalog rman/rman@orcl
RMAN>create catalog tablespace "RMANTBS";
=====
orcl
=====
SQL> select name from v$tablespace;
NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
USERS
TEMP
EXAMPLE
LX_CHEN
RMANTBS         ------------建立成功
8 rows selected.

7:
RMAN>register database;        --------將目標資料庫的資訊註冊到catalog資料庫裡
catalog 資料庫
select * from rc_database;
 
=============
具體步驟
=============
目標資料庫:db02
catalog資料庫:orcl


如何啟動第二個資料庫?
[oracle@chen oradata]$ cd $ORACLE_HOME/dbs


[oracle@chen dbs]$ vim initorcl.ora
#*.memory_target=771751936
*.memory_target=371751936


[oracle@chen dbs]$ export ORACLE_SID=orcl


[oracle@chen dbs]$ rlwrap sqlplus


SQL> select name from v$database;


NAME
---------
ORCL


[oracle@chen ~]$ lsnrctl


LSNRCTL> status
Services Summary...
Service "db02" has 1 instance(s).
  Instance "db02", status READY, has 1 handler(s) for this service...
Service "db02XDB" has 1 instance(s).
  Instance "db02", status READY, has 1 handler(s) for this service...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully








  =====
1:orcl
  =====
2:
SQL> create tablespace rmantbs datafile '/u01/app/oracle/oradata/orcl/rmantbs01.dbf' size 100M;


Tablespace created.


3:
SQL> create user rman identified by rman 
  2  default tablespace rmantbs
  3  temporary tablespace temp;


User created.


4:
SQL> grant connect,resource to rman;


Grant succeeded.


SQL> grant RECOVERY_CATALOG_OWNER to rman;


Grant succeeded.




5:[oracle@chen ~]$ rman target sys/oracle@db02 catalog rman/rman@orcl


Recovery Manager: Release 11.2.0.3.0 - Production on Tue Jun 24 08:49:14 2014


Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


connected to target database: DB02 (DBID=1598433958)
connected to recovery catalog database


RMAN> 




6:
RMAN> create catalog tablespace "RMANTBS";


recovery catalog created




7:
RMAN> register database;


database registered in recovery catalog
starting full resync of recovery catalog
full resync complete


=====
orcl
=====
SQL> conn rman/rman


SQL> select * from tab;
.....
142 rows selected.




=====
db02
=====
create script Level0Backup {
backup
incremental level 0
format ‘/u01/db01/backup/%d_%s_%p’
filesperset 5
(database include current controlfile);
sql ‘alter system archive log current’;


=====
orcl 
=====
conn rman/man
SQL> select * from rc_stored_script;














=========================================================================================================================
                                                                  二:less 11 RMAN Backups
=========================================================================================================================
1:Image copy
2:Backup set   ---可以直接寫入磁碟或磁帶
               ---資料檔案可以增量備份,也可以全備
               ---不備份沒有使用的塊


Backup Piece:
A backup piece is a file in a backup set.
A backup piece can contain blocks from more than one datafile.


Backup Piece Size
Backup piece size can be limited as follows:


==============
1:Image copy
==============
備份:1 備份資料檔案
     2 備份歸檔日誌
     3 備份控制檔案



======
db02
======
SQL> set linesize 100
SQL> col file_name for a50
SQL> select file_name,file_id from dba_data_files;


FILE_NAME      FILE_ID
-------------------------------------------------- ----------
/u01/app/oracle/oradata/db02/users01.dbf    4
/u01/app/oracle/oradata/db02/undotbs01.dbf    3
/u01/app/oracle/oradata/db02/sysaux01.dbf    2
/u01/app/oracle/oradata/db02/system01.dbf    1
/u01/app/oracle/oradata/db02/lx_db02.dbf    5










一:備份資料檔案
[oracle@chen ~]$ rman target sys/oracle@db02 catalog rman/rman@orcl


RMAN> run{
2> allocate channel d1 type disk;              -------在磁碟上建立一個管道   
3> copy datafile 5 to '/u01/app/oracle/oradata/bak/lx_dd01.dbf';
4> }


RMAN> list copy;


specification does not match any control file copy in the repository
specification does not match any archived log in the repository
List of Datafile Copies
=======================


Key     File S Completion Time Ckp SCN    Ckp Time       
------- ---- - --------------- ---------- ---------------
2       5    A 23-JUN-14       1020184    23-JUN-14      
        Name: /u01/app/oracle/oradata/bak/lx_dd01.dbf
        Tag: TAG20140623T174948


[oracle@chen bak]$ ls
db_32_1.bus01  db_35_1.bus01  db_37_1.bus01
db_33_1.bus01  db_36_1.bus01  lx_dd01.dbf


RMAN>report schema;


二:備份歸檔檔案


run{
   allocate channel d1 type disk;    -------在磁碟上建立一個管道
   copy archivelog '/u01/oracle/oradata/db02/archive2/app/db02_111.arc' to '/u01/....../bak/...arc';


RMAN> run
2> {
3> allocate channel d1 type disk;
4> copy archivelog '/home/oracle/db02_archive1/db02_1851001130_11.arc' to '/u01/app/oracle/oradata/db_archive01.arc';
5> }




三:備份控制檔案


RMAN> run{
2> allocate channel d1 type disk;
3> copy current controlfile to '/u01/app/oracle/oradata/bak/control01.ctl';
4> }


RMAN> list copy of controlfile;


List of Control File Copies
===========================


Key     S Completion Time Ckp SCN    Ckp Time       
------- - --------------- ---------- ---------------
3       A 23-JUN-14       1020865    23-JUN-14      
        Name: /u01/app/oracle/oradata/bak/control01.ctl
        Tag: TAG20140623T180435


[oracle@chen bak]$ ls
control01.ctl  db_33_1.bus01  db_36_1.bus01  lx_dd01.dbf
db_32_1.bus01  db_35_1.bus01  db_37_1.bus01




三個複製一起執行


run{
    allocate channel d1 type disk;
    allocate channel d2 type disk;
    allocate channel d3 type disk;


copy datafile 4 to /u01/oracle/oradata/bak/app_data01.dbf',
     archivelog '/u01/oracle/oradata/db02/archive2/db02_111.arc' to '/u01/....../bak/...arc',
     current controlfile to '/u01/oracle/oradata/bak/control.ctl';


RMAN> run{
2> allocate channel d1 type disk;
3> allocate channel d2 type disk;
4> copy datafile 5 to '/u01/app/oracle/oradata/bak/lx_02.dbf';
5> copy current controlfile to '/u01/app/oracle/oradata/bak/control2.ctl';
6> }


RMAN> list copy of controlfile;


List of Control File Copies
===========================


Key     S Completion Time Ckp SCN    Ckp Time       
------- - --------------- ---------- ---------------
5       A 23-JUN-14       1021404    23-JUN-14      
        Name: /u01/app/oracle/oradata/bak/control2.ctl
        Tag: TAG20140623T181334


3       A 23-JUN-14       1020865    23-JUN-14      
        Name: /u01/app/oracle/oradata/bak/control01.ctl
        Tag: TAG20140623T180435




資料檔案少時(20個檔案一下)用image copy;












====================
二:backup set 備份集
====================
                  可以將檔案打包,多個檔案打包成一個檔案,易於管理,作業系統備份辦不到;
                  只備份有資料的地方,不備份沒有分配的extents;   ---(因為他是基於block的備份)
                  可以將資料備份到磁帶上;
                  資源限制;(I/O限制)---備份時可以調節所佔的I/O


備份集是一個邏輯的概念;
備份出來的資料稱為備份片:piece








datafile級別的     :


tablespace級別的   :


database級別的     :


run{
    allocate channel d1 type disk;
    backup datafile 4 format '/u01/app/oracle/oradata/bak/db02_%s_%p.chen';
    backup datafile 5 format '/u01/app/oracle/oradata/bak/db02_%s_%p.chen';
    backup datafile 6 format '/u01/app/oracle/oradata/bak/db02_%s_%p.chen';


上一條命令可以簡寫:


run{
    allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db02_%s_%p.chen';
    backup datafile 4;
    backup datafile 5;
    backup datafile 6;






1:備份資料檔案   --------只備份有資料的地方,速度快


RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db02_%s_%p.chen';
3> backup datafile 5;
4> }


RMAN> list backup;




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




BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
32      Full    1.07M      DISK        00:00:01     23-JUN-14      
        BP Key: 32   Status: AVAILABLE  Compressed: NO  Tag: TAG20140623T184232
        Piece Name: /u01/app/oracle/oradata/bak/db02_43_1.chen
  List of Datafiles in backup set 32
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  5       Full 1022221    23-JUN-14 /u01/app/oracle/oradata/db02/lx_db02.dbf








備份表空間
SQL> select segment_name,tablespace_name from user_segments;


SEGMENT_NAME   TABLESPACE_NAME
-------------------------------------------------- ------------------------------
DEPT   USERS
EMP   USERS
SALGRADE   USERS
LX_TAB   LX_DB02
T1   LX_DB02
T2   LX_DB02
T3   LX_DB02
T4   LX_DB02
PK_DEPT   USERS
PK_EMP   USERS


10 rows selected.


run{
allocate channel d1 type disk format '/u01/oracle/oradata/bak/tbs_%s_%p.bus';
backup tablespace app_data filesperset 3;   -------每三個檔案形成一個檔案集




RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/ccc_%s_%p.cccs';
3> backup tablespace lx_db02 filesperset 3;
4> }










RMAN> list backup of datafile 4;                       ------檢視datafile4的備份資訊,相當與過濾條件where;
RMAN> list backup of tablespace app_data;
















[oracle@chen admin]$ rlwrap rman target sys/oracle@orcl nocatalog                ---------用目標資料庫的控制檔案做為目標檔案的回覆目錄資料庫


Recovery Manager: Release 11.2.0.3.0 - Production on Mon Jun 23 12:15:49 2014


Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


connected to target database: ORCL (DBID=1378775808)
using target database control file instead of recovery catalog


RMAN> 








====================
備份資料庫   ----全備份
====================


database 約等於 datafile


---------------備份資料檔案包括當前的控制檔案
RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.aaa';
3> backup database include current controlfile filesperset 3;
4> }


[oracle@chen bak]$ ls
ccc_44_1.cccs  db02_43_1.chen  db_45_1.aaa  db_46_1.aaa  db_47_1.aaa










RMAN> list backup of controlfile;


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




BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
36      Full    9.33M      DISK        00:00:01     23-JUN-14      
        BP Key: 36   Status: AVAILABLE  Compressed: NO  Tag: TAG20140623T185719
        Piece Name: /u01/app/oracle/oradata/bak/db_47_1.aaa
  Control File Included: Ckp SCN: 1022770      Ckp time: 23-JUN-14


RMAN> list backup;


rman能夠備份的有:資料檔案,控制檔案,歸檔檔案


























============
備份控制檔案:
============
RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.qqq';
3> backup current controlfile;
4> }




自動備份控制檔案:


RMAN> show all;


CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default    -----預設是關閉的


RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;


new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored




RMAN> show all;


CONFIGURE CONTROLFILE AUTOBACKUP ON;








============
歸檔檔案的備份
============
RMAN> list copy;    ---檢視歸檔模式       只有備份以後的歸檔對我們才有用,備份之前的歸檔對我們沒有用,需要刪除;


specification does not match any datafile copy in the repository
specification does not match any control file copy in the repository
List of Archived Log Copies for database with db_unique_name DB02
=====================================================================


Key     Thrd Seq     S Low Time 
------- ---- ------- - ---------
2       1    2       A 23-JUN-14
        Name: /home/oracle/db02_archive2/db02_1851001130_2.arc


1       1    2       A 23-JUN-14
        Name: /home/oracle/db02_archive1/db02_1851001130_2.arc




RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.ooooo';
3> backup archivelog all;
4> }


RMAN> list backup;












=========================================================================================================================
                                                                三:Rman的恢復
=========================================================================================================================
Rman 恢復
1:完全恢復---datafile
            tablespace
            database
            更改恢復目錄


2:不完全恢復--基於時間點的恢復
           --基於取消的恢復


===============================
1:完全恢復
===============================




恢復:noarchive 破壞一個檔案,或者一個表空間,或者整個資料庫 恢復時都需要恢復整個資料庫;
     archive   完全恢復 :破壞一個檔案(system)
                            一個檔案(非 system)
                            表空間
                            資料庫database     ---恢復database,tablespace,datafile






run{
   allocate channel d1 type disk;
   restore datafile 4;                 --------相當與解壓檔案集,資料到T1時刻,其他檔案在T2時刻,需要由歸檔或日誌補齊
   recover datafile 4;
                                           


案例場景: datafile檔案級別的恢復
        1 備份 ---&gt1200
        2 update---&gt1300
        3 破壞
        4 恢復


=====
db02
=====
conn scott/tiger
update emp set sal=1300 where empno=7369;
commit;


conn sys/oracle as sysdba


shutdown immediate;


oracle@chen db02]$ rm -rf users01.dbf


startup 


select * from v$recover_file;


rman target sys/oracle catalog rman/rman@orcl     -------catalog資料庫必須open,因為寫入表空間了


RMAN>run{
   allocate channel d1 type disk;
   restore datafile 4;                 --------相當與解壓檔案集,資料到T1時刻,其他檔案在T2時刻,需要由歸檔或日誌補齊
   recover datafile 4;
}


RMAN>alter database open;


sqlplus


scott/tiger


select empno,ename,sal from emp;






========
具體步驟
========
1:備份
SQL> select name from v$database;


NAME
---------
DB02


SQL> select * from t1 where empno=7369;


     EMPNO ENAME     SAL
---------- ---------- ----------
      7369 SMITH     800


SQL> update t1 set sal=1000 where empno=7369;


1 row updated.


SQL> commit;


SQL> select segment_name,tablespace_name from user_segments;


SEGMENT_NAME   TABLESPACE_NAME
-------------------------------------------------- ------------------------------
DEPT   USERS
EMP   USERS
LX_TAB   LX_DB02
PK_DEPT   USERS
PK_EMP   USERS
SALGRADE   USERS
T1   LX_DB02


7 rows selected.




[oracle@chen ~]$ rlwrap rman target sys/oracle catalog rman/rman@orcl


Recovery Manager: Release 11.2.0.3.0 - Production on Tue Jun 24 10:15:01 2014


Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.


connected to target database: DB02 (DBID=1598433958)
connected to recovery catalog database


RMAN> run{
2> allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/kkk_%s_%p.chen';
3> backup database filesperset 3;
4> }


2:
SQL> conn scott/tiger
Connected.
SQL> update t1 set sal=1300 where empno=7369;


1 row updated.


SQL> commit;


Commit complete.


3:破壞
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.


[oracle@chen db02]$ mv lx_db02.dbf lx_db02.dbf.bak


4:恢復
SQL> startup
ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
ORACLE instance started.


Total System Global Area  371617792 bytes
Fixed Size    2228504 bytes
Variable Size  251662056 bytes
Database Buffers  113246208 bytes
Redo Buffers    4481024 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/db02/lx_db02.dbf'


[oracle@chen ~]$ rlwrap rman target sys/oracle catalog rman/rman@orcl


RMAN> run{
2> allocate channel d1 type disk;
3> restore datafile 5;
4> recover datafile 5;
5> }


RMAN> alter database open;


database opened


[oracle@chen ~]$ rlwrap sqlplus


SQL> select name from v$database;


NAME
---------
DB02


SQL> conn scott/tiger
Connected.


SQL> select * from t1 where empno=7369;


     EMPNO ENAME     SAL
---------- ---------- ----------
      7369 SMITH    1300






















二:表空間級別的恢復


run{
    allocate channel d1 type disk;
    restore tablespace app_data;                  ----資料恢復到t1
    recover tablespace app_data;                  ----透過歸檔將資料由t1推到t2
}




案例場景: datafile檔案級別的恢復
        1 備份 ---&gt1200
        2 update---&gt1300
        3 破壞
        4 恢復




alter tablespace app_data offline;


rm -rf app_data01.dbf


alter tablespace app_data online; --失敗


RMAN>run{
    allocate channel d1 type disk;
    restore tablespace app_data;                  ----資料恢復到t1
    recover tablespace app_data;                  ----透過歸檔將資料由t1推到t2
}
  


alter tablespace app_data online;


select empno,ename,sal from scott.emp;




==========
具體步驟
==========
1:備份
之前已經有一個全備了


2:1300


3:破壞
SQL> select name from v$tablespace;


NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
USERS
TEMP
LX_DB02


6 rows selected.


SQL> alter tablespace lx_db02 offline;


Tablespace altered.


[oracle@chen db02]$ mv lx_db02.dbf lx_db02.dbf.bak


SQL> alter tablespace lx_db02 online;
alter tablespace lx_db02 online
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/db02/lx_db02.dbf'


4:恢復
[oracle@chen ~]$ rlwrap rman target sys/oracle catalog rman/rman@orcl


RMAN> run{
2> allocate channel d1 type disk;
3> restore tablespace lx_db02;
4> recover tablespace lx_db02;
5> }


SQL> alter tablespace lx_db02 online;


Tablespace altered.


SQL> select * from scott.t1 where empno=7369;


     EMPNO ENAME     SAL
---------- ---------- ----------
      7369 SMITH    1300






==================
更改恢復目錄
==================




如果目標資料庫的恢復目錄所在的磁碟損壞,資料不能恢復到指定目錄,如何解決?
將指定目錄轉到其他的目錄下;
run{
allocate channel d1 type disk;
set newname for datafile 4 to '/u01/oracle/oradata/db02/tbs/app_data01.dbf'; 
restore tablespace app_data;
switch datafile 4;                                ----------------------------------如果有多個檔案可以寫成switch datafile all;
recover tablespace app_data;




alter tablespace app_data offline;


rm -rf app_data01.dbf


alter tablesapce app_data online;  ---失敗


rman>run{
allocate channel d1 type disk;
set newname for datafile 4 to '/u01/oracle/oradata/db02/tbs/app_data01.dbf';               ---目錄tbs應該提前建立
restore tablespace app_data;
switch datafile 4;
recover tablespace app_data;
}


sql>altert tablespace app_data online;


select * from emp where empno=7369;


=========
具體步驟
=========
[oracle@chen oradata]$ mkdir tbs


[oracle@chen oradata]$ ls
bak  db02  orcl  tbs


1:破壞
SQL> alter tablespace lx_db02 offline;


Tablespace altered.


[oracle@chen db02]$ mv lx_db02.dbf lx_db02.dbf.bak


SQL> alter tablespace lx_db02 online;
alter tablespace lx_db02 online
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: '/u01/app/oracle/oradata/db02/lx_db02.dbf'


2:恢復
[oracle@chen ~]$ rlwrap rman target sys/oracle catalog rman/rman@orcl


RMAN> run{
2> allocate channel d1 type disk;
3> set newname for datafile 5 to '/u01/app/oracle/oradata/tbs/lx_dbccc.dbf';
4> restore tablespace lx_db02;
5> switch datafile 5;                    --------更改控制檔案的結構
6> recover tablespace lx_db02;
7> }




SQL> alter tablespace lx_db02 online;


Tablespace altered.


SQL> select * from scott.t1 where empno=7369;


     EMPNO ENAME     SAL
---------- ---------- ----------
      7369 SMITH    1300














==========
全部恢復
==========


所以的資料檔案都壞了?
全部恢復


run{
   allocate channel d1 type disk;
   restore database;                ---------將打包的資料分發出來
   recover database;                ---------利用歸檔改資料
}




shutdown immediate;


rm -rf *.dbf


startup


exit


rman target sys/oracle catalog rman/rman@orcl


RMAN>run{
   allocate channel d1 type disk;
   restore database;                ---------將打包的資料分發出來
   recover database;                ---------利用歸檔改資料
}




alter database open;


select empno,ename,sal from scott.emp where empno=7369;






===============
Rman不完全恢復
===============




Rman
不完全恢復:基於時間點的恢復   (使用者誤操作)       --------預防:1對使用者做培訓 2對使用者許可權做限制
          基於取消的恢復    (歸檔或當前日誌被破壞)
          基於備份的控制檔案 (相當與基於時間點的恢復)


Incomplete Recovery of a Database Using RMAN


1 Mount the database.
2 Allocate multiple channels for parallelization.
3 Restore all datafiles.
4 Recover the database by using UNTIL TIME, UNTIL SEQUENCE, or UNTIL SCN.
5 Open the database by using RESETLOGS.
6 Perform a whole database backup.




=============
les 15
=============
一個管道就是一個server程式,多個管道並行工作速度快


Incomplete Recovery of a Database Using RMAN


1. Mount the database.
2. Allocate multiple channels for parallelization.
3. Restore all datafiles.
4. Recover the database by using UNTIL TIME, UNTIL SEQUENCE, or UNTIL SCN.
5. Open the database by using RESETLOGS.
6. Perform a whole database backup.






==============
1:基於時間點
==============
1:
SQL> select sysdate from dual;


SYSDATE
-------------------
2014-06-24 11:55:03


2:誤刪除
SQL> conn scott/tiger
Connected.


Session altered.


SQL> drop table t1 purge;


Table dropped.


SQL> select * from t1;
select * from t1
              *
ERROR at line 1:
ORA-00942: table or view does not exist


3:恢復
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.


4:
SQL> startup mount


5:
[oracle@chen ~]$ rlwrap rman target sys/oracle catalog rman/rman@orcl


connected to target database: DB02 (DBID=1598433958, not open)
connected to recovery catalog database


RMAN> run{
2> allocate channel c1 type disk;
3> allocate channel c2 type disk;
4> set until time = '2014-06-24 11:55:03';
5> restore database;
6> recover database;
7> }


6:
SQL> alter database open resetlogs;


Database altered.


SQL> select * from scott.t1;


=======
老師案例
=======
2014-06-24:12:04:06    7369----1300
update sghr.emp set sal=1400 where empno=7369;
commit;
drop table sghr.emp purge;




shutdown immediate


備份  -----用OS命令做一個全備(保留現場)


startup mount  -----用rman必須mount


RMAN>run{
allocate channel d1 type disk;
set until time = '2014-06-24:12:04:06';
restore database;
recover database;
alter database open resetlogs;
}


RMAN>reset database;


sqlplus
select * from all_users;
select empno,ename,sal from sghr.emp;

2:基於取消
RMAN> list copy;

specification does not match any datafile copy in the repository
specification does not match any control file copy in the repository
List of Archived Log Copies for database with db_unique_name DB02
=====================================================================

Key     Thrd Seq     S Low Time 
------- ---- ------- - ---------
163     1    14      A 23-JUN-14
        Name: /home/oracle/db02_archive1/db02_1851001130_14.arc

164     1    14      A 23-JUN-14
        Name: /home/oracle/db02_archive2/db02_1851001130_14.arc

584     1    15      A 24-JUN-14
        Name: /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/o1_mf_1_15_9tlfr2rg_.arc

608     1    16      A 24-JUN-14
        Name: /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/o1_mf_1_16_9tlk3cvg_.arc

SQL> select * from emp001 where empno=7369;
     EMPNO ENAME     SAL
---------- ---------- ----------
      7369 SMITH     800

SQL> alter system switch logfile;
System altered.

RMAN> list copy;

specification does not match any datafile copy in the repository
specification does not match any control file copy in the repository
List of Archived Log Copies for database with db_unique_name DB02
=====================================================================

Key     Thrd Seq     S Low Time 
------- ---- ------- - ---------
163     1    14      A 23-JUN-14
        Name: /home/oracle/db02_archive1/db02_1851001130_14.arc


164     1    14      A 23-JUN-14
        Name: /home/oracle/db02_archive2/db02_1851001130_14.arc


584     1    15      A 24-JUN-14
        Name: /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/o1_mf_1_15_9tlfr2rg_.arc


608     1    16      A 24-JUN-14
        Name: /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/o1_mf_1_16_9tlk3cvg_.arc


720     1    1       A 24-JUN-14
        Name: /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/o1_mf_1_1_9tlly347_.arc

SQL> conn scott/tiger
Connected.
Session altered.

SQL> drop table emp001 purge;
Table dropped.


SQL> select * from emp001;
select * from emp001
              *
ERROR at line 1:
ORA-00942: table or view does not exist

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

[oracle@chen oradata]$ cd /u01/app/oracle/fast_recovery_area/DB02/archivelog/2014_06_24/
[oracle@chen 2014_06_24]$ rm -rf o1_mf_1_1_9tlly347_.arc

SQL> startup mount
ORACLE instance started.

Total System Global Area  371617792 bytes
Fixed Size    2228504 bytes
Variable Size  251662056 bytes
Database Buffers  113246208 bytes
Redo Buffers    4481024 bytes
Database mounted.

=========================================================================================================================
                                                               四:Rman的級別
=========================================================================================================================
備份級別:0,1,2,3,4
Making Incremental Backups:同一級別和級別比他低的增量備份;
Cumulative Incremental Backup(累計增量備份):同一級別和級別比他低的累計備份;

完全備份:所有的block都備份出來
增量備份:備份上一次改變的block

     t0     t1       t2     t3     t4     t5     t6      t7
-----|------|--------|------|------|------|------|--------|-----&gt
     0      2        2      2      1      2      0        0
    全備  增量備份 

     t0     t1       t2     t3     t4     t5     t6      t7
-----|------|--------|------|------|------|------|--------|-----&gt      -----佔用儲存空間少,備份快,但恢復慢
     0      1        1      1      1      1      1        0
    全備  增量備份 
         每天的備份


cumulative累積   同一級別和比他級別低的累計備份

     t0     t1       t2     t3     t4     t5     t6      t7
-----|------|--------|------|------|------|------|--------|-----&gt      
     0      1c       1c     1c     1c     1c     1c       0

例如:
一個月進行一次級別0的備份
每週進行一次級別1的備份
每天進行一次級別2的備份
每小時進行一次級別3的備份
每分鐘進行一次級別4的備份

差異型的增量備份
累積型的增量備份

=======
級別零:
=======
run{
   allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.bus01';
   backup incremental level 0 database filesperset 3;
}

[oracle@chen bak]$ ls
db_18_1.bus  db_24_1.chen  db_29_1.bus01  db_s%_1.chen
db_19_1.bus  db_25_1.chen  db_30_1.bus01  lx_chen00.dbf
db_20_1.bus  db_26_1.chen  db_31_1.bus01

SQL> conn scott/tiger
Connected.
SQL> select * from tab;
TNAME       TABTYPE CLUSTERID
------------------------------ ------- ----------
BONUS       TABLE
DEPT       TABLE
EMP       TABLE
LX_TAB       TABLE
SALGRADE       TABLE

SQL> update lx_tab set sal=1000 where empno=7369;   ---改變一個數,看是否能增量備份
1 row updated.

run{
   allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.bus01';
   backup incremental level 0 database filesperset 3;
}

[oracle@chen bak]$ ll
總用量 3082264
-rw-r-----. 1 oracle oinstall 626016256 6月  23 15:34 db_29_1.bus01
-rw-r-----. 1 oracle oinstall 394330112 6月  23 15:34 db_30_1.bus01
-rw-r-----. 1 oracle oinstall   9797632 6月  23 15:35 db_31_1.bus01
-rw-r-----. 1 oracle oinstall 626016256 6月  23 15:37 db_32_1.bus01
-rw-r-----. 1 oracle oinstall 394330112 6月  23 15:37 db_33_1.bus01
-rw-r-----. 1 oracle oinstall   9797632 6月  23 15:37 db_34_1.bus01
-rw-r-----. 1 oracle oinstall     10240 6月  23 14:45 db_s%_1.chen
-rw-r-----. 1 oracle oinstall   5251072 6月  23 15:09 lx_chen00.dbf
說明級別0的並不是增量備份而是全備份;

=======
級別一:
=======
SQL> conn scott/tiger
Connected.
SQL> update lx_tab set sal=1200 where empno=7369;
1 row updated.

SQL> commit;
Commit complete.


run{
   allocate channel d1 type disk format '/u01/app/oracle/oradata/bak/db_%s_%p.bus01';
   backup incremental level 1 database filesperset 3;
}


[oracle@chen bak]$ ll
總用量 3092328
-rw-r-----. 1 oracle oinstall 626016256 6月  23 15:34 db_29_1.bus01
-rw-r-----. 1 oracle oinstall 394330112 6月  23 15:34 db_30_1.bus01
-rw-r-----. 1 oracle oinstall   9797632 6月  23 15:35 db_31_1.bus01

-rw-r-----. 1 oracle oinstall 626016256 6月  23 15:37 db_32_1.bus01
-rw-r-----. 1 oracle oinstall 394330112 6月  23 15:37 db_33_1.bus01
-rw-r-----. 1 oracle oinstall   9797632 6月  23 15:37 db_34_1.bus01

-rw-r-----. 1 oracle oinstall    188416 6月  23 15:47 db_35_1.bus01   ---增量備份
-rw-r-----. 1 oracle oinstall    319488 6月  23 15:47 db_36_1.bus01
-rw-r-----. 1 oracle oinstall   9797632 6月  23 15:47 db_37_1.bus01

=========================================================================================================================
                                                                五:Rman備份的刪除
=========================================================================================================================
刪除   ------------image copy
===========
[oracle@chen oradata]$ ls
bak  db02  db_archive01.arc  orcl

[oracle@chen oradata]$ rm -rf db_archive01.arc 
RMAN> crosscheck copy;
validation failed for archived log
archived log file name=/u01/app/oracle/oradata/db_archive01.arc RECID=21 STAMP=851018388


validation succeeded for archived log
archived log file name=/home/oracle/db02_archive1/db02_1851001130_11.arc RECID=19 STAMP=851018141

RMAN> delete expired copy;
=====================================================================
Key     Thrd Seq     S Low Time 
------- ---- ------- - ---------
21      1    11      X 23-JUN-14
        Name: /u01/app/oracle/oradata/db_archive01.arc

Do you really want to delete the above objects (enter YES or NO)? yes
deleted archived log
archived log file name=/u01/app/oracle/oradata/db_archive01.arc RECID=21 STAMP=851018388
Deleted 1 EXPIRED objects

=========
刪除    ---------backup set
=========
[oracle@chen bak]$ ls
control01.ctl  db_32_1.bus01  db_35_1.bus01  db_37_1.bus01  lx_dd01.dbf
control2.ctl   db_33_1.bus01  db_36_1.bus01  lx_02.dbf


[oracle@chen bak]$ rm -rf *
RMAN> list backup;
RMAN> crosscheck backup;   ---檢查保留策略

using channel ORA_DISK_1
crosschecked backup piece: found to be 'EXPIRED'
backup piece handle=/u01/app/oracle/oradata/bak/db_32_1.bus01 RECID=26 STAMP=851009829
crosschecked backup piece: found to be 'EXPIRED'
backup piece handle=/u01/app/oracle/oradata/bak/db_33_1.bus01 RECID=27 STAMP=851009844
......

RMAN> delete expired backup;
RMAN> list backup;
specification does not match any backup in the repository

刪除陳舊的備份
RMAN> delete obsolete;
刪除過期的(如:用作業系統命令刪除的備份就會變成過期的)
RMAN> delete expired backup;  
檢視壞塊的資訊
SQL> desc v$copy_corruption        映像集
SQL> desc v$backup_corruption      備份集


 歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!

rman 搭建,備份,恢復基礎練習

rman 搭建,備份,恢復基礎練習



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

相關文章