分類  >  資料庫 >

ORACLE10G RMAN 下令

tags:    時間:2013-12-23 21:51:21
ORACLE10G RMAN 命令
RMAN 命令



連接到目標資料庫(不用恢複目錄資料庫)
rman target / nocatalog

顯示rman配置
RMAN> show all;


====================
報告目標資料庫的物理結構
RMAN> report schema;

報告陳舊備份
RMAN> report obsolete;

報告不可恢復的數據文件
RMAN> report unrecoverable;

列出備份信息
RMAN> list backup;

RMAN> list backup of database;

RMAN> list backup of tablespace table_name;

RMAN> list backup of controlfile;

RMAN> list backup of spfile;

RMAN> list backupset id;

========================
校驗備份信息:
RMAN> crosscheck backup;

RMAN> crosscheck backup of database;

RMAN> crosscheck backup of tablespace system;

RMAN> crosscheck backup of controlfile;

RMAN> crosscheck backup of spfile;


========================
刪除備份

Use the ALLOCATE CHANNEL FOR MAINTENANCE command to manually allocate a channel in preparation for issuing a CHANGE, DELETE, or CROSSCHECK command. You can use the RELEASE CHANNEL command to unallocate the channel.

ALLOCATE CHANNEL FOR MAINTENANCE device type (disk,sbt ...)


RMAN> delete obsolete; -- 刪除陳舊備份

RMAN> delete expired backup;

RMAN> delete backupset id;

RMAN> delete backup; -- 刪除所有備份


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


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

1 列出對應物  RMAN>list incarnation;

2 列出備份

2.1概述可用的備份    RMAN>list backup summary;
B 表示 backup
F 表示 FULL
A 表示 archive log
0 1 表示 incremental backup
S 說明備份狀態 (A AVAILABLE   X EXPIRED )

2.2按備份類型列出備份  RMAN>list backup by file;
按照 數據文件備份,歸檔日誌備份,控制文件備份,伺服器參數文件備份 列出

2.3列出詳細備份     RMAN>list backup;

2.4列出過期備份     RMAN>list expired backup;

2.5列出表空間和數據文件備份
list backup of tablespace 和list backup of datafile 輸出和list backup 相似
如:list backup of tablespace user_tbs;
list backup of datafile 3;

2.6列出歸檔日誌備份   RMAN>list archivelog all;      簡要信息
RMAN>list backup of archivelog all; 詳細信息

2.7列出控制文件和伺服器參數文件
RMAN>list backup of controfile;
RMAN>list backup of spfile;

=====================================================
=====================================================
RMAN 的 report命令

1. 報告最近沒有被備份的數據文件
RMAN>report need backup days=3;

2. 報告備份冗餘或恢復窗口
RMAN>report need backup redundancy=2;
RMAN>report need backup recovery window of 2 days;

3. 報告數據文件的不可恢復操作
RMAN>report unrecoverable;

4. 報告資料庫模式
RMAN>report schema;

5. 報告丟棄的備份
如果使用了保存策略,備份會標記為丟棄狀態  RMAN>report obsolete; 刪除丟棄狀態備份RMAN>delete obsolete;

=====================================================
=====================================================
RMAN的crosscheck 命令

1 備份集有兩種狀態A(Available,RMAN認為該項存在於備份介質上)X(Expired,備份存在於控制文件或恢複目錄中,但是並沒有物理存在於備份介質上)

2 crosscheck 的目的是檢查RMAN 的目錄以及物理文件,如果物理文件不存在於介質上,將標記為Expired。如果物理文件存在,將維持Available。如果原先標記為Expired的備份集再次存在於備份介質上(如恢復了損壞的磁碟驅動器后),crosscheck將把狀態重新從Expired標記回Available。

3 crosscheck 輸出分兩部分。第一部分列出確定存在於備份介質上的所有備份集片,第二部分列出不存在於備份介質上的備份集片,並將其標記為Expired。當設置備份保 存策略后,一個備份過期,crosscheck之後標記為丟棄的備份狀態依舊為availabel,要刪除丟棄備份delete obsolete。

4 示例:
crosscheck backup
crosscheck backup of datafile 1;
crosscheck backup of tablespace users;
crosscheck backup of controfile;
crosscheck backup of controlfile;
crosscheck backup tag='SAT_BACKUP';
crosscheck backup completed after 'sysdate - 2'
crosscheck backup completed between 'sysdate - 5' and 'sysdate -2 '
crosscheck backup device type sbt;
crosscheck archivelog all;
crosscheck archivelog like '%ARC00012.001'
crosscheck archivelog from sequence 12;
crosscheck archivelog until sequence 522;

=====================================================
=====================================================
RMAN 的validate 命令

1 validate 命令驗證備份集片是否能夠被還原

2 list backup summary; 得到了備份集得主鍵ID如40,然後validate backupset 40;
=====================================================
=====================================================
RMAN 的備份保存策略以及change ,delete命令

0.帶delete參數的change 命令刪除備份集,從備份介質,並且從控制文件和恢複目錄中刪除。
change backupset 117,118 delete;
change backuppiece 1304 delete;
change archivelog until logseq =544 delete;

1.兩類策略:恢復窗口備份保存策略(recovery windows backup retension policy) 基於時間
備份冗餘備份保存策略(backup redundancy backup retension policy) 基於備份的數量

兩類策略互相排斥

2.即使使用了備份保存策略,備份到期並不刪除,只是在RMAN目錄中標記為丟棄,看到的狀態依舊為available;
要查看標記為丟棄的備份 report obsolete,只有使用delete obsolete才真正物理刪除。

3.configure retension policy to recovery window of 7 days;
configure retension policy to redundancy 3;
顯示結果 show all;

4.查看到期丟棄的備份時,可能需要手工保存一些備份 ,可以使用change 命令帶keep 參數,使用這個命令后,那些被修改的備份將被認為是個long-term backup,不在受保存策略影響也就是說delete obsolete 也不刪除。

5.要使備份時候不受保存策略影響 ,使用帶keep參數的backup 命令
backup database keep forever;
backup database keep 5 days;

6.change 命令功能

可以修改備份為永久保存並將以及該備份的相關的日誌保存下來,保證總能將備份恢復到當前時間點
change backupset 31 keep forever logs;
可以設置備份丟棄的新日期 ,將備份在多保存7 天,7 天後將刪除
change backupset 32 keep until time 'sysdata + 7' logs;

7.change 可以將備份集設置為unavailable
change backupset 33 unavailable;
標記為unavailable狀態的備份集並不參與crosscheck;
=====================================================
=====================================================
恢複目錄的記錄刪除

1. $ORACLE_HOME/rdbms/admin/prgrmanc.sql 腳本定期刪除恢複目錄中具有DELETED狀態的記錄

2. 要刪除舊的對應物記錄incarnation.必須從DBINC 表中刪除這些對應物,使用RC_DATABASE_INCARNATION 視圖來確定要刪除的對應物。記錄要刪除的每個對應物的DBINC_KEY
隨後啟動SQL*Plus,執行delete from dbinc where dbinc_key=2;
=====================================================
=====================================================
手工同步恢複目錄

resync catalog;

Oracle同步恢複目錄的時候,首先創建快照控制文件,然後比較這個文件和恢複目錄,完成後,Oracle 更新恢複目錄,使恢複目錄和控制文件同步
=====================================================
=====================================================
在RMAN 中存儲腳本

1,連接到目標資料庫和恢複目錄
rman target / catalog rman/rman@rman9i

2,創建腳本
RMAN>create script my_bk_script
2>{backup database plus archivelog;}
create script my_bk_script

3,列印腳本
RMAN>print script my_bk_script;
printing stored scipt:my_bk_script
{backup database plus archivelog;}

4,運行這個腳本,備份目標資料庫
RMAN>run {execute scipt my_bk_script;}

5 刪除腳本
RMAN>delete script my_bk_script;
=====================================================
=====================================================
archivelog模式下的完全恢復

step1:set oracle_sid=recover
rman target rman_backup/password
configure controlfile autobackup on;

step2:backup database plus archivelog delete input;

step3:shutdown immediate;

step4:重命名所有的數據文件和控制文件,不重命名聯機重做日誌。

step5: startup nomount;
set DBID=****
restore controlfile from autobackup;
alter database mount;

step6: restore database;
recover database;
alter database open resetlogs;

表空間恢復

sql"alter tablespace users offline";
sql"alter tablespace tools offline";
restore tablespace users,tools;
recover tablespace users,tools;
sql"alter tablespace users online";
sql"alter tablespace tools online";

數據文件恢復

sql"alter database datafile 3 offline";
sql"alter database datafile 'd:oracleoradatausers01.dbf' offline";
restore datafile 3
restore datafile 'd:oracleoradatausers01.dbf';
recover datafile 3
recover datafile 'd:oracleoradatausers01.dbf';
sql"alter database datafile 3 online";
sql"alter database datafile 'd:oracleoradatausers01.dbf'online";

