ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 自动表空间时间点恢复实验过程

自动表空间时间点恢复实验过程

原创 Linux操作系统 作者:红叶DBA 时间:2011-02-27 16:44:46 0 删除 编辑
自动表空间时间点恢复

一、在试验前,先要备份数据库。

[oracle@RAC1 catalog]$ rman

Recovery Manager: Release 10.2.0.1.0 - Production on Wed Feb 9 12:24:16 2011

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

RMAN> connect target /

connected to target database: HONGYE2 (DBID=860418810)

RMAN> list backup;

RMAN> backup database plus archivelog delete input;

Starting backup at 09-FEB-11
。。。。。。(输出省略)
Finished backup at 09-FEB-11

RMAN> list backup summary;

List of Backups
===============
Key     TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag
------- -- -- - ----------- --------------- ------- ------- ---------- ---
9       B  A  A DISK        09-FEB-11       1       1       NO         TAG20110209T122707
10      B  F  A DISK        09-FEB-11       1       1       NO         TAG20110209T122710
11      B  A  A DISK        09-FEB-11       1       1       NO         TAG20110209T122757
12      B  F  A DISK        09-FEB-11       1       1       NO         TAG20110209T122800

RMAN> exit


Recovery Manager complete.

二、构建测试数据,本次实验中,truncate表t,如果恢复成功,则t中的数据会找回。

[oracle@RAC1 catalog]$ sqlplus

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Feb 9 12:28:50 2011

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

IDEL > conn hongye/hongye
Connected.
HONGYE:146@HONGYE2 > select * from t;

no rows selected

HONGYE:146@HONGYE2 > insert into t select rownum id,'name'||rownum name from dba_objects where rownum<10;

9 rows created.

HONGYE:146@HONGYE2 > commit;

Commit complete.

HONGYE:146@HONGYE2 > select * from t;

        ID NAME
---------- ----------
         1 name1
         2 name2
         3 name3
         4 name4
         5 name5
         6 name6
         7 name7
         8 name8
         9 name9

9 rows selected.

HONGYE:146@HONGYE2 > select dbms_flashback.get_system_change_number from dual;

GET_SYSTEM_CHANGE_NUMBER
------------------------
                  384394

HONGYE:146@HONGYE2 > truncate table t;

Table truncated.

HONGYE:146@HONGYE2 > select * from t;

no rows selected

HONGYE:146@HONGYE2 > exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

三、执行表空间的时间点恢复,命令很简单,此处简要分析一下命令的输出,可以和手动恢复的操作做个对比,加深理解。

[oracle@RAC1 catalog]$ rman

Recovery Manager: Release 10.2.0.1.0 - Production on Wed Feb 9 12:30:06 2011

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

RMAN> connect target /

connected to target database: HONGYE2 (DBID=860418810)

注意在恢复目录中有足够的剩余空间。
RMAN> recover tablespace users until scn 384394 auxiliary destination '/oracle/rmantest/';

Starting recover at 09-FEB-11
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=142 devtype=DISK

List of tablespaces expected to have UNDO segments
tablespace SYSTEM
tablespace UNDOTBS1

1、自动创建辅助实例,此处辅助实例名为wrcF
Creating automatic instance, with SID='wrcF'

辅助实例的相关参数:特别注意到,此处的db_name与目标数据库是一致的,而且指定了不同的db_unique_name,这一点是非常重要的。
initialization parameters used for automatic instance:
db_name=HONGYE2
compatible=10.2.0.1.0
db_block_size=8192
db_files=200
db_unique_name=tspitr_HONGYE2_wrcF
large_pool_size=1M
shared_pool_size=110M
#No auxiliary parameter file used
db_create_file_dest=/oracle/rmantest/
control_files=/oracle/rmantest//cntrl_tspitr_HONGYE2_wrcF.f

启动辅助实例(nomount状态):
starting up automatic instance HONGYE2

Oracle instance started

Total System Global Area     201326592 bytes

