Oracle 异机恢复 - 仅仅恢复业务表空间

-- 场景
数据库中单独的一个表被delete或者truncate。排除可能进行闪回、日志挖掘、闪回查询、基于时间点的表空间恢复等方法,在11g上只能进行异机恢复。然后抽出数据,导入到生产库。在12c开始,支持RMAN对表的恢复。当库的大小不太大的时候,异机恢复还可以,当库为几个T的时候,为了一个表的数据恢复几个T的数据,时间上比较难以接受。本测试,主要测试,仅仅恢复表所在的表空间,来节约恢复时间。经过测试,能达到目的。
测试环境:RDBMS 11.2.0.4 + redhat7.4
恢复到Windows Server 2008 + RDBMS 11.2.0.4


--测试过程,(在同一台机器上进行恢复测试,所以不涉及数据文件路径、redo log路径转换。异机的时候转换下)
## 对数据库做一次全备
## 删除掉数据库所有的文件
## 恢复SPFILE、控制文件
## 在同一台机器上进行恢复,仅仅恢复SYSTEM、SYSAUX、XTTS等表空间(XTTS为业务表空间)数据文件,SPFILE、控制文件等
## 重新创建控制文件,去除掉控制文件中除过SYSTEM、SYSAUX、XTTS之外的数据文件
## 对数据库进行recover(会产生其他表空间的数据文件,实际路径下并没有该文件)
## open resetlogs 开库
## 导出数据 ,导入数据。

-- 数据库上原有的数据文件

RMAN> report schema;

Report of database schema for database with db_unique_name TESTOGG

List of Permanent Datafiles
===========================
File Size(MB) Tablespace           RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1    800      SYSTEM               ***     /u01/app/oracle/oradata/testogg/system01.dbf
2    590      SYSAUX               ***     /u01/app/oracle/oradata/testogg/sysaux01.dbf
3    1150     UNDOTBS1             ***     /u01/app/oracle/oradata/testogg/undotbs01.dbf
4    18       USERS                ***     /u01/app/oracle/oradata/testogg/users01.dbf
5    346      EXAMPLE              ***     /u01/app/oracle/oradata/testogg/example01.dbf
6    200      GGS_TBS              ***     /u01/app/oracle/oradata/testogg/ggs_tbs01.dbf
7    10       BB_TBS               ***     /u01/app/oracle/oradata/testogg/bb_tbs01.dbf
8    50       TEST_TBS             ***     /u01/app/oracle/oradata/testogg/test_tbs01.dbf
9    100      XTTS                 ***     /u01/app/oracle/oradata/testogg/xtts01.dbf

List of Temporary Files
=======================
File Size(MB) Tablespace           Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1    68       TEMP                 32767       /u01/app/oracle/oradata/testogg/temp01.dbf

RMAN>

-- 启动库到nomount 状态

RMAN> startup nomount force;

Oracle instance started

Total System Global Area     835104768 bytes

Fixed Size                     2257840 bytes
Variable Size                281021520 bytes
Database Buffers             545259520 bytes
Redo Buffers                   6565888 bytes

 

-- 还原spfile,略

-- 还原控制文件

RMAN> restore controlfile from '/backup/control_TESTOGG_20200606_0bv22fdf_1_1';

Starting restore at 06-JUN-20
using channel ORA_DISK_1

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/app/oracle/oradata/testogg/control01.ctl
output file name=/u01/app/oracle/fast_recovery_area/testogg/control02.ctl
Finished restore at 06-JUN-20

RMAN> startup mount;

database is already started
database mounted
released channel: ORA_DISK_1

RMAN>

-- 还原表空间 system,sysaux,undotbs1,xtts的数据文件

RMAN> restore tablespace system,sysaux,undotbs1,XTTS ;

Starting restore at 06-JUN-20
Starting implicit crosscheck backup at 06-JUN-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=10 device type=DISK
Crosschecked 7 objects
Finished implicit crosscheck backup at 06-JUN-20

Starting implicit crosscheck copy at 06-JUN-20
using channel ORA_DISK_1
Crosschecked 1 objects
Finished implicit crosscheck copy at 06-JUN-20

searching for all files in the recovery area
cataloging files...
no files cataloged

using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00003 to /u01/app/oracle/oradata/testogg/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00009 to /u01/app/oracle/oradata/testogg/xtts01.dbf
channel ORA_DISK_1: reading from backup piece /backup/fulldb_TESTOGG_20200606_05v22f8r_1_1
channel ORA_DISK_1: piece handle=/backup/fulldb_TESTOGG_20200606_05v22f8r_1_1 tag=TAG20200606T100203
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:08
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00001 to /u01/app/oracle/oradata/testogg/system01.dbf
channel ORA_DISK_1: restoring datafile 00002 to /u01/app/oracle/oradata/testogg/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /backup/fulldb_TESTOGG_20200606_04v22f8r_1_1
channel ORA_DISK_1: piece handle=/backup/fulldb_TESTOGG_20200606_04v22f8r_1_1 tag=TAG20200606T100203
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 06-JUN-20

RMAN>

-- 直接对表空间进行recover,是不可以的 。

RMAN> recover tablespace system,sysaux,undotbs1,XTTS ;

Starting recover at 06-JUN-20
using channel ORA_DISK_1

RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 06/06/2020 10:19:41
RMAN-06067: RECOVER DATABASE required with a backup or created control file

RMAN>

-- 如果对库进行recover的话,会提示其他数据文件不存在。比如提示4号文件,USERS表空间文件是不存在的。(所以需要做的事情,就是重建控制文件。排除这些文件)

RMAN> recover database;

Starting recover at 06-JUN-20
using channel ORA_DISK_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 06/06/2020 10:23:13
RMAN-06094: datafile 4 must be restored

--无论是drop 表空间,还是offline表空间,都不可以

-- 重新创建控制文件,其他业务表空间的数据文件不需要。前提是system表空间文件要在(这个时候,已经把system、sysaux、xtts表空间的数据文件已经还原出来)

SYS@testogg>startup nomount
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size                  2257840 bytes
Variable Size             281021520 bytes
Database Buffers          545259520 bytes
Redo Buffers                6565888 bytes
SYS@testogg>CREATE CONTROLFILE REUSE DATABASE "TESTOGG" RESETLOGS FORCE LOGGING ARCHIVELOG
      2  MAXLOGFILES 16
    M  3  AXLOGMEMBERS 3
   4     MAXDATAFILES 100
  5      MAXINSTANCES 8
  6      MAXLOGHISTORY 292
LOG  7  FILE
  G  8  ROUP 1 '/u01/app/oracle/oradata/testogg/redo01.log'  SIZE 50M BLOCKSIZE 512,
  9    GROUP 2 '/u01/app/oracle/oradata/testogg/redo02.log'  SIZE 50M BLOCKSIZE 512,
  GRO 10  UP 3 '/u01/app/oracle/oradata/testogg/redo03.log'  SIZE 50M BLOCKSIZE 512
-- ST 11  ANDBY LOGFILE
DAT 12  AFILE
 13    '/u01/app/oracle/oradata/testogg/system01.dbf',
   14  '/u01/app/oracle/oradata/testogg/sysaux01.dbf',
 15    '/u01/app/oracle/oradata/testogg/undotbs01.dbf',
   16  '/u01/app/oracle/oradata/testogg/xtts01.dbf'
CHA 17  RACTER SET AL32UTF8
 18  ;

Control file created.

SYS@testogg>

-- catalog 备份文件

RMAN> catalog start with '/backup/';

using target database control file instead of recovery catalog
searching for all files that match the pattern /backup/

List of Files Unknown to the Database
=====================================
File Name: /backup/fulldb_TESTOGG_20200606_07v22fa2_1_1
File Name: /backup/fulldb_TESTOGG_20200606_06v22f9u_1_1
File Name: /backup/arc_TESTOGG_20200606_0av22fde_1_1
File Name: /backup/fulldb_TESTOGG_20200606_04v22f8r_1_1
File Name: /backup/arc_TESTOGG_20200606_09v22fbn_1_1
File Name: /backup/arc_TESTOGG_20200606_08v22fbn_1_1
File Name: /backup/control_TESTOGG_20200606_0bv22fdf_1_1
File Name: /backup/spfile_TESTOGG_20200606_0cv22fdh_1_1
File Name: /backup/fulldb_TESTOGG_20200606_05v22f8r_1_1
File Name: /backup/xtts/full/XTTS_9.tf
File Name: /backup/xtts/inc/02v1lmv6_1_1
File Name: /backup/xtts/inc/03v1lof9_1_1

Do you really want to catalog the above files (enter YES or NO)? yes
cataloging files...
cataloging done

List of Cataloged Files
=======================
File Name: /backup/fulldb_TESTOGG_20200606_07v22fa2_1_1
File Name: /backup/fulldb_TESTOGG_20200606_06v22f9u_1_1
File Name: /backup/arc_TESTOGG_20200606_0av22fde_1_1
File Name: /backup/fulldb_TESTOGG_20200606_04v22f8r_1_1
File Name: /backup/arc_TESTOGG_20200606_09v22fbn_1_1
File Name: /backup/arc_TESTOGG_20200606_08v22fbn_1_1
File Name: /backup/control_TESTOGG_20200606_0bv22fdf_1_1
File Name: /backup/spfile_TESTOGG_20200606_0cv22fdh_1_1
File Name: /backup/fulldb_TESTOGG_20200606_05v22f8r_1_1
File Name: /backup/xtts/full/XTTS_9.tf
File Name: /backup/xtts/inc/02v1lmv6_1_1
File Name: /backup/xtts/inc/03v1lof9_1_1

