Saturday, January 10, 2009

oracle rman备份命令-备份恢复

oracle rman备份命令-备份恢复
http://www.stuchina.net/hy/ce/200811/129361.html
文章来源:转载 点击数:44 更新时间:2008-11-11 3:36:23 

  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; -- 删除所有备份 网管下载dl.bitscn.com

  改变备份集的状态

  change backupset id unavailable; -- available

  改为长期备份

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

  网管u家bitscn.net

  报告丢弃的备份

  report obsolete;

  删除丢弃状态的备份

  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;

  概述可用的备份

  list backup summary;

  得到了备份集得主键ID如40

  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

  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)
%t--tablespace name,
%s--backup sets sequence number,
%p--backup pieces sequence number

  脚本

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;
}

No comments: