重庆思庄Oracle、Redhat认证学习论坛

标题: Oracle 表空间基于时间点恢复TSPITR [打印本页]

作者: 郑全    时间: 2014-5-9 18:38
标题: Oracle 表空间基于时间点恢复TSPITR
Oracle 表空间基于时间点恢复TSPITR
TSPITR介绍
   TSPITR(表空间时间点恢复)用于将一个或多个表空间恢复到过去某个时间点的状态,而其他表空间仍然保持现有状态。
   (1) TSPITR(Tablespace Point-In-Time Recovery),只适用于ARCHIVELOG模式。
   (2) TSPITR实现方法,建议使用RMAN实现表空间时间点恢复。
   (3) DBPITR(Database Point-In-TIme Recovery),数据库时间点恢复;表示将数据库的所有表空间恢复到过去时间颠倒俄状态,只适用于ARCHIVELOG模式。
   (4) 主数据库(Primary Database),用于存放应用系统数据的Oracle数据库。当执行TSPITR时,主数据库是指包含有被恢复表空间的数据库。
   (5) 恢复集(Recovery Set),是指在主数据库上需要执行TSPITR的表空间集合。注意,当在恢复集的表空间上执行TSPITR时,要求这些表空间必须是自包含的。
   (6) 辅助数据库(Auxiliary Database),是主数据库的一个副本数据库。当执行TSPITR时,辅助数据库用于将恢复集表空间恢复到过去时间点。注意,辅助数据库的所有物理文件都是从主数据库备份中取得,并且辅助数据库必须包含SYSTEM表空间、UNDO表空间、恢复集表空间的备份文件。
   (7) 辅助集(Auxiliary Set),是指辅助数据库所需要的、除了恢复集表空间文件之外地饿其他文件集合。当执行TSPITR时,辅助数据库除了需要恢复表空间的备份文件之外,还需要备份控制文件、SYSTEM表空间的备份文件、UNDO表空间的备份文件。

注意:恢复集表空间必须为自包含;
       违反自包含表空间集合的常见情况如下:
       1> 表空间集合包含有SYS方案对象
       2> 表空间集合包含了索引所在的表空间,但没有包含索引基表所在的表空间
       3> 表空间集合没有包含分区表的所有分区
       4> 表空间集合包含了表所在的表空间,但没有包含其LOB列所在的表空间
检查自包含方式:
    SQL> connect sys/oracle as sysdba
    SQL> execute dbms_tts.transport_set_check('TS_SZTECH',true);
    SQL> Select * From transport_set_violations;

作者: 郑全    时间: 2014-5-9 18:40
以下是实际演示过程

环境 10.2.0.5 + LINUX 5.5

--创建表空间
CREATE TABLESPACE ts_sztech DATAFILE '/u01/app/oracle/oradata/orcl/ts_sztech.dba' SIZE 50M;
  

--创建用户
create user sztech identified by sztech defaut tablespace ts_sztech;
grant connect,resource to sztech;

 
-- create table

conn sztech/sztech

create table ttest (i_id integer, c_name varchar2(10)) tablespace ts_sztech;
create index itest on ttest(i_id) tablespace ts_sztech;

insert into ttest(i_id,c_name) values (1, '1');
insert into ttest(i_id,c_name) values (2, '2');
insert into ttest(i_id,c_name) values (3, '3');
insert into ttest(i_id,c_name) values (4, '4');
insert into ttest(i_id,c_name) values (5, '5');
insert into ttest(i_id,c_name) values (6, '6');
insert into ttest(i_id,c_name) values (7, '7');
insert into ttest(i_id,c_name) values (8, '8');
insert into ttest(i_id,c_name) values (8, '9');
insert into ttest(i_id,c_name) values (10, '10');
insert into ttest(i_id,c_name) values (11, '11');


SQL> select * from ttest;

      I_ID C_NAME
---------- ----------
         1 1
         2 2
         3 3
         4 4
         5 5
         6 6
         7 7
         8 8
         8 9
        10 10
        11 11

11 rows selected.