RMAN>

--recover数据库

RMAN> recover database;

Starting recover at 06-JUN-20
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=10 device type=DISK

starting media recovery

channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=84
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=85
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=86
channel ORA_DISK_1: reading from backup piece /backup/arc_TESTOGG_20200606_09v22fbn_1_1
channel ORA_DISK_1: piece handle=/backup/arc_TESTOGG_20200606_09v22fbn_1_1 tag=TAG20200606T100334
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:45
archived log file name=/archive_log/1_84_1037791425.dbf thread=1 sequence=84
archived log file name=/archive_log/1_85_1037791425.dbf thread=1 sequence=85
archived log file name=/archive_log/1_86_1037791425.dbf thread=1 sequence=86
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=87
channel ORA_DISK_1: reading from backup piece /backup/arc_TESTOGG_20200606_0av22fde_1_1
channel ORA_DISK_1: piece handle=/backup/arc_TESTOGG_20200606_0av22fde_1_1 tag=TAG20200606T100334
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/archive_log/1_87_1037791425.dbf thread=1 sequence=87
unable to find archived log
archived log thread=1 sequence=88
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 06/06/2020 10:39:19
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 88 and starting SCN of 1493690

RMAN>

-- Open restlogs 打开数据库

RMAN> alter database open resetlogs;

database opened

RMAN>

-- recover完毕,RMAN下report schema 。发现其他的表空间数据文件是存在的。大小是0.文件名称是MISSING0000X。

RMAN> report schema;

Report of database schema for database with db_unique_name TESTOGG

List of Permanent Datafiles
===========================
File Size(MB) Tablespace           RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1    800      SYSTEM               ***     /u01/app/oracle/oradata/testogg/system01.dbf
2    590      SYSAUX               ***     /u01/app/oracle/oradata/testogg/sysaux01.dbf
3    1150     UNDOTBS1             ***     /u01/app/oracle/oradata/testogg/undotbs01.dbf
4    0        USERS                ***     /u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00004
5    0        EXAMPLE              ***     /u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00005
6    0        GGS_TBS              ***     /u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006
7    0        BB_TBS               ***     /u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00007
8    0        TEST_TBS             ***     /u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00008
9    100      XTTS                 ***     /u01/app/oracle/oradata/testogg/xtts01.dbf

RMAN>

-- 查看表空间信息。 其他表空间还是有的。

SYS@testogg>select tablespace_name from dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
EXAMPLE
GGS_TBS
BB_TBS
TEST_TBS
XTTS

10 rows selected.

SYS@testogg>

SYS@testogg>select file_name from dba_data_files;

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/testogg/xtts01.dbf
/u01/app/oracle/oradata/testogg/undotbs01.dbf
/u01/app/oracle/oradata/testogg/sysaux01.dbf
/u01/app/oracle/oradata/testogg/system01.dbf
/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00004
/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00005
/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006
/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00007
/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00008

9 rows selected.

SYS@testogg>

-- 当进行drop 掉表空间的时候,提示无法drop。

SYS@testogg>drop tablespace users;
drop tablespace users
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 1
ORA-00376: file 6 cannot be read at this time
ORA-01111: name for data file 6 is unknown - rename to correct file
ORA-01110: data file 6:
'/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006'
ORA-06512: at line 1166
ORA-00376: file 6 cannot be read at this time
ORA-01111: name for data file 6 is unknown - rename to correct file
ORA-01110: data file 6:
'/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006'


SYS@testogg>

-- 实际上dbs里面并没有这些文件

[oracle@oggtest backup]$ cd /u01/app/oracle/product/11.2.0/dbhome_1/dbs
[oracle@oggtest dbs]$ ll
total 9836
-rw-rw----. 1 oracle oinstall     1544 Apr 15 10:56 hc_orcl.dat
-rw-rw----. 1 oracle oinstall     1544 Apr 15 11:18 hc_test.dat
-rw-rw----. 1 oracle oinstall     1544 Jun  6 10:40 hc_testogg.dat
-rw-r--r--. 1 oracle oinstall     2851 May 15  2009 init.ora
-rw-r-----. 1 oracle oinstall       24 Apr 15 11:23 lkTESTOGG
-rw-r-----. 1 oracle oinstall     1536 Apr 15 14:37 orapwtestogg
-rw-r-----  1 oracle oinstall 10043392 Jun  6 10:04 snapcf_testogg.f
-rw-r-----. 1 oracle oinstall     3584 Jun  6 10:41 spfiletestogg.ora
[oracle@oggtest dbs]$

-- 测试下,启动关闭库 ,启动关闭正常。

SYS@testogg>shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@testogg>startup
ORACLE instance started.

