[20170627]使用TSPITR恢复表空间.txt

--//RMAN提供了一种实现所谓TSPITR(Tablespace Point-In-Time Recovery)的技术,通过简单的一个语句,就可以在主库不停库(很吸引人)
--//的情况下,利用备份集和连续的归档日志,实现表空间级别的定点恢复。

--//实际上rman就是把人工一步一步执行的命令打包,执行恢复工作,减轻dba负担.我一直认为这个不是很实用,前几天做了利用传输表空
--//间的测试,感觉那个更实用一些.
--//blog.itpub.net/267265/viewspace-2141166/=>[20170623]利用传输表空间恢复部分数据.txt

--//因为当误操作发生时,许多事务还是作用在这个表空间,为了取消这个误操作,要恢复到表空间特定的时间点.而丢失一些事务,
--//总之dba要选择这样的操作时要权衡利弊.

--//实际上,以前也做过类似的测试,只不过没有写出来,今天完善这方面的内容,做一个测试:

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> create table emp2 tablespace tea as select * from emp ;
Table created.

SCOTT@book> create table dept2 tablespace tea as select * from dept ;
Table created.

SCOTT@book> create table t tablespace tea as select * from all_objects ;
Table created.

SCOTT@book> alter system archive log current ;
System altered.

2.做一次rman全备份:
backup database format '/home/oracle/backup/full_20170627_%U.bak';
backup archivelog all  format '/home/oracle/backup/archive_20170627_%U';

3.开始模拟误操作.

SCOTT@book> set numw 12
SCOTT@book> select current_scn,sysdate from v$database;
 CURRENT_SCN SYSDATE
------------ -------------------
 13276934358 2017-06-27 09:05:36

SCOTT@book> truncate table emp2;
Table truncated.

SCOTT@book> drop table  dept2 purge ;
Table dropped.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       84762

SCOTT@book> delete from t where owner='SYS';
37354 rows deleted.

SCOTT@book> commit;
Commit complete.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       47408

4.开始恢复看看:
$ mkdir /home/oracle/aux/

--//执行命令: recover tablespace tea until scn 13276934358 auxiliary destination '/home/oracle/aux';

RMAN> recover tablespace tea until scn 13276934358 auxiliary destination '/home/oracle/aux';

Starting recover at 2017-06-27 09:08:39
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=14 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=26 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=37 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='Bndc'

initialization parameters used for automatic instance:
db_name=BOOK
db_unique_name=Bndc_tspitr_BOOK
compatible=11.2.0.4.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
db_create_file_dest=/home/oracle/aux
log_archive_dest_1='location=/home/oracle/aux'
#No auxiliary parameter file used

starting up automatic instance BOOK

Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2260088 bytes
Variable Size                285213576 bytes
Database Buffers             771751936 bytes
Redo Buffers                   9711616 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 13276934358;
# 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 2017-06-27 09:08:47
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=127 device type=DISK
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: SID=133 device type=DISK
allocated channel: ORA_AUX_DISK_3
channel ORA_AUX_DISK_3: SID=139 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/app/oracle/fast_recovery_area/BOOK/autobackup/2017_06_27/o1_mf_s_947754283_do3csclx_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/BOOK/autobackup/2017_06_27/o1_mf_s_947754283_do3csclx_.bkp tag=TAG20170627T090443
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/home/oracle/aux/BOOK/controlfile/o1_mf_do3d10mf_.ctl
Finished restore at 2017-06-27 09:08:49

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 13276934358;
plsql <<<-- tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := 'alter tablespace '||  'TEA' ||' offline immediate';
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
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  3 to new;
set newname for clone datafile  2 to new;
set newname for clone tempfile  1 to new;
set newname for datafile  6 to
 "/mnt/ramdisk/book/tea01.dbf";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 3, 2, 6;
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause
sql statement: alter tablespace TEA 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 /home/oracle/aux/BOOK/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 2017-06-27 09:08:54
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

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 00003 to /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00006 to /mnt/ramdisk/book/tea01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /home/oracle/backup/full_20170627_f8s7r50e_1_1.bak
channel ORA_AUX_DISK_2: starting datafile backup set restore
channel ORA_AUX_DISK_2: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_2: restoring datafile 00001 to /home/oracle/aux/BOOK/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_2: reading from backup piece /home/oracle/backup/full_20170627_f9s7r50e_1_1.bak
channel ORA_AUX_DISK_3: starting datafile backup set restore
channel ORA_AUX_DISK_3: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_3: restoring datafile 00002 to /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_3: reading from backup piece /home/oracle/backup/full_20170627_f7s7r50e_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/home/oracle/backup/full_20170627_f8s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_2: piece handle=/home/oracle/backup/full_20170627_f9s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_2: restored backup piece 1
channel ORA_AUX_DISK_2: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_3: piece handle=/home/oracle/backup/full_20170627_f7s7r50e_1_1.bak tag=TAG20170627T085958
channel ORA_AUX_DISK_3: restored backup piece 1
channel ORA_AUX_DISK_3: restore complete, elapsed time: 00:00:15
Finished restore at 2017-06-27 09:09:09