作者: 郑全    时间: 2014-5-9 18:45
--check 跨表空间的对象
SELECT *
  FROM SYS.TS_PITR_CHECK
 WHERE (TS1_NAME IN ('TS_SZTECH') AND TS2_NAME NOT IN ('TS_SZTECH'))
    OR (TS1_NAME NOT IN ('TS_SZTECH') AND TS2_NAME IN ('TS_SZTECH'));
       


--执行rman备份
run{
  allocate channel d1 type disk;
  backup tag ts_test
  format '/home/oracle/TS_%t_%s.bkp'
  tablespace ts_sztech,UNDOTBS1,SYSTEM;
  backup current controlfile;
  release channel d1;
}


作者: 郑全    时间: 2014-5-9 18:45
--记住时间点,后面我们恢复,就恢复到这个时间

SQL> SELECT to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2014-05-09 18:16:09

作者: 郑全    时间: 2014-5-9 18:46
标题: 模拟破坏操作
--模拟破坏操作 修改ttest的表

SQL> conn sztech/sztech
Connected.
SQL> delete from ttest where c_name like '%1%';

3 rows deleted.

--再看,已经只有3行了。

SQL> select * from ttest;

      I_ID C_NAME
---------- ----------
         2 2
         3 3
         4 4
         5 5
         6 6
         7 7
         8 8
         8 9

8 rows selected.

SQL> commit;

作者: 郑全    时间: 2014-5-9 18:47
标题: 执行基于时间点的表空间恢复
--执行基于时间点的表空间恢复
  恢复到前面删除操作之前的时间

run {
    recover tablespace ts_sztech until time "to_date('2014-05-09 18:16:09','yyyy-mm-dd hh24:mi:ss')" 
             auxiliary destination '/home/oracle/auxiliary';
  }

作者: 郑全    时间: 2014-5-9 18:49
标题: 具体过程结果明细
[oracle@dbserver ~]$ rman target /

Recovery Manager: Release 10.2.0.5.0 - Production on Fri May 9 18:27:07 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: ORCL (DBID=1372923444)

RMAN> run {
2>     recover tablespace ts_sztech until time "to_date('2014-05-09 18:16:09','yyyy-mm-dd hh24:mi:ss')" auxiliary destination '/home/oracle/auxiliary';
3>   }

下面部分,全部是自动完成,不再需要手工操作:


Starting recover at 09-MAY-14
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=133 devtype=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='aBAz'

initialization parameters used for automatic instance:
db_name=ORCL
compatible=10.2.0.5.0
db_block_size=8192
db_files=200
db_unique_name=tspitr_ORCL_aBAz
sga_target=180M
processes=50
#No auxiliary parameter file used
db_create_file_dest=/home/oracle/auxiliary
control_files=/home/oracle/auxiliary/cntrl_tspitr_ORCL_aBAz.f


starting up automatic instance ORCL

Oracle instance started

Total System Global Area     188743680 bytes

Fixed Size                     1272696 bytes
Variable Size                 62915720 bytes
Database Buffers             117440512 bytes
Redo Buffers                   7114752 bytes
Automatic instance created

