从Oracle 10g之后,小规模数据误删除、误操作其实已经有了很好的解决方案,就是flashback技术。Flashback Query和Flashback Drop,就可以从数据记录和数据表这类比较细的粒度。
在实际业务场景中,相对比“天塌地陷”的全库恢复场景,单Schema、单Tablespace甚至单数据表的局部恢复更加有施展的空间。出现误操作的时候,用户往往希望一个或者部分数据表恢复到过去的一个时间点。同时又不希望将全库恢复到过去时间,丢失部分数据。
RMAN提供了一种实现所谓TSPITR(Tablespace Point-In-Time Recovery)的技术,通过简单的一个语句,就可以在主库不停库(很吸引人)的情况下,利用备份集和连续的归档日志,实现表空间级别的定点恢复。
本篇主要介绍进行RMAN TSPITR操作的步骤方法和一些技术细节点,供需要的朋友待查。
1、环境背景介绍
笔者选择Oracle 11g进行试验,具体版本为11.2.0.4版本。数据库模式为归档模式。
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Next log sequence to archive 8
Current log sequence 8
尝试构建一个全新schema,在一个全新的tablespace中。实验数据选择scott用户的数据。
SQL> create tablespace testtbl datafile size 10m autoextend on extent management local uniform size 1m segment space management auto;
Tablespace created
SQL> create user test identified by test default tablespace testtbl;
User created
SQL> grant resource, connect to test;
Grant succeeded
使用之前导出的scott数据,进行remap导入。
[oracle@SimpleLinux ~]$ impdp \"/ as sysdba\" dumpfile=scott_20150618.dmp remap_schema=scott:test remap_tablespace=users:testtbl
Import: Release 11.2.0.4.0 - Production on Thu Jun 18 09:50:17 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options
Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
(篇幅原因,有省略……)
Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/REF_CONSTRAINT
Processing object type SCHEMA_EXPORT/POST_SCHEMA/PROCACT_SCHEMA
Job "SYS"."SYS_IMPORT_FULL_01" completed with 1 error(s) at Thu Jun 18 09:50:28 2015 elapsed 0 00:00:08
SQL> select owner ,tablespace_name, count(*) from dba_segments where owner='TEST' group by owner, tablespace_name;
OWNER TABLESPACE_NAME COUNT(*)
------------------------------ ------------------------------ ----------
TEST TESTTBL 5
两个数据表。
SQL> select count(*) from test.emp;
COUNT(*)
----------
14
SQL> select count(*) from test.dept;
COUNT(*)
----------
4
2、数据备份和故障发生
归档模式下,需要有一份完全的备份,才能实现恢复操作。
RMAN> backup database plus archivelog delete input;
Starting backup at 18-JUN-15
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=47 device type=DISK
(篇幅原因,有省略…….)
Starting Control File and SPFILE Autobackup at 18-JUN-15
piece handle=/u01/app/fast_recovery_area/ORA11G/autobackup/2015_06_18/o1_mf_s_882701042_br4d3m3c_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 18-JUN-15
SQL> select group#, sequence# from v$log where status='CURRENT';
GROUP# SEQUENCE#
---------- ----------
2 17
此时,操作时间为:
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') a from dual;
A
--------------------
2015-06-18 10:51:19
过了几个小时,误操作发生。
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') a from dual;
A
--------------------
2015-06-18 13:47:29
SQL> select group#, sequence# from v$log where status='CURRENT';
GROUP# SEQUENCE#
---------- ----------
3 19
误删除发生
SQL> drop table test.emp;
Table dropped
SQL> drop table test.dept;
Table dropped
--Flashback Drop的机会也没有了。
SQL> purge dba_recyclebin;
Done
3、RMAN进行表空间TSPITR
单表表空间移动,要确保表空间内容“内包”,也就是不会有其他表空间牵扯其中。这个操作我们在可移动表空间的演示中也会进行。
SQL> exec dbms_tts.transport_set_check('testtbl',true);
PL/SQL procedure successfully completed
SQL> select * from transport_set_violations;
VIOLATIONS
--------------------------------------------------------------------------------
下面进行实际恢复操作,首先需要在目录中创建出一个可访问的空间,进行临时文件容纳。
[root@SimpleLinux ~]# cd /
[root@SimpleLinux /]# mkdir -p /extend/oradata/aux
[root@SimpleLinux /]# chown -R oracle:oinstall /extend/
[root@SimpleLinux /]# ls -l | grep extend
drwxr-xr-x. 3 oracle oinstall 4096 Jun 18 10:21 extend