datafile 1 switched to datafile copy
input datafile copy RECID=16 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_system_do3d16td_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=17 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_do3d16sz_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=18 STAMP=947754549 file name=/home/oracle/aux/BOOK/datafile/o1_mf_sysaux_do3d16tf_.dbf

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934358;
# online the datafiles restored or switched
sql clone "alter database datafile  1 online";
sql clone "alter database datafile  3 online";
sql clone "alter database datafile  2 online";
sql clone "alter database datafile  6 online";
# recover and open resetlogs
recover clone database tablespace  "TEA", "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  3 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  6 online

Starting recover at 2017-06-27 09:09:10
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

starting media recovery
archived log for thread 1 with sequence 696 is already on disk as file /u01/app/oracle/archivelog/book/1_696_896605872.dbf
archived log for thread 1 with sequence 697 is already on disk as file /u01/app/oracle/archivelog/book/1_697_896605872.dbf
archived log for thread 1 with sequence 698 is already on disk as file /u01/app/oracle/archivelog/book/1_698_896605872.dbf
archived log file name=/u01/app/oracle/archivelog/book/1_696_896605872.dbf thread=1 sequence=696
archived log file name=/u01/app/oracle/archivelog/book/1_697_896605872.dbf thread=1 sequence=697
archived log file name=/u01/app/oracle/archivelog/book/1_698_896605872.dbf thread=1 sequence=698
media recovery complete, elapsed time: 00:00:00
Finished recover at 2017-06-27 09:09:11

database opened

contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone 'alter tablespace  TEA read only';
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
}
executing Memory Script

sql statement: alter tablespace  TEA read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

Performing export of metadata...
   EXPDP> Starting "SYS"."TSPITR_EXP_Bndc":
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   EXPDP> Master table "SYS"."TSPITR_EXP_Bndc" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_Bndc is:
   EXPDP>   /home/oracle/aux/tspitr_Bndc_82023.dmp
   EXPDP> ******************************************************************************
   EXPDP> Datafiles required for transportable tablespace TEA:
   EXPDP>   /mnt/ramdisk/book/tea01.dbf
   EXPDP> Job "SYS"."TSPITR_EXP_Bndc" successfully completed at Tue Jun 27 09:09:53 2017 elapsed 0 00:00:35
Export completed

contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
}
executing Memory Script
contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
}
executing Memory Script

database closed
database dismounted
Oracle instance shut down

sql statement: drop tablespace  TEA including contents keep datafiles cascade constraints

Performing import of metadata...
   IMPDP> Master table "SYS"."TSPITR_IMP_Bndc" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_Bndc":
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   IMPDP> Job "SYS"."TSPITR_IMP_Bndc" successfully completed at Tue Jun 27 09:10:12 2017 elapsed 0 00:00:03
Import completed