=====================================================
=====================================================
切換當前的incarnation 回到resetlogs 前RESET DATABASE TO INCARNATION inc_key

=====================================================
=====================================================

RMAN 高級恢復

1 基於時間點的恢復
run
{
set until time "to_date('07/01/02 15:00:00','mm/dd/yy hh24:mi:ss')"'
restore database;
recover database;
alter database open resetlogs;
}

2 基於SCN 的恢復
startup mount;
restore database UNTIL SCN 10000;
recover database UNTIL SCN 10000;
alter database open resetlogs;

3 基於日誌序列的恢復
startup mount;
restore database UNTIL SEQUENCE 100 thread 1;
recover database UNTIL SEQUENCE 100 thread 1;
alter database open resetlogs;

==================================

c:/> rman cmdfile='' log=''

在run{}腳本裡面,format後面的參數含義:%t代表當前時間,s%代表備份集,%p代表備份片


使用恢複目錄資料庫執行RMAN步驟
我想建立一個RMAN備份機制,本機為目標資料庫,sid為orcl10的資料庫為恢複目錄來做備份,但總不成功,以下是我的執行步驟:
        SQL> conn sys/e2000jl@orcl10 as sysdba
        Connected.
        SQL> create user rman
               2  identified by rman123;
        User created.
        SQL> grant dba to rman;
        Grant succeeded.

        C:\Documents and Settings\zouyang>rman target sys/system catalog rman/rman123@orcl10
         Recovery Manager: Release 10.1.0.2.0 - Production
         Copyright (c) 1995, 2004, Oracle.  All rights reserved.
         connected to target database: ORCL (DBID=1227672490)
         connected to recovery catalog database
         recovery catalog is not installed

         RMAN> show channel for device type disk;
         recovery catalog is not installed
         recovery catalog is not installed
         RMAN-00571: ===============================================
         RMAN-00569: =========ERROR MESSAGE STACK FOLLOWS ========
         RMAN-00571: ===============================================
         RMAN-03002: failure of show command at 10/14/2009 09:30:02
         RMAN-06429: RCVCAT database is not compatible with this version of RMAN

         RMAN>
         可以連接上恢複目錄資料庫,但是無法使用這個恢複目錄,Oracle報錯「recovery catalog is not installed」。

         在網上搜了下,重新建立步驟如下:
第一步:Create Recovery Catalog
         First create a user to hold the recovery catalog:
         -- Create tablepsace to hold repository
         SQL> create tablespace rman_tools
                2  datafile
                3  'D:\ORACLE\PRODUCT\10.1.0\ORADATA\ORCL10\rman01.dbf'
                4  size 100m;
          Tablespace created
         -- Create rman schema owner
         SQL> create user rman
                2  identified by rman123
                3  default tablespace rman_tools
                4  quota unlimited on rman_tools;
           User created

          C:\Documents and Settings\zouyang>rman catalog rman/rman123@orcl10
             Recovery Manager: Release 10.1.0.2.0 - Production
             Copyright (c) 1995, 2004, Oracle.  All rights reserved.
             RMAN-00571: ===============================================
             RMAN-00569: ====== ERROR MESSAGE STACK FOLLOWS ==========
             RMAN-00571: ===============================================
             RMAN-00554: initialization of internal recovery manager package failed
             RMAN-04004: error from recovery catalog database: ORA-01045: user RMAN lacks CREATE SESSION privilege; logon denied
          -- 報錯!要給rman用戶分配一定的許可權!
          SQL> grant connect,resource,create session,recovery_catalog_owner
                2        to rman;
           Grant succeeded

          C:\Documents and Settings\zouyang>rman  catalog rman/rman123@orcl10
           Recovery Manager: Release 10.1.0.2.0 - Production
           Copyright (c) 1995, 2004, Oracle.  All rights reserved.
           connected to recovery catalog database
           recovery catalog is not installed

          RMAN> create catalog tablespace rman_tools;
           recovery catalog created      ——很好!!建立了!!
         
          可是,當我重新連接,顯示RMAN設置信息時又報錯了!!
          C:\Documents and Settings\zouyang>rman target sys/system catalog rman/rman123@orcl10
          Recovery Manager: Release 10.1.0.2.0 - Production
          Copyright (c) 1995, 2004, Oracle.  All rights reserved.
          connected to target database: ORCL (DBID=1227672490)
          connected to recovery catalog database
          RMAN> show channel for device type disk;
          RMAN configuration parameters are:
          RMAN-00571: ===============================================
          RMAN-00569: ========= ERROR MESSAGE STACK FOLLOWS =======
          RMAN-00571: ===============================================
          RMAN-03002: failure of show command at 10/14/2009 10:32:36
          RMAN-06004: ORACLE error from recovery catalog database: RMAN-20001: target database not found in recovery catalog
          RMAN>

          又在網上查找原因,原來還需要註冊資料庫!!
