oracle rman備份命令

wuyuanyong發表於2010-07-15

RMAN簡單使用

  目標機A:

  create tablespace RMAN datafile '$ORACLE_HOME/oradata/FILENAME.DBF' size 20M;

  RMAN簡單使用

  目標機A:

  create tablespace RMAN datafile '$ORACLE_HOME/oradata/FILENAME.DBF' size 20M;

  

create username RMAN identified by RMAN default tablespace rman temporary tablespace temp quota unlimited on RMAN;

  

grant recovery_catalog_owner to rman ;

  rman catalog rman/rman

  create catalog tablespace 'RMAN';

  Rman主機B:

  rman target /

  connect catalog rman/rman@$A_SID

  註冊資料庫

  register database;

  登出資料庫

  sqlplus rman/rman

  select * from db;

  EXECUTE dbms_rcvcat.unregisterdatabase(db_key, db_id);

  ///////////非歸檔模式使用RMAN備份///////////////////////

  備份目標資料庫:

  rman target /

  connect catalog rman/rman@$A_SID

  定義通道

  allocate channel c1 type disk;

  撤消通道

  release channel c1;

  備份整個資料庫

  run {

  allocate channel c1 type disk;

  backup full tag full_db_backup

  format '/export/home/oracle/oradata/bak_db/db_%d' database

  include current controlfile;

  release channel c1;

  }

  備份表空間

  run {

  allocate channel d1 type disk;

  backup tag tbs_users_read_only

  format '/export/home/oracle/oradata/bak_tab/block_t%t_s%s' (tablespace "BLOCK");

  release channel c1;

  }

  run {

  allocate channel d1 type disk;

  backup tablespace "BLOCK" filesperset 3 format '/export/home/oracle/oradata/bak_tab/aatst_%d%t%s.%p'

  include current controlfile;

  release channel c1;

  }

  備份單獨資料檔案

  run {

  allocate channel c1 type disk;

  backup format '/export/home/oracle/oradata/bak_tab/%d_%u'

  (datafile '/export/home/oracle/oradata/ECOM/block01.dbf');

  release channel c1;

  }

  複製目標資料庫資料檔案:

  run {

  allocate channel c1 type disk;

  copy datafile '/export/home/oracle/oradata/ECOM/block02.dbf' to '/export/home/oracle/oradata/bak_tab/block02.dbf' tag=u1215;

  release channel c1;

  }

  備份控制檔案:

  run {

  allocate channel c1 type disk;

  backup format '/export/home/oracle/oradata/bak_cont/cf_t%t_s%s_p%p'

  tag cf_monday_night (current controlfile);

  release channel c1;

  }

  run {

  allocate channel d1 type disk;

  backup current controlfile format '/export/home/oracle/oradata/bak_tab/%d%t%s.ctl';

  }

  run {

  allocate channel d1 type disk;

  copy current controlfile to '/export/home/oracle/oradata/bak_tab/%d%t%s.ctl';

  }

  /////////////////////歸檔模式使用RMAN備份//////////////////////

  歸檔日誌備份:

  run {

  allocate channel c1 type disk;

  backup format '/export/home/oracle/oradata/bak_log/log_t%t_s%s_p%p'

  (archivelog all);

  release channel c1;

  }

  下面的指令碼歸檔日誌從# 90 to 100

  run {

  allocate channel c1 typye disk;

  backup format '/export/home/oracle/oradata/bak_log/log_t%t_s%s_p%p'

  (archivelog from logseq=90 until logseq=100 thread 1);

  release channel c1;

  }

  下面的指令碼備份在24小時內產生的歸檔日誌,在備份完成後會自動刪除歸檔日誌。如果備份失敗,歸檔日誌不會被刪除。

  run {

  allocate channel c1 type disk;

  backup format '/export/home/oracle/oradata/bak_log/log_t%t_s%s_p%p'

  (archivelog from time 'sysdate-1' all delete input);

  release channel c1;

  }

  run {

  allocate channel c1 type disk;

  backup archivelog low logseq 1143 high logseq 1147 thread 1 format 'aaarc%t.%s';

  }

  使用下面的命令顯示恢復目錄中的歸檔日誌

  list backupset of archivelog all;

  備份聯機日誌

  run {

  allocate channel c1 type disk;

  sql "alter system archive log current";

  backup format '/export/home/oracle/oradata/bak_log/onlog_t%t_s%s_p%p'

  (archivelog from time 'sysdate-1' all delete input);

  release channel c1;

  }

  備份spfile

  run {

  allocate channel c1 type disk;

  backup spfile tag 'ECOM_spfile' format '/export/home/oracle/oradata/bak_conf/spfile_%t.ora';

  release channel c1;

  }

  清除聯機日誌備份

  delete copy;

  /* 如果物理檔案被刪除,用CROSSCHECK修復 */

  crosscheck archivelog all;

  delete noprompt obsolete;

  列出對應物

  list incarnation;

  概述可用的備份

  list backup summary;

  按備份型別列出備份

  list backup by file;

  列出詳細備份

  list backup;

  列出過期備份

  list expired backup;

  列出表空間和資料檔案備份

  list backup of tablespace ;

  list backup of datafile;

  列出歸檔日誌備份

  list archivelog all;

  list backup of archivelog all;

  列出控制檔案和伺服器引數檔案

  list backup of controfile;

  list backup of spfile;

  檢查備份

  crosscheck backup;

  crosscheck backup of database;

  crosscheck backup of tablespace system;

  crosscheck backup of controlfile;

  crosscheck backup of spfile;

  刪除備份

  allocate channel for delete type disk;

  change backupset BS Key delete;

  delete obsolete; 刪除陳舊備份

  delete expired backup;

  delete backupset id;

  delete backup; -- 刪除所有備份

  改變備份集的狀態

  change backupset id unavailable; -- available

  改為長期備份

  change backupset id keep forever logs;

  change backupset id keep until time 'sysdate+60' logs;

  change backupset id nokeep;

  報告丟棄的備份

  report obsolete;

  刪除丟棄狀態的備份

  delete obsolete;

  RMANcrosscheck 命令

  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;

  概述可用的備份

  list backup summary;

  得到了備份集得主鍵ID40

  validate backupset 40;

  命令刪除備份集

  change backupset ID1,ID2,ID3 delete;

  change backuppiece ID1 delete;

  change archivelog until logseq=ID1 delete;

  要檢視標記為丟棄的備份

  report obsolete;

  只有使用

  delete obsolete才真正物理刪除。

  手工同步恢復目錄

  resync catalog;

  --檢視RMAN的資訊

  list backup

  --列出過期備份

  report obsolete;

  --刪除過期的備份

  allocate channel for maintenance type disk;

  --allocate channel for delete type disk;

  change datafilecopy 44 delete;

  release channel;

  --如果目標資料庫物理物件發生了變化,如新增了一個資料檔案,需要用如下命令同步

  resync catalog;

  --如果目標資料庫reset了資料庫,需要用如下命令同步

  reset database;

  --當手工刪除了資料庫的歸檔檔案後,要執行以下指令碼同步

  allocate channel for maintenance type disk;

  --allocate channel for delete type disk;

  change archivelog all crosscheck;

  release channel;

  --當手工刪除了資料庫的RMAN備份後,要執行以下指令碼來同步

  allocate channel for maintenance type disk;

  crosscheck backup;

  delete expire backup;

  release channel;

  ////////////////////////////////////////////////// /增量備份 ////////////////////////////////////////////////

  Level 0 是增量備份策略的基礎

  run {

  allocate channel c1 type disk;

  backup incremental level 0 filesperset 4 format '/export/home/oracle/oradata/bak_db/sunday_level0_%t'

  (database);

  release channel c1;

  }

  ==================Recovery 恢復==================

  0.恢復控制檔案(以前必須有過控制檔案的備份)

  startup nomount

  rman target/

  connect catalog rman/rman

  (資料庫在nomount的時候才能set DBID)

  set DBID=3287725908

  1.Datafile recovery 資料檔案恢復

  run {

  allocate channel c1 type disk;

  sql "alter tablespace TB_name offline immediate";

  restore datafile 4;

  recover datafile 4;

  sql "alter tablespace TB_name online";

  release channel c1;

  }

  2.Tablespace recovery 表空間恢復

  run {

  allocate channel c1 type disk;

  sql "alter tablespace TB_name offline immediate";

  restore tablespace TB_name;

  recover tablespace TB_name;

  sql "alter tablespace TB_name online";

  release channel c1;

  }

  如果還原系統表空間檔案,資料庫必須關閉,因為系統表空間不可以離線

  =======完全還原(丟失聯機日誌)並且前滾 - 資料庫關閉=======

  run {

  allocate channel c1 type disk;

  set until logseq=105 thread=1;

  restore controlfile to '/export/home/oracle/oradata/ECOM/control01.ctl'

  replicate controlfile from '/export/home/oracle/oradata/bak_cont/ctrltargdb.ctl' ;

  restore database;

  sql "alter database mount";

  recover database;

  sql "alter database open resetlogs";

  release channel c1;

  }

  'set until' 命令指明恢復到指定的日誌檔案。這一點在資料檔案恢復時很重要,否則RMAN將試圖恢復最近的資料檔案,該資料檔案可能在指定的日誌以前。

  'replicate controlfile' 複製還原的控制檔案到INIT.ORA指定的控制檔案。

  如果資料庫使用WITH RESETLOGS開啟,則需要使用RESET DATABASE命令註冊改變後的資料庫。在使用RESETLOGS命令開啟資料庫後強烈建議做一個完全的資料庫備份。

  還原資料檔案的子集,完全恢復

  run {

  allocate channel c1 type disk;

  sql "alter database mount";

  restore datafile 2;

  restore datafile 3;

  restore archivelog all;

  recover database;

  sql "alter database open";

  release channel c1;

  }

  恢復表空間

  startup mount

  rman target /

  run { allocate channel c1 type disk ; restore tablespace 'TB_NAME';recover tablespace 'TB_NAME';}

  或者

  run { allocate channel c1 type disk ; restore database ;recover database ;}

  sqlplus /nolog

  alter database open;

  指令碼

  create script alloc_disk {

  Allocates one disk

  allocate channel c1 type disk;

  setlimit channel c1 kbytes 2097150 maxopenfiles 32 readrate 200;

  }

  replace script rel_disk {

  releases disk

  release channel c1;

  }

  replace script backup_db_full {

  execute script alloc_disk;

  backup

  .....

  execute script rel_disk;

  }

  前兩個指令碼分別用來分配和回收通道。

  alloc_disk 指令碼還額外指定了備份片的最大兆位元組數,備份時可以同時開啟的輸入檔案的最大數目,以及每秒鐘讀每個輸入檔案的資料緩衝區的最大數目。

  第三個指令碼呼叫先前儲存的兩個指令碼進行資料庫備份。

  執行儲存指令碼的示範:

  run {

  execute script backup_db_full;

  }

  檢視備份及複製的資訊

  list backup;

  list copy;

  在備份是設定相關引數

  format --檔案輸出格式,

  %d--database name, (SID)

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

相關文章