Total System Global Area  835104768 bytes
Fixed Size                  2257840 bytes
Variable Size             281021520 bytes
Database Buffers          545259520 bytes
Redo Buffers                6565888 bytes
Database mounted.
Database opened.
SYS@testogg>

-- 再次重建控制文件。发下这些MISSING0000X文件还是有的 (过程略)

-- 导出数据试试 ,数据在表空间xtts中 (其实到了这一步,库能起来,能看到表。方法就比较多了)

SYS@testogg>conn u_xtts/oracle
Connected.
U_XTTS@testogg>desc t_xtts
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 OWNER                                              VARCHAR2(30)
 OBJECT_NAME                                        VARCHAR2(128)
 SUBOBJECT_NAME                                     VARCHAR2(30)
 OBJECT_ID                                          NUMBER
 DATA_OBJECT_ID                                     NUMBER
 OBJECT_TYPE                                        VARCHAR2(19)
 CREATED                                            DATE
 LAST_DDL_TIME                                      DATE
 TIMESTAMP                                          VARCHAR2(19)
 STATUS                                             VARCHAR2(7)
 TEMPORARY                                          VARCHAR2(1)
 GENERATED                                          VARCHAR2(1)
 SECONDARY                                          VARCHAR2(1)
 NAMESPACE                                          NUMBER
 EDITION_NAME                                       VARCHAR2(30)

U_XTTS@testogg>select count(*) from t_xtts;

  COUNT(*)
----------
    261273

U_XTTS@testogg>
U_XTTS@testogg>select default_tablespace from dba_users where username='U_XTTS';

DEFAULT_TABLESPACE
------------------------------
XTTS

U_XTTS@testogg>

--expdp无法导出表(记得以前用RMAN从Linux恢复到Windows下,使用隐含参数_allow_restlogs_corruption打开库,也是无法使用数据泵,使用exp解决了)

[oracle@oggtest trace]$ expdp 'u_xtts/oracle' directory=UDMP dumpfile=xtts.dmp tables=T_XTTS

Export: Release 11.2.0.4.0 - Production on Sat Jun 6 10:57:16 2020

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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
ORA-31626: job does not exist
ORA-31633: unable to create master table "U_XTTS.SYS_EXPORT_TABLE_05"
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 95
ORA-06512: at "SYS.KUPV$FT", line 1038
ORA-00376: file 6 cannot be read at this time
ORA-01111: name for data file 6 is unknown - rename to correct file
ORA-01110: data file 6: '/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006'
ORA-06512: at line 1166
ORA-00376: file 6 cannot be read at this time
ORA-01111: name for data file 6 is unknown - rename to correct file
ORA-01110: data file 6: '/u01/app/oracle/product/11.2.0/dbhome_1/dbs/MISSING00006'


[oracle@oggtest trace]$

-- 使用exp可以导出数据

[oracle@oggtest trace]$ exp u_xtts/oracle file=/home/oracle/a.dmp tables=t_xtts

Export: Release 11.2.0.4.0 - Production on Sat Jun 6 10:59:47 2020

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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and UTF8 NCHAR character set
server uses AL32UTF8 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table                         T_XTTS     261273 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
Export terminated successfully with warnings.
[oracle@oggtest trace]$

-- 导入测试

PS C:\Users\Administrator> sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on 星期六 6月 6 13:07:58 2020

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

@>conn system/oracle
Connected.

SYSTEM@test>create user u_xtts identified by oracle;

User created.

SYSTEM@test>grant connect,resource to u_xtts;

Grant succeeded.

SYSTEM@test>grant dba to u_xtts;

Grant succeeded.

SYSTEM@test>

PS C:\Users\Administrator> imp u_xtts/oracle file=c:\dump\a.dmp tables=t_xtts

Import: Release 11.2.0.4.0 - Production on 星期六 6月 6 13:12:30 2020

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 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

Export file created by EXPORT:V11.02.00 via conventional path
import done in ZHS16GBK character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
export client uses US7ASCII character set (possible charset conversion)
export server uses UTF8 NCHAR character set (possible ncharset conversion)
. importing U_XTTS's objects into U_XTTS
. importing U_XTTS's objects into U_XTTS
. . importing table                       "T_XTTS"     261273 rows imported
Import terminated successfully without warnings.
PS C:\Users\Administrator>

-- 验证下数据

SYSTEM@test>conn u_xtts/oracle
Connected.
U_XTTS@test>select count(*) from t_xtts;

  COUNT(*)
----------
    261273

U_XTTS@test>delete from t_xtts where object_id=500;

3 rows deleted.

U_XTTS@test>commit;

Commit complete.

U_XTTS@test>select count(*) from t_xtts;

  COUNT(*)
----------
    261270

U_XTTS@test>

END

你可能感兴趣的:(RMAN)