ORACLE RMAN备份恢复单个表空间

原因

单个库可能有很多的冗余数据,比如查询库,通过ogg等方式同的数据,当这台库上做其他的分析或者是另一个有业务的主库的时。就有需求做备份恢复。
但是本身是一个查询库,整库rman备份非常的慢。
通过nbu或者rman可以备份单个schema(一个schema对应一个表空间),这样速度快,恢复快。

步骤

1.准备好参数文件(略)
2.获取控制文件,启动到mount
startup nomount;
set controlfile autobackup format for device type disk to '/u01/app/oracle/oradata/%F';
SET DBID=${dbid}
run 
{ 
allocate channel d1 type 'SBT_TAPE'; 
send 'NB_ORA_SERV=NBU01,NB_ORA_CLIENT=${HOST_NAME}'; 
restore controlfile from '${crontrol_file}';     
release channel d1; 
} 
alter database mount;
reset database to incarnation 2;  有时候恢复失败,但是重试会有问题。
exit;

3.restore
run
{
`cat /home/oracle/recovery_test/config/file_name_${ORACLE_SID}.ls`
allocate channel ch00 type 'SBT_TAPE';
allocate channel ch01 type 'SBT_TAPE';
allocate channel ch03 type 'SBT_TAPE';
allocate channel ch04 type 'SBT_TAPE';
send 'NB_ORA_SERV=NBU01,NB_ORA_CLIENT=${HOST_NAME}';
RESTORE TABLESPACE SYSTEM,SYSAUX,USERS,UNDOTBS2,${SCHEMA};
`cat /home/oracle/recovery_test/config/delete_file_name_${ORACLE_SID}.ls`
SWITCH DATAFILE ALL;
SWITCH TEMPFILE ALL;
release channel ch00;
release channel ch01;
release channel ch03;
release channel ch04;
}
exit;

这里的/home/oracle/recovery_test/config/file_name_${ORACLE_SID}.ls 是通过查询出来的数据文件的批量文件,需要该路径:

set heading off feedback off pagesize 0 verify off echo off
set linesize 1000
select name_ from 
(select file_id, 'set newname for datafile ' || FILE_ID || ' to ''' ||
       '/u01/app/oracle/oradata/${ORACLE_SID}' ||
       substr(FILE_NAME, instr(FILE_NAME, '/', -1)) || ''';' as name_
  from dba_data_files 
   where  tablespace_name in ('SYSTEM','SYSAUX','UNDOTBS2','USERS','${SCHEMA}')
union all   
select file_id,'set newname for TEMPFILE ' || FILE_ID || ' to ''' ||  
       '/u01/app/oracle/oradata/${ORACLE_SID}' ||
       substr(FILE_NAME, instr(FILE_NAME, '/', -1)) || ''';' as name
  from dba_temp_files
)
  order by file_id;
exit;

cat /home/oracle/recovery_test/config/delete_file_name_${ORACLE_SID}.ls 同样是删除不需要恢复的数据文件

4.recover
run
{
sql 'alter session set nls_date_format="yyyymmddhh24miss"';
allocate channel ch00 type 'SBT_TAPE';
allocate channel ch01 type 'SBT_TAPE';
allocate channel ch03 type 'SBT_TAPE';
allocate channel ch04 type 'SBT_TAPE';
send 'NB_ORA_SERV=NBU01,NB_ORA_CLIENT= ${HOST_NAME}'; 
recover database until time "to_date('$str2','yyyymmddhh24miss')" skip tablespace  ${tbss} ;
  release channel ch00;
  release channel ch01;
  release channel ch03;
  release channel ch04;
}
exit;

${tbss} 是对应的需要跳过的表空间,也是通过查询得到的一个参数
这个时间点可以要设置好,也可以是scn

5.open resetlogs
run
{
sql 'alter session set nls_date_format="yyyymmddhh24miss"';
allocate channel ch00 type 'SBT_TAPE';
allocate channel ch01 type 'SBT_TAPE';
allocate channel ch03 type 'SBT_TAPE';
allocate channel ch04 type 'SBT_TAPE';
send 'NB_ORA_SERV=NBU01,NB_ORA_CLIENT=  ${HOST_NAME}'; 
 alter database open resetlogs;
  release channel ch00;
  release channel ch01;
  release channel ch03;
  release channel ch04;
}

这样基本上能恢复出对应的想要的表空间。这个基于恢复测试的写的一个脚本中的关键步骤。

你可能感兴趣的:(oracle)