RMAN不完全恢复
RMAN不完全恢复
1 概念
RMAN不完全恢复的三个标准模式:
- 基于time
- 基于scn
- 基于sequence
与手工不完全恢复相比原理类似,语法稍有不同:
1.1 备份数据库
清理表空间、旧的备份、进行一次全备
@/u01/backup/rman/hot.sql
run{ allocate channel c1 device type disk; allocate channel c2 device type disk; backup database format '/u01/backup/rman/db_%d_%T_%U.bak' plus archivelog format '/u01/backup/rman/ar_%d_%T_%U.bak'; backup current controlfile format '/u01/backup/rman/ctl_%d_%T_%U.bak'; release channel c1; release channel c2; }
1.2 配置环境变量NLS_DATE_FORMAT
vi .bash_profile
写入:
export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'
使其生效
source .bash_profile
2 【实战案例1】恢复spfile或controlfile
2.1 环境准备:修改环境变量
有一套全备份,controlfile AUTOBACKUP设为on
CONFIGURE CONTROLFILE AUTOBACKUP ON;
控制文件自动备份,备份目的地是fast_recovery_area
report need backup;
2.2 关闭数据库,删参数文件
shutdown abort;
cd $ORACLE_HOME/dbs rm spfileorcl.ora
startup
2.3 RMAN启动到nomount
startup nomount;
2.4 从自动备份转储spfile
查看快速恢复区中自动备份
restore spfile from '/u01/flash_recovery_area/ORCL/autobackup/2021_07_07/o1_mf_s_1077278436_jgb9q4z7_.bkp';
cd $ORACLE_HOME/dbs/
ll
查看在$ORACLE_HOME/dbs/目录下已经产生spfileorcl.ora文件,证明spfile恢复完成
起库
startup force;
2.5 恢复控制文件
shutdown abort;
rm /u01/app/oracle/oradata/ORCL/control*.ctl ll /u01/app/oracle/oradata/ORCL/
startup
restore controlfile from '/u01/flash_recovery_area/ORCL/autobackup/2021_07_07/o1_mf_s_1077278436_jgb9q4z7_.bkp';
alter database mount; recover database; alter database open resetlogs;
3 【实战案例2】恢复过去某个时间点误操作(基于time或scn)
3.1 环境准备:建表,插入数据
create table scott.t3(id number); insert into scott.t3 values(99); insert into scott.t3 values(999); insert into scott.t3 values(9999); commit; select * from scott.t3;
3.2 查看故障前的时间
select current_scn,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from v$database;
SCN:2906423
时间:2021-07-07 17:02:05
3.3 模拟误操作truncate表
truncate table scott.t3;
3.4 RMAN基于时间点做不完全恢复
run{ startup force mount; allocate channel c1 type disk; allocate channel c2 type disk; set until time "to_date('2021-07-07 17:02:05','yyyy-mm-dd hh24:mi:ss')"; restore database; recover database; alter database open resetlogs; }
3.5 验证
select * from scott.t3;
基于scn的方法与基于time相似,语法是set until scn 876383;
基于日志的方法类似手工恢复的例子,语法是set until sequence 3;
4 【实战案例3】RMAN恢复误删除表空间
本例要做的是drop tablespace test,然后再通过不完全恢复,使数据库在drop表空间前的那一刻打开,从而恢复testtbs表空间及t2表的内容。当前控制文件里没有testtbs表空间的记录,需要通过备份的控制文件进行恢复
4.1 准备工作:创建表空间、创建表、插入数据
create tablespace t_tbs datafile '/u01/app/oracle/oradata/ORCL/t_tbs01.dbf' size 5m; create table scott.t4(id number) tablespace t_tbs; insert into scott.t4 values(10); insert into scott.t4 values(20); commit; select * from scott.t4;
4.2 打开告警日志
查看drop tablespace的告警信息,然后记下时间点
tail -f /u01/app/oracle/diag/rdbms/orcl/orcl/trace/alert_orcl.log
4.3 模拟误删除t_tbs表空间
drop tablespace t_tbs including contents and datafiles;
4.4 查看告警有关信息
记录下误删除之前时间,是until time的时刻 2021-07-07 17:33:24
4.5 查看DBID
从自动备份转储控制文件需set DBID
connected to target database: ORCL (DBID=1597307870)
4.6 使用旧的控制文件做不完全恢复
run{ startup force nomount; set dbid=1597307870; restore controlfile from autobackup until time "to_date('2021-07-07 17:33:24','yyyy-mm-dd hh24:mi:ss')"; alter database mount; restore database; recover database until time "to_date('2021-07-07 17:33:24','yyyy-mm-dd hh24:mi:ss')"; alter database open resetlogs; }
4.7 验证
select * from scott.t4;
要点:
① 不使用当前的控制文件恢复误删除的表空间,因为当前控制文件已经没有该表空间的记录了
② DBID是RMAN识别数据库的身份证,保存在控制文件中,set dbid=对于控制文件自动恢复(from autobackup)是必要的;
5 【实战案例4】表空间时间点恢复(TSPITR)
作为一条基本原则,不完全恢复必须应用到整个数据库,即必须还原整个数据库并运用日志一起向前滚动
TSPITR是一种对个别表空间执行不完全恢复的技术,一般是针对用户错误的删除(或截断)了表。TSPITR 的最大好处是不需要生产库停机
前提是:
① 有一套全库备份,因为TSPITR这个过程除了复制需要恢复的表空间外,也必须复制system,sysaux和undo表空间(考点)
② 本例设置了控制文件自动备份:RMAN> CONFIGURE CONTROLFILE AUTOBACKUP ON;
【官方文档】
Backup and Recovery User's Guide ---> 21 Performing RMAN Tablespace Point-in-Time Recovery (TSPITR) ---> Performing Fully Automated RMAN TSPITR
5.1 环境准备:创建表空间、表
create tablespace test_tbs datafile '/u01/app/oracle/oradata/ORCL/test_tbs01.dbf' size 10m; create table scott.emp1 tablespace test_tbs as select * from scott.emp; select table_name,tablespace_name from dba_tables where table_name='EMP1';
select * from scott.emp1;
5.2 RMAN备份
@/u01/backup/rman/hot.sql
做全备
5.3 取当前时间(误操作前)
select current_scn,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from v$database;
SCN:2913007
时间:2021-07-07 18:06:37
5.4 模拟误删除表
drop table scott.emp1 purge;
5.5 模拟误删除后 表空间上继续做其他业务操作
create table scott.dept1 tablespace test_tbs as select * from scott.dept; select table_name,tablespace_name from dba_tables where table_name='DEPT1'; select * from scott.dept1;
5.6 检查自包含
exec DBMS_TTS.TRANSPORT_SET_CHECK('TEST_TBS',TRUE,TRUE); SELECT * FROM TRANSPORT_SET_VIOLATIONS;
如果此表空间上有索引,指向其他表空间上的表,则不满足自包含
5.7 查看TSPTR后丢失对象
故障前的正常SCN为 2913007
正常时间为 2021-07-07 18:06:37
col owner for a15 col name for a15 col tablespace_name for a20 SELECT OWNER,NAME,TABLESPACE_NAME,TO_CHAR(CREATION_TIME, 'yyyy-mm-dd hh24:mi:ss') FROM TS_PITR_OBJECTS_TO_BE_DROPPED WHERE TABLESPACE_NAME ='TEST_TBS' AND CREATION_TIME > TO_DATE('2021-07-07 18:06:37','yyyy-mm-dd hh24:mi:ss') ORDER BY TABLESPACE_NAME, CREATION_TIME;
既可以使用时间来查也可以通过SCN号来查
SELECT OWNER,NAME,TABLESPACE_NAME,TO_CHAR(CREATION_TIME,'YYYY-MM-DD:HH24:MI:SS') FROM TS_PITR_OBJECTS_TO_BE_DROPPED WHERE TABLESPACE_NAME ='TEST_TBS'AND CREATION_TIME > TO_DATE(TO_CHAR(SCN_TO_TIMESTAMP(2903117),'MM/DD/YYYY HH24:MI:SS'),'MM/DD/YYYY HH24:MI:SS') ORDER BY TABLESPACE_NAME, CREATION_TIME;
5.8 导出对象
创建导出文件夹
mkdir /u01/dir
create directory dir as '/u01/dir'; grant read,write on directory dir to public;
导出dept1表对象
cd /u01/dir/ expdp scott/tiger directory=dir dumpfile=dept1.dmp tables=dept1
5.9 建立目录指定辅助库目标
mkdir /u01/auxdest
5.10 做RMAN TSPITR并指定辅助库目的地
建议使用CRT log session方便查看过程
recover tablespace test_tbs until scn 2913007 auxiliary destination '/u01/auxdest';
输出内容较长,如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
RMAN> recover tablespace test_tbs until scn 2913007 auxiliary destination '/u01/auxdest'; Starting recover at 2021-07-07 18:26:19 current log archived allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=254 device type=DISK RMAN-05026: warning: presuming following set of tablespaces applies to specified point-in-time List of tablespaces expected to have UNDO segments Tablespace SYSTEM Tablespace UNDOTBS1 Creating automatic instance, with SID='ojjb' initialization parameters used for automatic instance: db_name=ORCL db_unique_name=ojjb_pitr_ORCL compatible=19.0.0 db_block_size=8192 db_files=200 diagnostic_dest=/u01/app/oracle _system_trig_enabled=FALSE sga_target=1584M processes=200 db_create_file_dest=/u01/auxdest log_archive_dest_1='location=/u01/auxdest' #No auxiliary parameter file used starting up automatic instance ORCL Oracle instance started Total System Global Area 1660940992 bytes Fixed Size 8897216 bytes Variable Size 402653184 bytes Database Buffers 1241513984 bytes Redo Buffers 7876608 bytes Automatic instance created Running TRANSPORT_SET_CHECK on recovery set tablespaces TRANSPORT_SET_CHECK completed successfully contents of Memory Script: { # set requested point in time set until scn 2913007; # restore the controlfile restore clone controlfile; # mount the controlfile sql clone 'alter database mount clone database'; # archive current online log sql 'alter system archive log current'; # avoid unnecessary autobackups for structural changes during TSPITR sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;'; } executing Memory Script executing command: SET until clause Starting restore at 2021-07-07 18:27:29 allocated channel: ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: SID=4 device type=DISK channel ORA_AUX_DISK_1: starting datafile backup set restore channel ORA_AUX_DISK_1: restoring control file channel ORA_AUX_DISK_1: reading from backup piece /u01/flash_recovery_area/ORCL/autobackup/2021_07_07/o1_mf_s_1077300341_jgbz3og0_.bkp channel ORA_AUX_DISK_1: piece handle=/u01/flash_recovery_area/ORCL/autobackup/2021_07_07/o1_mf_s_1077300341_jgbz3og0_.bkp tag=TAG20210707T180541 channel ORA_AUX_DISK_1: restored backup piece 1 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01 output file name=/u01/auxdest/ORCL/controlfile/o1_mf_jgc0dm8d_.ctl Finished restore at 2021-07-07 18:27:32 sql statement: alter database mount clone database sql statement: alter system archive log current sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end; contents of Memory Script: { # set requested point in time set until scn 2913007; plsql <<<-- declare sqlstatement varchar2(512); pdbname varchar2(128); offline_not_needed exception; pragma exception_init(offline_not_needed, -01539); begin pdbname := null; -- pdbname sqlstatement := 'alter tablespace '|| 'TEST_TBS' ||' offline immediate'; krmicd.writeMsg(6162, sqlstatement); krmicd.execSql(sqlstatement, 0, pdbname); exception when offline_not_needed then null; end; >>>; # set destinations for recovery set and auxiliary set datafiles set newname for clone datafile 1 to new; set newname for clone datafile 4 to new; set newname for clone datafile 3 to new; set newname for clone tempfile 1 to new; set newname for datafile 2 to "/u01/app/oracle/oradata/ORCL/test_tbs01.dbf"; # switch all tempfiles switch clone tempfile all; # restore the tablespaces in the recovery set and the auxiliary set restore clone datafile 1, 4, 3, 2; switch clone datafile all; } executing Memory Script executing command: SET until clause sql statement: alter tablespace TEST_TBS offline immediate executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME executing command: SET NEWNAME renamed tempfile 1 to /u01/auxdest/ORCL/datafile/o1_mf_temp_%u_.tmp in control file Starting restore at 2021-07-07 18:27:41 using channel ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: starting datafile backup set restore channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set channel ORA_AUX_DISK_1: restoring datafile 00001 to /u01/auxdest/ORCL/datafile/o1_mf_system_%u_.dbf channel ORA_AUX_DISK_1: restoring datafile 00002 to /u01/app/oracle/oradata/ORCL/test_tbs01.dbf channel ORA_AUX_DISK_1: reading from backup piece /u01/backup/rman/db_ORCL_20210707_1q03cj2o_1_1.bak channel ORA_AUX_DISK_1: piece handle=/u01/backup/rman/db_ORCL_20210707_1q03cj2o_1_1.bak tag=TAG20210707T180512 channel ORA_AUX_DISK_1: restored backup piece 1 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:25 channel ORA_AUX_DISK_1: starting datafile backup set restore channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/auxdest/ORCL/datafile/o1_mf_undotbs1_%u_.dbf channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/auxdest/ORCL/datafile/o1_mf_sysaux_%u_.dbf channel ORA_AUX_DISK_1: reading from backup piece /u01/backup/rman/db_ORCL_20210707_1r03cj2o_1_1.bak channel ORA_AUX_DISK_1: piece handle=/u01/backup/rman/db_ORCL_20210707_1r03cj2o_1_1.bak tag=TAG20210707T180512 channel ORA_AUX_DISK_1: restored backup piece 1 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15 Finished restore at 2021-07-07 18:28:23 datafile 1 switched to datafile copy input datafile copy RECID=8 STAMP=1077301704 file name=/u01/auxdest/ORCL/datafile/o1_mf_system_jgc0dzbb_.dbf datafile 4 switched to datafile copy input datafile copy RECID=9 STAMP=1077301704 file name=/u01/auxdest/ORCL/datafile/o1_mf_undotbs1_jgc0frcw_.dbf datafile 3 switched to datafile copy input datafile copy RECID=10 STAMP=1077301704 file name=/u01/auxdest/ORCL/datafile/o1_mf_sysaux_jgc0frbj_.dbf contents of Memory Script: { # set requested point in time set until scn 2913007; # online the datafiles restored or switched sql clone "alter database datafile 1 online"; sql clone "alter database datafile 4 online"; sql clone "alter database datafile 3 online"; sql clone "alter database datafile 2 online"; # recover and open resetlogs recover clone database tablespace "TEST_TBS", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog; alter clone database open resetlogs; } executing Memory Script executing command: SET until clause sql statement: alter database datafile 1 online sql statement: alter database datafile 4 online sql statement: alter database datafile 3 online sql statement: alter database datafile 2 online Starting recover at 2021-07-07 18:28:26 using channel ORA_AUX_DISK_1 starting media recovery archived log for thread 1 with sequence 2 is already on disk as file /u01/arch/arch_ORCL_1_2_1077299060.dbf archived log for thread 1 with sequence 3 is already on disk as file /u01/arch/arch_ORCL_1_3_1077299060.dbf archived log file name=/u01/arch/arch_ORCL_1_2_1077299060.dbf thread=1 sequence=2 archived log file name=/u01/arch/arch_ORCL_1_3_1077299060.dbf thread=1 sequence=3 media recovery complete, elapsed time: 00:00:04 Finished recover at 2021-07-07 18:28:36 database opened contents of Memory Script: { # make read only the tablespace that will be exported sql clone 'alter tablespace TEST_TBS read only'; # create directory for datapump import sql "create or replace directory TSPITR_DIROBJ_DPDIR as '' /u01/auxdest''"; # create directory for datapump export sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as '' /u01/auxdest''"; } executing Memory Script sql statement: alter tablespace TEST_TBS read only sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/auxdest'' sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/auxdest'' Performing export of metadata... EXPDP> Starting "SYS"."TSPITR_EXP_ojjb_grAD": EXPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/TABLE_STATISTICS EXPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKER EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE EXPDP> Master table "SYS"."TSPITR_EXP_ojjb_grAD" successfully loaded/unloaded EXPDP> ****************************************************************************** EXPDP> Dump file set for SYS.TSPITR_EXP_ojjb_grAD is: EXPDP> /u01/auxdest/tspitr_ojjb_86879.dmp EXPDP> ****************************************************************************** EXPDP> Datafiles required for transportable tablespace TEST_TBS: EXPDP> /u01/app/oracle/oradata/ORCL/test_tbs01.dbf EXPDP> Job "SYS"."TSPITR_EXP_ojjb_grAD" successfully completed at Wed Jul 7 18:30:49 2021 elapsed 0 00:00:44 Export completed contents of Memory Script: { # shutdown clone before import shutdown clone abort # drop target tablespaces before importing them back sql 'drop tablespace TEST_TBS including contents keep datafiles cascade constraints'; } executing Memory Script Oracle instance shut down sql statement: drop tablespace TEST_TBS including contents keep datafiles cascade constraints Performing import of metadata... IMPDP> Master table "SYS"."TSPITR_IMP_ojjb_ixfr" successfully loaded/unloaded IMPDP> Starting "SYS"."TSPITR_IMP_ojjb_ixfr": IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE IMPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/TABLE_STATISTICS IMPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKER IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK IMPDP> Job "SYS"."TSPITR_IMP_ojjb_ixfr" successfully completed at Wed Jul 7 18:31:41 2021 elapsed 0 00:00:40 Import completed contents of Memory Script: { # make read write and offline the imported tablespaces sql 'alter tablespace TEST_TBS read write'; sql 'alter tablespace TEST_TBS offline'; # enable autobackups after TSPITR is finished sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;'; } executing Memory Script sql statement: alter tablespace TEST_TBS read write sql statement: alter tablespace TEST_TBS offline sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end; Removing automatic instance Automatic instance removed auxiliary instance file /u01/auxdest/ORCL/datafile/o1_mf_temp_jgc0hhf4_.tmp deleted auxiliary instance file /u01/auxdest/ORCL/onlinelog/o1_mf_3_jgc0gqxb_.log deleted auxiliary instance file /u01/auxdest/ORCL/onlinelog/o1_mf_2_jgc0gnmp_.log deleted auxiliary instance file /u01/auxdest/ORCL/onlinelog/o1_mf_1_jgc0gnlq_.log deleted auxiliary instance file /u01/auxdest/ORCL/datafile/o1_mf_sysaux_jgc0frbj_.dbf deleted auxiliary instance file /u01/auxdest/ORCL/datafile/o1_mf_undotbs1_jgc0frcw_.dbf deleted auxiliary instance file /u01/auxdest/ORCL/datafile/o1_mf_system_jgc0dzbb_.dbf deleted auxiliary instance file /u01/auxdest/ORCL/controlfile/o1_mf_jgc0dm8d_.ctl deleted auxiliary instance file tspitr_ojjb_86879.dmp deleted Finished recover at 2021-07-07 18:31:53 RMAN>
5.11 验证
select tablespace_name,status from dba_tablespaces;
alter tablespace test_tbs online;
select owner,table_name from dba_tables where tablespace_name='test_tbs';
select * from scott.emp1;
5.12 导入丢失对象
cd /u01/dir/ impdp scott/tiger directory=dir dumpfile=dept1.dmp
select owner,table_name from dba_tables where tablespace_name='test_tbs'; select * from scott.dept1;
5.13 相关知识点
1) TSPITR是保持在线业务下表空间级的不完全恢复,恢复的表空间要自包含。使用TS_PITR_CHECK视图查看自包含信息
2) TSPITR一般用于将已有的表空间恢复到过去的某个时间点,而11gR2版使用可传输表空间和数据泵技术,Oracle声明可以恢复被删除的表空间
3) 通过TS_PITR_OBJECTS_TO_BE_DROPPED视图,查看creation time字段可以知道截止恢复时间之后的有哪些新建对象可能丢失了
4) 完成指定表空间的TSPITR后,这个表空间之前做的备份就不能再用于以后的TSPITR。这就是在TSPITR之后要重新备份表空间的原因