从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
启动RMAN,登录后执行recover tablespace操作,指定出恢复时间。注意:RMAN的recover tablespace命令本质上是执行一系列的指令脚本,将利用RMAN恢复过程自动化,无需人工介入。
[oracle@SimpleLinux ~]$ rman nocatalog
Recovery Manager: Release 11.2.0.4.0 - Production on Thu Jun 18 13:48:44 2015
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
RMAN> connect target /
connected to target database: ORA11G (DBID=4239941846)
using target database control file instead of recovery catalog
RMAN> recover tablespace testtbl until time "to_date('2015-06-18 10:51:19','yyyy-mm-dd hh24:mi:ss')" auxiliary destination '/extend/oradata/aux';
Starting recover at 18-JUN-15
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=44 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='DDfe' –在目录中创建出一个虚拟的Oracle实例
--启动实例的参数都准备好了。
initialization parameters used for automatic instance:
db_name=ORA11G
db_unique_name=DDfe_tspitr_ORA11G
compatible=11.2.0.4.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
db_create_file_dest=/extend/oradata/aux
log_archive_dest_1='location=/extend/oradata/aux'
#No auxiliary parameter file used
starting up automatic instance ORA11G
Oracle instance started
Total System Global Area 1071333376 bytes
Fixed Size 1369420 bytes
Variable Size 281021108 bytes
Database Buffers 784334848 bytes
Redo Buffers 4608000 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 time "to_date('2015-06-18 10:51:19','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
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 18-JUN-15
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=19 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/fast_recovery_area/ORA11G/autobackup/2015_06_18/o1_mf_s_882701042_br4d3m3c_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/autobackup/2015_06_18/o1_mf_s_882701042_br4d3m3c_.bkp tag=TAG20150618T104402
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
output file name=/extend/oradata/aux/ORA11G/controlfile/o1_mf_br4q1v2o_.ctl
Finished restore at 18-JUN-15
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 time "to_date('2015-06-18 10:51:19','yyyy-mm-dd hh24:mi:ss')";
plsql ;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile 1 to new;
set newname for clone datafile 5 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 7 to
"/u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile 1, 5, 3, 2, 7;
switch clone datafile all;
}
executing Memory Script
executing command: SET until clause
sql statement: alter tablespace TESTTBL offline immediate
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
renamed tempfile 1 to /extend/oradata/aux/ORA11G/datafile/o1_mf_temp_%u_.tmp in control file
Starting restore at 18-JUN-15
using channel ORA_AUX_DISK_1
--只恢复system, sysaux, undo和目标testtbl表空间。
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 /extend/oradata/aux/ORA11G/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00005 to /extend/oradata/aux/ORA11G/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00002 to /extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00007 to /u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_nnndf_TAG20150618T104315_br4d23ty_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_nnndf_TAG20150618T104315_br4d23ty_.bkp tag=TAG20150618T104315
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:05
Finished restore at 18-JUN-15
datafile 1 switched to datafile copy
input datafile copy RECID=5 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q22sy_.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=6 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q230g_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=7 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_br4q22z4_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=8 STAMP=882712324 file name=/extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_br4q22yx_.dbf
--启动虚拟实例数据,完成恢复。
contents of Memory Script:
{
# set requested point in time
set until time "to_date('2015-06-18 10:51:19','yyyy-mm-dd hh24:mi:ss')";
# online the datafiles restored or switched
sql clone "alter database datafile 1 online";
sql clone "alter database datafile 5 online";
sql clone "alter database datafile 3 online";
sql clone "alter database datafile 2 online";
sql clone "alter database datafile 7 online";
# recover and open resetlogs
recover clone database tablespace "TESTTBL", "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 5 online
sql statement: alter database datafile 3 online
sql statement: alter database datafile 2 online
sql statement: alter database datafile 7 online
Starting recover at 18-JUN-15
using channel ORA_AUX_DISK_1
starting media recovery
archived log for thread 1 with sequence 17 is already on disk as file /u01/app/fast_recovery_area/ORA11G/archivelog/2015_06_18/o1_mf_1_17_br4pw26f_.arc
channel ORA_AUX_DISK_1: starting archived log restore to default destination
channel ORA_AUX_DISK_1: restoring archived log
archived log thread=1 sequence=16
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_annnn_TAG20150618T104401_br4d3k9f_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/fast_recovery_area/ORA11G/backupset/2015_06_18/o1_mf_annnn_TAG20150618T104401_br4d3k9f_.bkp tag=TAG20150618T104401
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/extend/oradata/aux/1_16_846081044.dbf thread=1 sequence=16
channel clone_default: deleting archived log(s)
archived log file name=/extend/oradata/aux/1_16_846081044.dbf RECID=27 STAMP=882712328
archived log file name=/u01/app/fast_recovery_area/ORA11G/archivelog/2015_06_18/o1_mf_1_17_br4pw26f_.arc thread=1 sequence=17
media recovery complete, elapsed time: 00:00:01
Finished recover at 18-JUN-15
database opened
--将表空间导出来
contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone 'alter tablespace TESTTBL read only';
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/extend/oradata/aux''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/extend/oradata/aux''";
}
executing Memory Script
sql statement: alter tablespace TESTTBL read only
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/extend/oradata/aux''
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/extend/oradata/aux''
Performing export of metadata...
EXPDP> Starting "SYS"."TSPITR_EXP_DDfe":
EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX/INDEX
EXPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT
EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
EXPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/REF_CONSTRAINT
EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
EXPDP> Master table "SYS"."TSPITR_EXP_DDfe" successfully loaded/unloaded
EXPDP> ******************************************************************************
EXPDP> Dump file set for SYS.TSPITR_EXP_DDfe is:
EXPDP> /extend/oradata/aux/tspitr_DDfe_54026.dmp
EXPDP> ******************************************************************************
EXPDP> Datafiles required for transportable tablespace TESTTBL:
EXPDP> /u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf
EXPDP> Job "SYS"."TSPITR_EXP_DDfe" successfully completed at Thu Jun 18 13:54:10 2015 elapsed 0 00:01:13
Export completed
--关闭虚拟实例
contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace TESTTBL including contents keep datafiles cascade constraints';
}
executing Memory Script
database closed
database dismounted
Oracle instance shut down
--把原来的表空间删除掉
sql statement: drop tablespace TESTTBL including contents keep datafiles cascade constraints
--恢复导入数据
Performing import of metadata...
IMPDP> Master table "SYS"."TSPITR_IMP_DDfe" successfully loaded/unloaded
IMPDP> Starting "SYS"."TSPITR_IMP_DDfe":
IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX/INDEX
IMPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/CONSTRAINT
IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
IMPDP> Processing object type TRANSPORTABLE_EXPORT/CONSTRAINT/REF_CONSTRAINT
IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
IMPDP> Job "SYS"."TSPITR_IMP_DDfe" successfully completed at Thu Jun 18 13:54:54 2015 elapsed 0 00:00:10
Import completed
--第三部分脚本,恢复表空间
contents of Memory Script:
{
# make read write and offline the imported tablespaces
sql 'alter tablespace TESTTBL read write';
sql 'alter tablespace TESTTBL offline';
# enable autobackups after TSPITR is finished
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script
sql statement: alter tablespace TESTTBL read write
sql statement: alter tablespace TESTTBL offline
sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;
Removing automatic instance
Automatic instance removed
auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_temp_br4q4ojl_.tmp deleted
auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_4_br4q4j17_.log deleted
auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_3_br4q4ghl_.log deleted
auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_2_br4q4dt5_.log deleted
auxiliary instance file /extend/oradata/aux/ORA11G/onlinelog/o1_mf_1_br4q4brh_.log deleted
auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_sysaux_br4q22yx_.dbf deleted
auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_undotbs1_br4q22z4_.dbf deleted
auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q230g_.dbf deleted
auxiliary instance file /extend/oradata/aux/ORA11G/datafile/o1_mf_system_br4q22sy_.dbf deleted
auxiliary instance file /extend/oradata/aux/ORA11G/controlfile/o1_mf_br4q1v2o_.ctl deleted
Finished recover at 18-JUN-15
RMAN>
上面的步骤可以归纳为如下:
ü 在目录中创建一个虚拟临时数据库实例;
ü RMAN自动辅助库AUX创建,利用源数据库的备份集合和归档日志进行全库不完全恢复。恢复范围包括system、sysaux、undo和目标表空间;
ü 借助Expdp和Impdp,使用可移动表空间原理步骤,将目标表空间数据导出并导回源数据库;
ü 恢复完成;
下面可以看实际效果。
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') a from dual;
A
--------------------
2015-06-18 13:55:43
SQL> select group#, sequence# from v$log where status='CURRENT';
GROUP# SEQUENCE#
---------- ----------
1 20
尝试访问数据表。
SQL> select count(*) from test.emp;
select count(*) from test.emp
ORA-00376: 无法读取数据文件 7
ORA-01110: 数据文件 7: '/u01/app/oradata/ORA11G/datafile/o1_mf_testtbl_br48h8wp_.dbf'
此时,表空间被offline,需要手工进行online操作。
SQL> select tablespace_name, status from dba_tablespaces;
TABLESPACE_NAME STATUS
------------------------------ ---------
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
TESTTBL OFFLINE
6 rows selected
SQL> alter tablespace testtbl online;
Tablespace altered
SQL> select tablespace_name, status from dba_tablespaces;
TABLESPACE_NAME STATUS
------------------------------ ---------
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
TESTTBL ONLINE
6 rows selected
SQL> select count(*) from test.emp;
COUNT(*)
----------
14
SQL> select count(*) from test.dept;
COUNT(*)
----------
4
5、结论
Oracle RMAN的TSPITR在实际中还是很有用处的,特别是当我们没有办法使用Flashback技术(如已经发生了Undo覆盖)的时候。Tablespace的粒度还是比较大,希望以后可以实现Schema或者数据表table级别的的不完全恢复。
: