[20161012]数据文件offline马上执行recover

[20161012]数据文件offline马上执行recover.txt

--前几天看的1篇文章,提到数据文件offline,应该养成随手执行recover习惯.保证下一次online时,不需要恢复。
--如果offline很久,忘记online了,而归档日志已经不存在,该如何跳过应用日志,online数据文件呢?
--以前也做过一些测试,再重复测试看看。(注意:这样恢复存在数据丢失的风险)

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

CREATE TABLESPACE SUGAR DATAFILE
  '/mnt/ramdisk/book/sugar01.dbf' SIZE 100M AUTOEXTEND ON NEXT 64M MAXSIZE UNLIMITED
NOLOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

alter system archive log current ;
alter system archive log current ;
alter system archive log current ;

RMAN> delete archivelog all ;

2.建立测试数据:
SCOTT@book> create table t tablespace sugar as select rownum id ,'AAAA' name from dual connect by level<=4;
Table created.

SCOTT@book> select rowid,t.* from t;
ROWID                        ID NAME
------------------ ------------ ------
AAAVp2AAGAAAACDAAA            1 AAAA
AAAVp2AAGAAAACDAAB            2 AAAA
AAAVp2AAGAAAACDAAC            3 AAAA
AAAVp2AAGAAAACDAAD            4 AAAA

SCOTT@book> @ &r/rowid AAAVp2AAGAAAACDAAA
      OBJECT         FILE        BLOCK          ROW ROWID_DBA            DBA                  TEXT
------------ ------------ ------------ ------------ -------------------- -------------------- ----------------------------------------
       88694            6          131            0  0x1800083           6,131                alter system dump datafile 6 block 131 ;

SCOTT@book> @ &r/logfile
GROUP# THREAD# SEQUENCE#    BYTES BLOCKSIZE MEMBERS ARC STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME           GROUP# STATUS     TYPE       MEMBER                       IS_
------ ------- --------- -------- --------- ------- --- ---------- ------------- ------------------- ------------ ------------------- ------ ---------- ---------- ---------------------------- ---
     1       1        36 52428800       512       1 YES INACTIVE         1730954 2016-10-12 08:57:01      1730962 2016-10-12 08:57:04      1            ONLINE     /mnt/ramdisk/book/redo01.log NO
     2       1        35 52428800       512       1 YES INACTIVE         1730948 2016-10-12 08:57:00      1730954 2016-10-12 08:57:01      2            ONLINE     /mnt/ramdisk/book/redo02.log NO
     3       1        37 52428800       512       1 NO  CURRENT          1730962 2016-10-12 08:57:04 2.814750E+14                          3            ONLINE     /mnt/ramdisk/book/redo03.log NO
--当前seq#=37.

--打开session1:
SCOTT@book> update t set name='BBBB' where id=2;
1 row updated.

--打开session2,执行:
SYS@book> alter system checkpoint ;
System altered.

alter system archive log current ;
alter system archive log current ;
alter system archive log current ;

--session1:
SCOTT@book> update t set name='CCCC' where id=3;
1 row updated.

--session2:
SYS@book> alter database datafile 6 offline ;
Database altered.

SCOTT@book> @ &r/logfile ;
GROUP# THREAD# SEQUENCE#       BYTES BLOCKSIZE MEMBERS ARC STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME           GROUP# STATUS     TYPE       MEMBER                                             IS_
------ ------- --------- ----------- --------- ------- --- ---------- ------------- ------------------- ------------ ------------------- ------ ---------- ---------- -------------------------------------------------- ---
     1       1        39    52428800       512       1 YES INACTIVE         1731312 2016-10-12 09:04:03      1731316 2016-10-12 09:04:06      1            ONLINE     /mnt/ramdisk/book/redo01.log                       NO
     2       1        38    52428800       512       1 YES INACTIVE         1731308 2016-10-12 09:04:02      1731312 2016-10-12 09:04:03      2            ONLINE     /mnt/ramdisk/book/redo02.log                       NO
     3       1        40    52428800       512       1 NO  CURRENT          1731316 2016-10-12 09:04:06 2.814750E+14                          3            ONLINE     /mnt/ramdisk/book/redo03.log                       NO
----当前seq#=40.

--打开session2:
SCOTT@book> commit ;
Commit complete.

alter system archive log current ;
alter system archive log current ;
alter system archive log current ;

3.现在假想seq#=40的归档丢弃了。看看如何恢复。

$ cd /u01/app/oracle/archivelog/book
$ mkdir backup
$ mv 1_40_896605872.dbf backup/
$ ls -l backup/
total 102528
-rw-r----- 1 oracle oinstall     13824 2016-10-12 09:07:34 1_40_896605872.dbf

4.开始测试恢复:
--先做1个备份拷贝:
$ cp /mnt/ramdisk/book/sugar01.dbf backup/

SCOTT@book> alter database datafile 6 online ;
alter database datafile 6 online
*
ERROR at line 1:
ORA-01113: file 6 needs media recovery
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'

SCOTT@book> recover datafile 6;
ORA-00279: change 1731316 generated at 10/12/2016 09:04:06 needed for thread 1
ORA-00289: suggestion : /u01/app/oracle/archivelog/book/1_40_896605872.dbf
ORA-00280: change 1731316 for thread 1 is in sequence #40
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
ORA-00308: cannot open archived log '/u01/app/oracle/archivelog/book/1_40_896605872.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3

ORA-00308: cannot open archived log '/u01/app/oracle/archivelog/book/1_40_896605872.dbf'
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3

--由于丢失seq#=40的归档日志,恢复无法进行。
SCOTT@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS     CHECKPOINT_COUNT FUZ NAME                             TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ---------- ---------------- --- -------------------------------- ---------------
    1            1731413 2016-10-12 09:07:35                7            925702 ONLINE                  140 YES /mnt/ramdisk/book/system01.dbf   SYSTEM
    2            1731413 2016-10-12 09:07:35             1834            925702 ONLINE                  138 YES /mnt/ramdisk/book/sysaux01.dbf   SYSAUX
    3            1731413 2016-10-12 09:07:35           923328            925702 ONLINE                   59 YES /mnt/ramdisk/book/undotbs01.dbf  UNDOTBS1
    4            1731413 2016-10-12 09:07:35            16143            925702 ONLINE                  137 YES /mnt/ramdisk/book/users01.dbf    USERS
    5            1731413 2016-10-12 09:07:35           952916            925702 ONLINE                   57 YES /mnt/ramdisk/book/example01.dbf  EXAMPLE
    6            1731316 2016-10-12 09:04:06          1730665            925702 OFFLINE                   7 YES /mnt/ramdisk/book/sugar01.dbf    SUGAR
6 rows selected.

SCOTT@book> select recid,name,sequence#,first_change#,next_change# from v$archived_log where sequence#>=37;
RECID NAME                                               SEQUENCE# FIRST_CHANGE# NEXT_CHANGE#
------ -------------------------------------------------- --------- ------------- ------------
    35 /u01/app/oracle/archivelog/book/1_37_896605872.dbf        37       1730962      1731308
    36 /u01/app/oracle/archivelog/book/1_38_896605872.dbf        38       1731308      1731312
    37 /u01/app/oracle/archivelog/book/1_39_896605872.dbf        39       1731312      1731316
    38 /u01/app/oracle/archivelog/book/1_40_896605872.dbf        40       1731316      1731405
    39 /u01/app/oracle/archivelog/book/1_41_896605872.dbf        41       1731405      1731409
    40 /u01/app/oracle/archivelog/book/1_42_896605872.dbf        42       1731409      1731413
6 rows selected.

--seq#=40已经不存在,也就是要跳过归档40.从scn=1731405开始恢复。实际上其他不需要修改,至少我测试这个版本不需要修改。

4.通过bbed修改文件头:
BBED> p /d kcvfh.kcvfhckp.kcvcpscn
struct kcvcpscn, 8 bytes                    @484
   ub4 kscnbas                              @484      1731316
   ub2 kscnwrp                              @488      0

BBED> assign kcvfh.kcvfhckp.kcvcpscn.kscnbas=1731405
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y
ub4 kscnbas                                 @484      0x001a6b4d

BBED> p /d kcvfh.kcvfhckp.kcvcpscn
struct kcvcpscn, 8 bytes                    @484
   ub4 kscnbas                              @484      1731405
   ub2 kscnwrp                              @488      0

BBED> sum apply
Check value for File 6, Block 1:
current = 0x6656, required = 0x6656

SCOTT@book> recover datafile 6;
Media recovery complete.

SCOTT@book> alter database datafile 6 online ;
Database altered.

SCOTT@book> select rowid,t.* from t;
ROWID               ID NAME
------------------ --- -----
AAAVp2AAGAAAACDAAA   1 AAAA
AAAVp2AAGAAAACDAAB   2 BBBB
AAAVp2AAGAAAACDAAC   3 AAAA
AAAVp2AAGAAAACDAAD   4 AAAA

--你可以发现这样恢复id=3的修改丢失。

5.好了,现在重新再来恢复一次。这次能找到seq#=40.

SCOTT@book> alter database datafile 6 offline ;
Database altered.

$ cd /u01/app/oracle/archivelog/book
$ mv backup/1_40_896605872.dbf .
$ cp backup/sugar01.dbf /mnt/ramdisk/book

SCOTT@book> alter database datafile 6 online ;
alter database datafile 6 online
*
ERROR at line 1:
ORA-01113: file 6 needs media recovery
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'