contents of Memory Script:
{
# make read write and offline the imported tablespaces
sql 'alter tablespace  TEA read write';
sql 'alter tablespace  TEA offline';
# enable autobackups after TSPITR is finished
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script

sql statement: alter tablespace  TEA read write

sql statement: alter tablespace  TEA offline

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

Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_temp_do3d1rfz_.tmp deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_3_do3d1r3y_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_2_do3d1qz3_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_1_do3d1qr3_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_do3d16tf_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_do3d16sz_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_system_do3d16td_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/controlfile/o1_mf_do3d10mf_.ctl deleted
Finished recover at 2017-06-27 09:10:15

RMAN>

--//整个过程就是通过建立辅组实例,恢复部分文件到的特定时间点或者scn,然后利用传输表空间导出源数据.然后
--//有1个sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
--//删除表空间.copy对应数据文件
--//倒回源数据到数据库.

5.测试是否恢复正常.
--//注意结束时表空间是offline的.保险你可以read only看看.
SCOTT@book> alter tablespace tea online  ;
Tablespace altered.

SCOTT@book> alter tablespace tea  read only ;
Tablespace altered.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       84762

SCOTT@book> select count(*) from emp2 ;
    COUNT(*)
------------
          14

SCOTT@book> select count(*) from dept2 ;
    COUNT(*)
------------
           4

--//你可以发现完全恢复了误操作的内容.我一直认为这样操作不是太合理,如果表空间很大,恢复工作量也很大,
--//12c 增加了 recover table会更加好一些.

[20170627]使用TSPITR恢复表空间.txt的更多相关文章

  1. RMAN数据库恢复之恢复表空间和数据文件

    执行表空间或数据文件恢复时,数据库既可以是MOUNT状态,也可以是OPEN状态.1.恢复表空间在执行恢复之前,如果被操作的表空间未处理OFFLINE状态,必须首先通过ALTER TABLESPACE… ...

  2. (Les16 执行数据库恢复)-表空间恢复

    NOARCHIVELOG模式下丢失了数据文件     数据库处于NOARCHIVELOG模式时,如果丢失任何数据文件,执行以下步骤         1.如果实例尚未关闭,请关闭实例         2 ...

  3. 表空间基于时间点的恢复(TSPITR)

    环境:RHEL 6.4 + Oracle 11.2.0.4 准备模拟环境 1. 验证表空间的依赖性 2. 确定执行TSPITR后会丢失的对象 3. 自动执行TSPITR Reference 准备模拟环 ...

  4. Oracle DB 执行表空间时间点恢复

    • 列出在执行表空间时间点恢复(TSPITR) 时会发生的操作 • 阐释TSPITR 使用的术语的定义 • 确定适合将TSPITR 用作解决方案的情况 • 确定时间点恢复的正确目标时间 • 确定不能使 ...

  5. RMAN备份与恢复之表空间

    通过以下事例来说明表空间的恢复,删除表空间内的数据文件,删除后在针对位于该表空间的表进行插入记录以及实施检查点进程 SQL> select a.tablespace_name from dba_ ...

  6. ORACLE表空间管理维护

    1:表空间概念 在ORACLE数据库中,所有数据从逻辑结构上看都是存放在表空间当中,当然表空间下还有段.区.块等逻辑结构.从物理结构上看是放在数据文件中.一个表空间可由多个数据文件组成. 如下图所示, ...

  7. 【基础】Oracle 表空间和数据文件

    多个表空间的优势:1.能够将数据字典与用户数据分离出来,避免由于字典对象和用户对象保存在同一个数据文件中而产生的I/O冲突2.能够将回退数据与用户数据分离出来,避免由于硬盘损坏而导致永久性的数据丢失3 ...

  8. ORACLE表空间

    在ORACLE数据库中,所有数据从逻辑结构上看都是存放在表空间当中,当然表空间下还有段.区.块等逻辑结构.从物理结构上看是放在数据文件中.一个表空间可由多个数据文件组成. 如下图所示,一个数据库由对应 ...

  9. oracle表空间创建及管理

    一.数据文件和数据库逻辑存储结构: 一个表空间包含一个或多个数据文件,一个表空间包含一个或多个段,一个段包含一个或多个区,一个区包含一个或多个连续的数据库块,一个数据库块包含一个或多个操作系统块.段是 ...

随机推荐

  1. VS2015 Git 源码管理工具简单入门

    1.VS Git插件 1.1 环境 VS2015+GitLab 1.2 Git操作过程图解 1.3 常见名词解释 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Meger) 获取 ...

  2. C++11新特性 lambda表达式

    C++11 添加了了一个名为lambda表达式的功能,可以用于添加匿名函数 语法: [capture_block](parameter) mutable exception_specification ...

  3. Android编译报Errors running builder &#39;Android Pre Compiler&#39; on project &#39;XXX&#39; java.lang.NullPointerException

    编译android时,遇到报错:Errors occurred during the build.Errors running builder 'Android Pre Compiler' on pr ...

  4. 大数据之nutch

    一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...

  5. 昨天在公司加班,上午好像就是弄一个ftp的linux服务问题

    在网上找了一些方法,可是其中有通过匿名方式登陆,但是在root的权限下才能存放文件,可是把匿名用户登陆取消之后又不能登陆,就是没有列出怎么来添加一个ftp的用户,今天打算直接装一个linux系统在虚拟 ...

  6. 利用HTML5的一个重要特性 —— DeviceOrientation来实现手机网站上的摇一摇功能

      介绍之前做两个声明: 以下代码可以直接运行,当然你别忘了引用jQuery才行. <script> // DeviceOrientation将底层的方向传感器和运动传感器进行了高级封装, ...

  7. 每日英语:A Whiff Of &#39;Welcome Home&#39;

    Buying real estate involves weighing a lot of factors: location, price . . . smell? Some condo devel ...

  8. IIS:错误: 无法提交配置更改,因为文件已在磁盘上更改

    文件名: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config 错误: 无法提交配置更改,因为文件已在磁盘上更改 通过 Micro ...

  9. WeQuant交易策略—RSI

    RSI指标策略 策略介绍 RSI(相对强弱指标),是通过一段时期内的平均收盘上涨和下跌数,计算价格上涨所产生的波动占整个波动的百分比,来分析市场买卖盘的意向和实力. 计算公式(以日为单位举例) RSI ...

  10. JVM调优:HotSpot JVM垃圾收集器

    HotSpot JVM垃圾收集器 - Snooper - 博客园https://www.cnblogs.com/snooper/p/8718478.html