第二步:Register Database
         Each database to be backed up by RMAN must be registered!
         RMAN> register database;
          database registered in recovery catalog
          starting full resync of recovery catalog
          full resync complete
         RMAN> show channel for device type disk;
          RMAN configuration parameters are:CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   'D:\BACKUP\RMAN\%d_%s.bak';
         RMAN>
   
         成功啦!!!
http://407882.blog.51cto.com/397882/212142
=========================================================
oracle10g用rman全備及控制文件在別的機器還原實驗


利用rman全備文件(控制文件、歸檔日誌、數據文件)在別處一台oracle10g上還原
要求兩台伺服器的oracle版本一致並且數據文件、控制文件、歸檔日誌文件目錄結構一樣
rman全備是用nocatalog方式備份的,所以備份集信息保存在控制文件里。在這裡證明一點,就是利用全備方式所備份出來的控制文件含的備份集信息是不全的,如果原來的控制文件丟失會給恢復帶來一定麻煩,只有通過
DBMS_BACKUP_RESTORE包來恢復。但如果打開自動備份控制文件功能就不一樣了。
rman全備腳本:
#!/bin/bash
rman target sys/sysdba nocatalog   configure controlfile autobackup on ;

全備后的文件如下:
cd /u01/rman
[oracle@test rman]$ ll
總計 754936
-rw-r----- 1 oracle oinstall 375029760 12-20 22:48 full_wapdb_01k2ot0u_1_1
-rw-r----- 1 oracle oinstall 339255296 12-20 22:48 full_wapdb_02k2ot0u_1_1
-rw-r----- 1 oracle oinstall   7110656 12-20 22:45 full_wapdb_03k2ot0u_1_1
-rw-r----- 1 oracle oinstall  50874880 12-20 22:49 redolog_wapdb_04k2ot6v_1_1
--------------------------------------------------------------------------------
由於備份是nocatalog所以備份集信息是寫到控制文件里的,用全備出來的控制文件信息是不全的。
來驗證一下:(把日誌文件 控制文件 數據文件刪除)
1.從全備文件恢復
[oracle@test ~]$ rman target /
Recovery Manager: Release 10.2.0.4.0 - Production on 星期六 12月 20 23:28:31 2008
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
connected to target database: oradb (not mounted)
RMAN> restore controlfile from '/u01/rman/full_wapdb_03k2ot0u_1_1';
Starting restore at 20-12月-08
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
output filename=/u01/app/oracle/oradata/oradb/control01.ctl
output filename=/u01/app/oracle/oradata/oradb/control02.ctl
output filename=/u01/app/oracle/oradata/oradb/control03.ctl
Finished restore at 20-12月-08
這樣三個控制文件就恢復了
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
RMAN> list backup;
using target database control file instead of recovery catalog
在恢復出來的控制文件里找不到備份的信息
2.用自動備份控制文件來恢復(把剛才的三個控制文件刪除)
RMAN> startup nomount;
connected to target database (not started)
Oracle instance started
Total System Global Area     167772160 bytes
Fixed Size                     1266392 bytes
Variable Size                 83889448 bytes
Database Buffers              79691776 bytes
Redo Buffers                   2924544 bytes
RMAN> exit
--------------------------------------------------------------------------------
2.再來看看用自動備份恢復的實驗
Recovery Manager complete.
[oracle@test ~]$ rman target /
Recovery Manager: Release 10.2.0.4.0 - Production on 星期六 12月 20 23:34:45 2008
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
connected to target database: oradb (not mounted)
RMAN> restore controlfile from '/u01/app/oracle/flash_recovery_area/ORADB/autobackup/2008_12_20/o1_mf_s_674002170_4nt1hx4z_.bkp';
Starting restore at 20-12月-08
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:04
output filename=/u01/app/oracle/oradata/oradb/control01.ctl
output filename=/u01/app/oracle/oradata/oradb/control02.ctl
output filename=/u01/app/oracle/oradata/oradb/control03.ctl
Finished restore at 20-12月-08
重新從autobackup里還原三個控制文件
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
RMAN> list backup;
using target database control file instead of recovery catalog
List of Backup Sets
===================
BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1       Full    6.77M      DISK        00:00:06     20-12月-08    
        BP Key: 1   Status: AVAILABLE  Compressed: NO  Tag: TAG20081220T224548
        Piece Name: /u01/rman/full_wapdb_03k2ot0u_1_1
  Control File Included: Ckp SCN: 339825       Ckp time: 20-12月-08
BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2       Full    357.65M    DISK        00:02:44     20-12月-08    
        BP Key: 2   Status: AVAILABLE  Compressed: NO  Tag: TAG20081220T224548
        Piece Name: /u01/rman/full_wapdb_01k2ot0u_1_1
  List of Datafiles in backup set 2
  File LV Type Ckp SCN    Ckp Time   Name
  ---- -- ---- ---------- ---------- ----
  1       Full 339824     20-12月-08 /u01/app/oracle/oradata/oradb/system01.dbf
  4       Full 339824     20-12月-08 /u01/app/oracle/oradata/oradb/users01.dbf
BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
3       Full    323.53M    DISK        00:02:45     20-12月-08    
        BP Key: 3   Status: AVAILABLE  Compressed: NO  Tag: TAG20081220T224548
        Piece Name: /u01/rman/full_wapdb_02k2ot0u_1_1
  List of Datafiles in backup set 3
  File LV Type Ckp SCN    Ckp Time   Name
  ---- -- ---- ---------- ---------- ----
  2       Full 339825     20-12月-08 /u01/app/oracle/oradata/oradb/undotbs01.dbf
  3       Full 339825     20-12月-08 /u01/app/oracle/oradata/oradb/sysaux01.dbf
BS Key  Size       Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
4       48.52M     DISK        00:00:21     20-12月-08    
        BP Key: 4   Status: AVAILABLE  Compressed: NO  Tag: TAG20081220T224901
        Piece Name: /u01/rman/redolog_wapdb_04k2ot6v_1_1
  List of Archived Logs in backup set 4
  Thrd Seq     Low SCN    Low Time   Next SCN   Next Time
  ---- ------- ---------- ---------- ---------- ---------
  1    29      327057     20-12月-08 340013     20-12月-08
這次rman找回了備份集的信息
3.開始恢復
RMAN> restore database;
Starting restore at 20-12月-08
Starting implicit crosscheck backup at 20-12月-08
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
Crosschecked 4 objects
Finished implicit crosscheck backup at 20-12月-08
Starting implicit crosscheck copy at 20-12月-08
using channel ORA_DISK_1
Finished implicit crosscheck copy at 20-12月-08
searching for all files in the recovery area
cataloging files...
cataloging done
List of Cataloged Files
=======================
File Name: /u01/app/oracle/flash_recovery_area/ORADB/autobackup/2008_12_20/o1_mf_s_674002170_4nt1hx4z_.bkp
File Name: /u01/app/oracle/flash_recovery_area/ORADB/archivelog/2008_12_20/o1_mf_1_30_4nt1jdhs_.arc
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/app/oracle/oradata/oradb/system01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/oradb/users01.dbf
channel ORA_DISK_1: reading from backup piece /u01/rman/full_wapdb_01k2ot0u_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=/u01/rman/full_wapdb_01k2ot0u_1_1 tag=TAG20081220T224548
channel ORA_DISK_1: restore complete, elapsed time: 00:00:45
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00002 to /u01/app/oracle/oradata/oradb/undotbs01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/oradb/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /u01/rman/full_wapdb_02k2ot0u_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=/u01/rman/full_wapdb_02k2ot0u_1_1 tag=TAG20081220T224548
channel ORA_DISK_1: restore complete, elapsed time: 00:00:45
Finished restore at 20-12月-08
RMAN> recover database noredo;
Starting recover at 20-12月-08
using channel ORA_DISK_1
Finished recover at 20-12月-08
RMAN> alter database open resetlogs;(注意在線日誌不能恢復,用resetlogs打開)
database opened
RMAN> exit
恢復結束。
結論:
如果用rman選擇nocatalog方式備份,因為備份信息放到控制文件里,所以控制文件非常重要,即使是全備,其里的備份信息也不全,必需打開自動備份控制文件configure controlfile autobackup on ;
一定要保存好自動備份文件。
               
               

本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u1/40226/showart_1730431.html

推薦閱讀文章

Bookmark the permalink ,來源:互聯網