SCOTT@book> recover datafile 6;
ORA-00283: recovery session canceled due to errors
ORA-00600: internal error code, arguments: [3020], [6], [131], [25165955], [], [], [], [], [], [], [], []
ORA-10567: Redo is inconsistent with data block (file# 6, block# 131, file offset is 1073152 bytes)
ORA-10564: tablespace SUGAR
ORA-01110: data file 6: '/mnt/ramdisk/book/sugar01.dbf'
ORA-10561: block type 'TRANSACTION MANAGED DATA BLOCK', data object# 88694

--出现了不一致的情况。如果使用bbed检查数据块,发现:

BBED> set dba 6,131
        DBA             0x01800083 (25165955 6,131)

BBED> x /rnc *kdbr[2]
rowdata[11]                                 @8155
-----------
flag@8155: 0x2c (KDRHFL, KDRHFF, KDRHFH)
lock@8156: 0x02
cols@8157:    2

col    0[2] @8158: 3
col    1[4] @8161: CCCC

SYS@book> SELECT file#, CHECKPOINT_CHANGE#, CHECKPOINT_TIME,CREATION_CHANGE#  , RESETLOGS_CHANGE#,status, CHECKPOINT_COUNT,fuzzy,name,tablespace_name  FROM v$datafile_header;
FILE# CHECKPOINT_CHANGE# CHECKPOINT_TIME     CREATION_CHANGE# RESETLOGS_CHANGE# STATUS  CHECKPOINT_COUNT FUZ NAME                            TABLESPACE_NAME
----- ------------------ ------------------- ---------------- ----------------- ------- ---------------- --- ------------------------------- ----------------
    1            1732543 2016-10-12 09:35:57                7            925702 ONLINE               142 YES /mnt/ramdisk/book/system01.dbf  SYSTEM
    2            1732543 2016-10-12 09:35:57             1834            925702 ONLINE               140 YES /mnt/ramdisk/book/sysaux01.dbf  SYSAUX
    3            1732543 2016-10-12 09:35:57           923328            925702 ONLINE                61 YES /mnt/ramdisk/book/undotbs01.dbf UNDOTBS1
    4            1732543 2016-10-12 09:35:57            16143            925702 ONLINE               139 YES /mnt/ramdisk/book/users01.dbf   USERS
    5            1732543 2016-10-12 09:35:57           952916            925702 ONLINE                59 YES /mnt/ramdisk/book/example01.dbf EXAMPLE
    6            1731413 2016-10-12 09:07:35          1730665            925702 OFFLINE                7 NO  /mnt/ramdisk/book/sugar01.dbf   SUGAR
6 rows selected.

SYS@book> select recid,name,sequence#,first_change#,next_change# from v$archived_log where sequence#>=37;
RECID NAME                                                  SEQUENCE# FIRST_CHANGE# NEXT_CHANGE#
------ -------------------------------------------------- ------------ ------------- ------------
    35 /u01/app/oracle/archivelog/book/1_37_896605872.dbf           37       1730962      1731308
    36 /u01/app/oracle/archivelog/book/1_38_896605872.dbf           38       1731308      1731312
    37 /u01/app/oracle/archivelog/book/1_39_896605872.dbf           39       1731312      1731316
    38 /u01/app/oracle/archivelog/book/1_40_896605872.dbf           40       1731316      1731405
    39 /u01/app/oracle/archivelog/book/1_41_896605872.dbf           41       1731405      1731409
    40 /u01/app/oracle/archivelog/book/1_42_896605872.dbf           42       1731409      1731413
6 rows selected.
--seq#=42已经应用完成。scn=1731413.

SYS@book> @ &r/logfile
GROUP# THREAD# SEQUENCE#       BYTES BLOCKSIZE MEMBERS ARC STATUS     FIRST_CHANGE# FIRST_TIME          NEXT_CHANGE# NEXT_TIME           GROUP# STATUS     TYPE       MEMBER                                             IS_
------ ------- --------- ----------- --------- ------- --- ---------- ------------- ------------------- ------------ ------------------- ------ ---------- ---------- -------------------------------------------------- ---
     1       1        42    52428800       512       1 YES INACTIVE         1731409 2016-10-12 09:07:34      1731413 2016-10-12 09:07:35      1            ONLINE     /mnt/ramdisk/book/redo01.log                       NO
     2       1        41    52428800       512       1 YES INACTIVE         1731405 2016-10-12 09:07:34      1731409 2016-10-12 09:07:34      2            ONLINE     /mnt/ramdisk/book/redo02.log                       NO
     3       1        43    52428800       512       1 NO  CURRENT          1731413 2016-10-12 09:07:35 2.814750E+14                          3            ONLINE     /mnt/ramdisk/book/redo03.log                       NO

--不知道如何修复,再增加scn看看。

BBED> set dba 6,1
        DBA             0x01800001 (25165825 6,1)

BBED> p /d kcvfh.kcvfhckp.kcvcpscn
struct kcvcpscn, 8 bytes                    @484
   ub4 kscnbas                              @484      1731413
   ub2 kscnwrp                              @488      0

BBED> assign kcvfh.kcvfhckp.kcvcpscn.kscnbas=1732543
Warning: contents of previous BIFILE will be lost. Proceed? (Y/N) y
ub4 kscnbas                                 @484      0x001a6fbf

BBED> p /d kcvfh.kcvfhckp.kcvcpscn
struct kcvcpscn, 8 bytes                    @484
   ub4 kscnbas                              @484      1732543
   ub2 kscnwrp                              @488      0

BBED> sum apply
Check value for File 6, Block 1:
current = 0x89a9, required = 0x89a9

SYS@book> recover datafile 6;
Media recovery complete.

SYS@book> alter database datafile 6 online ;
Database altered.

SYS@book> select rowid,t.* from scott.t;
ROWID               ID NAME
------------------ --- ------
AAAVp2AAGAAAACDAAA   1 AAAA
AAAVp2AAGAAAACDAAB   2 BBBB
AAAVp2AAGAAAACDAAC   3 CCCC
AAAVp2AAGAAAACDAAD   4 AAAA

--总结:
数据文件offline,最好随手执行1次recover。避免时间久了,下次online时,归档已经不存在的情况。
先发一个alter system checkpoint,也许也是一个好习惯。

或者如果仅仅表空间仅仅1个数据文件,可以offline表空间,这样表空间online时不需要恢复。

时间: 2024-10-22 14:47:17

[20161012]数据文件offline马上执行recover的相关文章

[20161019]数据文件offline后恢复到那个scn

[20161019]数据文件offline后恢复到那个scn号.txt --前一天别人问的问题,如果数据文件offline时,online要恢复,一般恢复到scn是多少,是offline时的scn吗? --总不见得如果长时间offline,要应用许多归档日志吧,通过测试说明问题: 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ----------------------------

20160331数据文件offline与open resetlogs2

[20160331]数据文件offline与open resetlogs3.txt --接上面的测试.链接: --关机做一个冷备份,便于重复测试.取出冷备份,重复测试: --做一些必要的清理清除归档. 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- ----------------------

[20161019]数据文件offline与open resetlog

[20161019]数据文件offline与open resetlog.txt --上午做了数据文件offline后恢复到那个scn号,恢复到该数据文件的LAST_CHANGE#的scn值. --如果数据文件做了offline,在以后数据库做了open resetlog后,如何online呢?如果归档存在没有问题, --但是10g开始支持跨resetlog的恢复.自己以前也做了类似的测试. 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING            

[20151028]理解数据文件offline+drop.txt

[20151028]理解数据文件offline+drop.txt --前几天做删除数据文件的恢复测试,自己在理解offline drop的方式存在错误,做一个记录: The ALTER DATABASE DATAFILE <datafile name> OFFLINE DROP command, is not meant to allow you to remove a datafile. What the command really means is that you are offlin

临时数据文件 offline 对于导入导出的影响

临时数据文件 offline 对于导入导出的影响 sys@ORACL> alter database tempfile 'd:\oracle\oradata\oracl\temp01.dbf' offline; 数据库已更改. sys@ORACL> ================================================tempfile offline的情况. 1 导出少量数据时,没有报错,当导出大量数据时,会报EXP-00068: 表空间 TEMP 脱机 C:\Use

数据文件offline后unusable索引造成的问题

最近在做一个oracle入库速度测试时,想到将最近一个小时的索引放到内存中(表是按小时分区)是不是会提升入库的速度,索引的维护对io是一个不小的开销: 不过这个方案如果要使用的话数据库必须是 oracle 12c,因为在当前小时结束后,需要将相关索引移出内存,让下一个小时的索引留在内存,这样内存的使用情况基本是一个定量: 而在移动的过程中不能对业务有影响,这样需要用到12c的新功能,在线移动数据文件. 测试的结果是入库速度有很明显的提升,入库速度是之前的几倍,但这个不是本文的重点:本文的重点是在

[20160329]bbed修复offline的数据文件.txt

[20160329]bbed修复offline的数据文件.txt --测试数据库,不小心将一个数据文件offline了,archivelog也删除了(主要磁盘空间紧张,做了一次整理). --自己测试修复看看,顺便做一个记录. 1.环境: SCOTT@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- --------

数据文件、表空间offline用法及区别

对数据库的脱机包括数据文件的脱机和对表空间的脱机,表空间脱机实际就是表空间对应的所有数据文件脱机. 1.         数据文件OFFLINE 数据文件添加到表空间之后不能够被删除的,没有语法支持这么做,如果想不使用该数据文件,唯一是将数据文件设置为OFFLINE状态.执行以下步骤将数据文件设置为OFFLINE状态: 1)         如果是归档模式可以执行如下SQL设置数据文件的状态为OFFLINE: ALTER DATABASE DATAFILE 'XXXX.DBF' OFFLINE;

迁移数据文件到ASM【转】

1.迁移数据文件到ASM 1)数据库一致性情况下迁移: 将数据库启动到mount状态,生成rman copy 语句,然后在rman中执行: SQL> startup mount SQL> select q'(copy datafile ')'||name||q'(' to '+oradata';)' from v$datafile; Q'(COPYDATAFILE')'||NAME||Q'('TO'+ORADATA';)' -----------------------------------