contents of Memory Script:
{
# set the until clause
set until  time "to_date('2014-05-09 18:16:09','yyyy-mm-dd hh24:mi:ss')";
# restore the controlfile
restore clone controlfile;
# mount the controlfile
sql clone 'alter database mount clone database';
# archive current online log for tspitr to a resent until time
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 09-MAY-14
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=47 devtype=DISK

channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/flash_recovery_area/ORCL/autobackup/2014_05_09/o1_mf_s_847131214_9psblhhh_.bkp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/autobackup/2014_05_09/o1_mf_s_847131214_9psblhhh_.bkp tag=TAG20140509T181334
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
output filename=/home/oracle/auxiliary/cntrl_tspitr_ORCL_aBAz.f
Finished restore at 09-MAY-14

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:
{
# generated tablespace point-in-time recovery script
# set the until clause
set until  time "to_date('2014-05-09 18:16:09','yyyy-mm-dd hh24:mi:ss')";
plsql <<<-- tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := 'alter tablespace '||  'TS_SZTECH' ||' offline for recover';
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;
# set an omf destination filename for restore
set newname for clone datafile  1 to new;
# set an omf destination filename for restore
set newname for clone datafile  2 to new;
# set an omf destination tempfile
set newname for clone tempfile  1 to new;
# set an omf destination tempfile
set newname for clone tempfile  2 to new;
# set an omf destination tempfile
set newname for clone tempfile  3 to new;
# set an omf destination tempfile
set newname for clone tempfile  4 to new;
# set an omf destination tempfile
set newname for clone tempfile  5 to new;
# set an omf destination tempfile
set newname for clone tempfile  6 to new;
# set an omf destination tempfile
set newname for clone tempfile  7 to new;
# set a destination filename for restore
set newname for datafile  8 to 
 "/u01/app/oracle/oradata/orcl/ts_sztech.dba";
# rename all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set plus the auxilliary tablespaces
restore clone datafile  1, 2, 8;
switch clone datafile all;
#online the datafiles restored or flipped
sql clone "alter database datafile  1 online";
#online the datafiles restored or flipped
sql clone "alter database datafile  2 online";
#online the datafiles restored or flipped
sql clone "alter database datafile  8 online";
# make the controlfile point at the restored datafiles, then recover them
recover clone database tablespace  "TS_SZTECH", "SYSTEM", "UNDOTBS1" delete archivelog;
alter clone database open resetlogs;
# PLUG HERE the creation of a temporary tablespace if export fails due to lack
# of temporary space.
# For example in Unix these two lines would do that:
#sql clone "create tablespace aux_tspitr_tmp
#           datafile ''/tmp/aux_tspitr_tmp.dbf'' size 500K";
}
executing Memory Script

executing command: SET until clause

sql statement: alter tablespace TS_SZTECH offline for recover

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME



作者: 郑全    时间: 2014-5-9 18:50
renamed temporary file 1 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_%u_.tmp in control file
renamed temporary file 2 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_/u01/app/oracle/product/10.2.0/db_1/u01/app/oracle/product/10.2.0/db_1/u01/app/oracle/product/10.2.0/db_1_%u_.tmp in control file
renamed temporary file 3 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp2_%u_.tmp in control file
renamed temporary file 4 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp3_%u_.tmp in control file
renamed temporary file 5 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_sss_%u_.tmp in control file
renamed temporary file 6 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_1_%u_.tmp in control file
renamed temporary file 7 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_2_%u_.tmp in control file

Starting restore at 09-MAY-14
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_system_%u_.dbf
restoring datafile 00002 to /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_undotbs1_%u_.dbf
restoring datafile 00008 to /u01/app/oracle/oradata/orcl/ts_sztech.dba
channel ORA_AUX_DISK_1: reading from backup piece /home/oracle/TS_847131157_7.bkp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/home/oracle/TS_847131157_7.bkp tag=TS_TEST
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:26
Finished restore at 09-MAY-14

datafile 1 switched to datafile copy
input datafile copy recid=5 stamp=847132076 filename=/home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_system_9pscdm24_.dbf
datafile 2 switched to datafile copy
input datafile copy recid=6 stamp=847132076 filename=/home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_undotbs1_9pscdm2d_.dbf

sql statement: alter database datafile  1 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  8 online

Starting recover at 09-MAY-14
using channel ORA_AUX_DISK_1



作者: 郑全    时间: 2014-5-9 18:53
starting media recovery

archive log thread 1 sequence 1 is already on disk as file /u01/app/oracle/flash_recovery_area/ORCL/archivelog/2014_05_09/o1_mf_1_1_9pscd80r_.arc
archive log filename=/u01/app/oracle/flash_recovery_area/ORCL/archivelog/2014_05_09/o1_mf_1_1_9pscd80r_.arc thread=1 sequence=1
media recovery complete, elapsed time: 00:00:01
Finished recover at 09-MAY-14

database opened

contents of Memory Script:
{
# export the tablespaces in the recovery set
host 'exp userid =\"/@\(DESCRIPTION=\(ADDRESS=\(PROTOCOL=beq\)\(PROGRAM=/u01/app/oracle/product/10.2.0/db_1/bin/oracle\)\(ARGV0=oracleaBAz\)\(ARGS=^'\(DESCRIPTION=\(LOCAL=YES\)\(ADDRESS=\(PROTOCOL=beq\)\)\)^'\)\(ENVS=^'ORACLE_SID=aBAz^'\)\)\(CONNECT_DATA=\(SID=aBAz\)\)\) as sysdba\" point_in_time_recover=y tablespaces=
 TS_SZTECH file=
tspitr_a.dmp';
# shutdown clone before import
shutdown clone immediate
# import the tablespaces in the recovery set
host 'imp userid =\"/@ as sysdba\" point_in_time_recover=y file=
tspitr_a.dmp';
# online/offline the tablespace imported
sql "alter tablespace TS_SZTECH online";
sql "alter tablespace  TS_SZTECH offline";
# enable autobackups in case user does open resetlogs from RMAN after TSPITR
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script


Export: Release 10.2.0.5.0 - Production on Fri May 9 18:28:06 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses ZHS16GBK character set (possible charset conversion)
Note: table data (rows) will not be exported

About to export Tablespace Point-in-time Recovery objects...
For tablespace TS_SZTECH ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                          TTEST
. exporting referential integrity constraints
. exporting triggers
. end point-in-time recovery
Export terminated successfully without warnings.
host command complete

database closed
database dismounted
Oracle instance shut down






作者: 郑全    时间: 2014-5-9 18:53
Import: Release 10.2.0.5.0 - Production on Fri May 9 18:28:20 2014

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V10.02.01 via conventional path
About to import Tablespace Point-in-time Recovery objects...
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses ZHS16GBK character set (possible charset conversion)
. importing SYS's objects into SYS
. importing SZTECH's objects into SZTECH
. . importing table                        "TTEST"
. importing SYS's objects into SYS
Import terminated successfully without warnings.
host command complete

sql statement: alter tablespace TS_SZTECH online

sql statement: alter tablespace  TS_SZTECH offline

sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;

Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/auxiliary/cntrl_tspitr_ORCL_aBAz.f deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_system_9pscdm24_.dbf deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_undotbs1_9pscdm2d_.dbf deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_9pscfm2v_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_/u01/app/oracle/product/10.2.0/db_1/u01/app/oracle/product/10.2.0/db_1/u01/app/oracle/product/10.2.0/db_1_%u_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp2_9pscfm58_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp3_9pscfm5x_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_sss_9pscfm6p_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_1_9pscfm7n_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/datafile/o1_mf_temp_2_9pscfm8k_.tmp deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/onlinelog/o1_mf_1_9pscfgj0_.log deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/onlinelog/o1_mf_2_9pscfhtx_.log deleted
auxiliary instance file /home/oracle/auxiliary/TSPITR_ORCL_ABAZ/onlinelog/o1_mf_3_9pscfk3m_.log deleted
Finished recover at 09-MAY-14

RMAN> 

还原成功,退出rman

作者: 郑全    时间: 2014-5-9 18:54
标题: 验证结果
--验证结果

[oracle@dbserver ~]$ sqlplus /nolog

SQL*Plus: Release 10.2.0.5.0 - Production on Fri May 9 18:29:46 2014

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

SQL> connect sztech/sztech
Connected.
SQL> select * from ttest;
select * from ttest
              *
ERROR at line 1:
ORA-00376: file 8 cannot be read at this time
ORA-01110: data file 8: '/u01/app/oracle/oradata/orcl/ts_sztech.dba'

表空间已经离线了,需要在线

SQL> conn / as sysdba
Connected.
SQL> alter tablespace TS_SZTECH online;

再次执行

SQL> conn sztech/sztech
Connected.
SQL> select * from ttest;

      I_ID C_NAME
---------- ----------
         1 1
         2 2
         3 3
         4 4
         5 5
         6 6
         7 7
         8 8
         8 9
        10 10
        11 11

11 rows selected.

到此,tspitr恢复完成。





欢迎光临 重庆思庄Oracle、Redhat认证学习论坛 (http://bbs.cqsztech.com/) Powered by Discuz! X3.2