Fixed Size                     1218508 bytes
Variable Size                146802740 bytes
Database Buffers              50331648 bytes
Redo Buffers                   2973696 bytes
Automatic instance created

2、执行下面的脚本,此脚本的主要操作是恢复辅助数据库的控制文件,此处的sql 'alter system archive log current' 命令也是比较重要的,因为我们对表所做的修改可能还没有归档,这样在恢复辅助实例时,就有可能无法恢复到指定的时间点(SCN),但是如果恢复时间点与现在时间比较遥远,确定已经归档过了,则此操作可以省略。

contents of Memory Script.:
{
# set the until clause
set until  scn 384394;
# 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-FEB-11
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=37 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 /oracle/flash_recovery_area/HONGYE2/autobackup/2011_02_09/o1_mf_s_742652880_6o45yk0m_.bkp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/oracle/flash_recovery_area/HONGYE2/autobackup/2011_02_09/o1_mf_s_742652880_6o45yk0m_.bkp tag=TAG20110209T122800
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output filename=/oracle/rmantest/cntrl_tspitr_HONGYE2_wrcF.f
Finished restore at 09-FEB-11

sql statement: alter database mount clone database

sql statement: alter system archive log current

sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end;
released channel: ORA_DISK_1
released channel: ORA_AUX_DISK_1

3、将待恢复的表空间脱机(离线),

contents of Memory Script.:
{
# generated tablespace point-in-time recovery script
# set the until clause
set until  scn 384394;
plsql <<<-- tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := 'alter tablespace '||  'USERS' ||' offline for recover';
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;

4、执行辅助数据库的还原操作(restore)。

# 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 a destination filename for restore
set newname for datafile  4 to
"/oracle/oradata/hongye2/users01.dbf";
# rename all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set plus the auxilliary tablespaces
restore clone datafile  1, 2, 4;
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  4 online";

5、还原结束后,恢复辅助数据库(recover),然后以resetlogs方式打开数据库。

# make the controlfile point at the restored datafiles, then recover them
recover clone database tablespace  "USERS", "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 USERS offline for recover

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed temporary file 1 to /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 09-FEB-11
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=39 devtype=DISK

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 /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_system_%u_.dbf
restoring datafile 00002 to /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_undotbs1_%u_.dbf
restoring datafile 00004 to /oracle/oradata/hongye2/users01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /oracle/flash_recovery_area/HONGYE2/backupset/2011_02_09/o1_mf_nnndf_TAG20110209T122710_6o45wz7l_.bkp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/oracle/flash_recovery_area/HONGYE2/backupset/2011_02_09/o1_mf_nnndf_TAG20110209T122710_6o45wz7l_.bkp tag=TAG20110209T122710
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 09-FEB-11

datafile 1 switched to datafile copy
input datafile copy recid=4 stamp=742653158 filename=/oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_system_6o4662qo_.dbf
datafile 2 switched to datafile copy
input datafile copy recid=5 stamp=742653158 filename=/oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_undotbs1_6o4662r5_.dbf

sql statement: alter database datafile  1 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  4 online

Starting recover at 09-FEB-11
using channel ORA_AUX_DISK_1

starting media recovery

archive log thread 1 sequence 12 is already on disk as file /oracle/flash_recovery_area/HONGYE2/archivelog/2011_02_09/o1_mf_1_12_6o463zt3_.arc
channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=11
channel ORA_AUX_DISK_1: reading from backup piece /oracle/flash_recovery_area/HONGYE2/backupset/2011_02_09/o1_mf_annnn_TAG20110209T122757_6o45ygly_.bkp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/oracle/flash_recovery_area/HONGYE2/backupset/2011_02_09/o1_mf_annnn_TAG20110209T122757_6o45ygly_.bkp tag=TAG20110209T122757
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
archive log filename=/oracle/product/10.2.0/db_1/dbs/arch1_11_742402857.dbf thread=1 sequence=11
channel clone_default: deleting archive log(s)
archive log filename=/oracle/product/10.2.0/db_1/dbs/arch1_11_742402857.dbf recid=12 stamp=742653162
archive log filename=/oracle/flash_recovery_area/HONGYE2/archivelog/2011_02_09/o1_mf_1_12_6o463zt3_.arc thread=1 sequence=12
media recovery complete, elapsed time: 00:00:03
Finished recover at 09-FEB-11

database opened

6、导出辅助数据库中和待恢复表空间相关的元数据。

contents of Memory Script.:
{
# export the tablespaces in the recovery set
host 'exp userid =\"/@\(DESCRIPTION=\(ADDRESS=\(PROTOCOL=beq\)\(PROGRAM=/oracle/product/10.2.0/db_1/bin/oracle\)\(ARGV0=oraclewrcF\)\(ARGS=^'\(DESCRIPTION=\(LOCAL=YES\)\(ADDRESS=\(PROTOCOL=beq\)\)\)^'\)\(ENVS=^'ORACLE_SID=wrcF^'\)\)\(CONNECT_DATA=\(SID=wrcF\)\)\) as sysdba\" point_in_time_recover=y tablespaces=
USERS file=
tspitr_a.dmp';

7、在导入到目标数据库之前,必须要先关闭辅助数据库:

# shutdown clone before import
shutdown clone immediate

8、将待恢复表空间的元数据导入到目标数据库中,然后就可以使得待恢复表空间联机了。

# 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  USERS online";
sql "alter tablespace  USERS 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.1.0 - Production on Wed Feb 9 12:33:06 2011

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

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

About to export Tablespace Point-in-time Recovery objects...
For tablespace USERS ...
. exporting cluster definitions
. exporting table definitions
. . exporting table                              T
EXP-00091: Exporting questionable statistics.
. exporting referential integrity constraints
. exporting triggers
. end point-in-time recovery
Export terminated successfully with warnings.
host command complete

database closed
database dismounted
Oracle instance shut down


Import: Release 10.2.0.1.0 - Production on Wed Feb 9 12:33:22 2011

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


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining 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 WE8ISO8859P1 character set (possible charset conversion)
. importing SYS's objects into SYS
. importing HONGYE's objects into HONGYE
. . importing table                            "T"
. importing SYS's objects into SYS
Import terminated successfully without warnings.
host command complete

sql statement: alter tablespace  USERS online

sql statement: alter tablespace  USERS offline

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

8、删除辅助数据库相关的文件,则此次的恢复成功。

Removing automatic instance
Automatic instance removed
auxiliary instance file /oracle/rmantest/cntrl_tspitr_HONGYE2_wrcF.f deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_system_6o4662qo_.dbf deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_undotbs1_6o4662r5_.dbf deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/datafile/o1_mf_temp_6o467qng_.tmp deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/onlinelog/o1_mf_1_6o467ggc_.log deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/onlinelog/o1_mf_2_6o467j2f_.log deleted
auxiliary instance file /oracle/rmantest/TSPITR_HONGYE2_WRCF/onlinelog/o1_mf_3_6o467kg2_.log deleted
Finished recover at 09-FEB-11

RMAN> exit


Recovery Manager complete.

四、结果的验收:

[oracle@RAC1 catalog]$ sqlplus

SQL*Plus: Release 10.2.0.1.0 - Production on Wed Feb 9 12:34:29 2011

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

IDEL > conn hongye/hongye
Connected.
HONGYE:142@HONGYE2 > select tablespace_name,status from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
UNDOTBS1                       ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          OFFLINE

HONGYE:142@HONGYE2 > alter tablespace users online;

Tablespace altered.

HONGYE:142@HONGYE2 > select * from t;

        ID NAME
---------- ----------
         1 name1
         2 name2
         3 name3
         4 name4
         5 name5
         6 name6
         7 name7
         8 name8
         9 name9

9 rows selected.

HONGYE:142@HONGYE2 >


总结:其实手动恢复的过程就是执行以上输出部分的脚本内容而已!

手动恢复实验过程:http://www.itpub.net/thread-1400696-1-1.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24465008/viewspace-688033/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-08-19

  • 博文量
    54
  • 访问量
    70896