Oracle 19c 特殊恢复——使用BBED推数据文件SCN

使用BBED推数据文件SCN

数据库环境Oracle 19.3.0.0.0

一、SCN与CHECKPOINT

与checkpoint相关的SCN号有四个,其中三个存在控制文件中,一个存放在数据文件头中。
这四个分别是:
1.System Checkpoint SCN
当checkpoint完成后,ORACLE将System Checkpoint SCN号存放在控制文件中。
我们可以通过下面SQL语句查询:

select checkpoint_change# from v$database;

2.Datafile Checkpoint SCN
当checkpoint完成后,ORACLE将Datafile Checkpoint SCN号存放在控制文件中。
我们可以通过下面SQL语句查询所有数据文件的Datafile Checkpoinnt SCN号。

select name,checkpoint_change# fromv$datafile;

3.Start SCN号
ORACLE将Start SCN号存放在数据文件头中。
这个SCN用于检查数据库启动过程是否需要做media recovery.
我们可以通过以下SQL语句查询:

select name,checkpoint_change# fromv$datafile_header;

4.End SCN号
ORACLE将End SCN号存放在控制文件中。
这个SCN号用于检查数据库启动过程是否需要做instance recovery.
我们可以通过以下SQL语句查询:

select name,last_change# from v$datafile;

在数据库正常运行的情况下,对可读写的,online的数据文件,该SCN号为NULL.

二、SCN号与数据库启动、关闭

在数据库启动过程中,当System Checkpoint SCN、Datafile Checkpoint SCN和Start SCN号都相同时,数据库可以正常启动,不需要做media recovery.三者当中有一个不同时,则需要做media recovery.
ORACLE在启动过程中首先检查是否需要media recovery,然后再检查是否需要instance recovery.

如果数据库的正常关闭的话,将会触发一个checkpoint,同时将数据文件的END SCN号设置为相应数据文件的Start SCN号。当数据库启动时,发现它们是一致的,则不需要做instance recovery。在数据库正常启动后,ORACLE会将END SCN号设置为NULL.
如果数据库异常关闭的话,则END SCN号将为NULL.则需要做instance recovery.

三、上传bbed相关文件,重新编译bbed

bbed 工具分32位和64位,并且从11g开始不提供bbed,故需要从10g的库找下面4个文件,上传到19c环境重新编译。

ls /home/oracle/bbed/bbed
bbedus.msb  bbedus.msg  sbbdpt.o  ssbbded.o
cp s*  /u01/app/oracle/product/19.9.0/dbhome_1/rdbms/lib/
cp bbedus.ms* /u01/app/oracle/product/19.9.0/dbhome_1/rdbms/mesg/
make -f  /u01/app/oracle/product/19.9.0/dbhome_1/rdbms/lib/ins_rdbms.mk BBED=$ORACLE_HOME/bin/bbed $ORACLE_HOME/bin/bbed

四、BBED初识

bbed 默认密码为:blockedit,可以编辑参数文件把密码、BLOCKSIZE 、FILENAME 等进去,省的 后面设置,也可以手动一项一项设置。

[oracle@test1 ~]$ bbed 
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Sun Feb 14 17:23:30 2021

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> info all;
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------

BBED> help all;
SET DBA [ dba | file#, block# ]
SET FILENAME 'filename'
SET FILE file#
SET BLOCK [+/-]block#
SET OFFSET [ [+/-]byte offset | symbol | *symbol ]
SET BLOCKSIZE bytes
SET LIST[FILE] 'filename'
SET WIDTH character_count
SET COUNT bytes_to_display
SET IBASE [ HEX | OCT | DEC ]
SET OBASE [ HEX | OCT | DEC ]
SET MODE  [ BROWSE | EDIT ]
SET SPOOL [ Y | N ]
SHOW [ <SET parameter> | ALL ]
INFO
MAP[/v] [ DBA | FILENAME | FILE | BLOCK ]
DUMP[/v] [ DBA | FILENAME | FILE | BLOCK | OFFSET | COUNT ]
PRINT[/x|d|u|o|c] [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
EXAMINE[/Nuf] [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
</Nuf>:
N - a number which specifies a repeat count.
u - a letter which specifies a unit size:
  b - b1, ub1 (byte)
  h - b2, ub2 (half-word)
  w - b4, ub4(word)
  r - Oracle table/index row
f - a letter which specifies a display format:
  x - hexadecimal
  d - decimal
  u - unsigned decimal
  o - octal
  c - character (native)
  n - Oracle number
  t - Oracle date
  i - Oracle rowid
FIND[/x|d|u|o|c] numeric/character string [ TOP | CURR ]
COPY [ DBA | FILE | FILENAME | BLOCK ] TO [ DBA | FILE | FILENAME | BLOCK ]
MODIFY[/x|d|u|o|c] numeric/character string
      [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
ASSIGN[/x|d|u|o] <target spec>=<source spec>
<target spec> : [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
<source spec> : [ value | <target spec options> ]
SUM [ DBA | FILE | FILENAME | BLOCK ] [ APPLY ]
PUSH [ DBA | FILE | FILENAME | BLOCK | OFFSET ]
POP [ALL]
REVERT [ DBA | FILE | FILENAME | BLOCK ]
UNDO
HELP [ <bbed command> | ALL ]
VERIFY [ DBA | FILE | FILENAME | BLOCK ]
CORRUPT [ DBA | FILE | FILENAME | BLOCK ]

BBED> 

五、环境准备

目前还不知道怎么使用bbed读取asm磁盘组中的数据文件,所以需要文件在文件系统中,asm磁盘组中的数据文件需要copy出来,修改好后,再copy回去,用rman还是cp都可以。



SQL> create tablespace test datafile '/home/oracle/test001.dbf' size 10m autoextend on next 1m;

Tablespace created.


SQL>  create table t1 tablespace test as select rownum n ,'wen' a from dual connect by rownum<20;

Table created.

SQL> select * from t1;

	 N A
---------- ---
	 1 wen
	 2 wen
	 3 wen
	 4 wen
	 5 wen
	 6 wen
	 7 wen
	 8 wen
	 9 wen
	10 wen
	11 wen
	12 wen
	13 wen
	14 wen
	15 wen
	16 wen
	17 wen
	18 wen
	19 wen

19 rows selected.

SQL>  SELECT a.FILE#, a.NAME,a.CHECKPOINT_CHANGE#,a.LAST_CHANGE#,status FROM v$datafile a;

     FILE# NAME 					      CHECKPOINT_CHANGE# LAST_CHANGE# STATUS
---------- ----------------------------- ------------------ ------------ -------
	 1 +DG1/TEST/system01.dbf					 2307067	      SYSTEM
	 2 /home/oracle/test001.dbf					 2307067	      ONLINE
	 3 +DG1/TEST/sysaux01.dbf					 2307067	      ONLINE
	 4 +DG1/TEST/undotbs01.dbf					 2307067	      ONLINE
	 5 +DG1/TEST/undotbs02.dbf					 2307067	      ONLINE
	 7 +DG1/TEST/users01.dbf					 2307067	      ONLINE

六、实验

  1. 创建好表空间、表后,关闭数据库,备份文件/home/oracle/test001.dbf为test001.dbf.0
  2. 开启数据库,删除部分数据
SQL> delete from t1 where n<17;

16 rows deleted.

SQL> commit;

Commit complete.

SQL> select * from t1;

	 N A
---------- ---
	17 wen
	18 wen
	19 wen
  1. 切换几次归档,关闭数据库。
  2. 将/home/oracle/test001.dbf备份为test001.dbf.1
  3. 将test001.dbf.0 重命名为test001.dbf,这样启动时候文件头scn就会不一致,需要恢复
SQL> select CHECKPOINT_CHANGE# from v$datafile_header;

CHECKPOINT_CHANGE#
------------------
	   2810204
	   2405924   ---file  /home/oracle/test001.dbf
	   2810204
	   2810204
	   2810204
	   2810204

6 rows selected.

SQL> SELECT a.FILE#, a.NAME,a.CHECKPOINT_CHANGE#,a.LAST_CHANGE#,status FROM v$datafile a;

     FILE# NAME 					      CHECKPOINT_CHANGE# LAST_CHANGE# STATUS
---------- ----------------------- ------------------ ------------ -------
	 1 +DG1/TEST/system01.dbf					 2810204      2810204 SYSTEM
	 2 /home/oracle/test001.dbf					 2810204      2810204 ONLINE
	 3 +DG1/TEST/sysaux01.dbf					 2810204      2810204 ONLINE
	 4 +DG1/TEST/undotbs01.dbf					 2810204      2810204 ONLINE
	 5 +DG1/TEST/undotbs02.dbf					 2810204      2810204 ONLINE
	 7 +DG1/TEST/users01.dbf					 2810204      2810204 ONLINE

6 rows selected.

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 2 needs media recovery
ORA-01110: data file 2: '/home/oracle/test001.dbf'

  1. 使用BBED修改文件头scn
 
BBED> info 
 File#  Name                                                        Size(blks)
 -----  ----                                                        ----------
     1  /home//oracle/test001.dbf                                            0
     2  /tmp/USERS.260.1064444539                                            0

BBED> p kcvfhckp
struct kcvfhckp, 36 bytes                   @484     ## @ 后面是偏移量
   struct kcvcpscn, 8 bytes                 @484     
      ub4 kscnbas                           @484      0x0024b624
      ub2 kscnwrp                           @488      0x8000
      ub2 kscnwrp2                          @490      0x0000
   ub4 kcvcptim                             @492      0x3f72dc0e
   ub2 kcvcpthr                             @496      0x0001
   union u, 12 bytes                        @500     
      struct kcvcprba, 12 bytes             @500     
         ub4 kcrbaseq                       @500      0x00000007
         ub4 kcrbabno                       @504      0x0000108e
         ub2 kcrbabof                       @508      0x0010
   ub1 kcvcpetb[0]                          @512      0x06
   ub1 kcvcpetb[1]                          @513      0x00
   ub1 kcvcpetb[2]                          @514      0x00
   ub1 kcvcpetb[3]                          @515      0x00
   ub1 kcvcpetb[4]                          @516      0x00
   ub1 kcvcpetb[5]                          @517      0x00
   ub1 kcvcpetb[6]                          @518      0x00
   ub1 kcvcpetb[7]                          @519      0x00


BBED> d /v dba 1,1 offset 484 count 16                                                             
 File: /home//oracle/test001.dbf (1)
 Block: 1       Offsets:  484 to  499  Dba:0x00400001
-------------------------------------------------------
 24b62400 00800000 0edc723f 01000000 l $¶$......۲?....
 

2405924的16进制为:24b624
2810204的16进制为:2ae15c
注意操作系统的大小端。Linux下字节序,存储是反的,这个困惑了我好久

BBED> modify /x 5ce12a  dba 1,1 offset 484
 File: /home//oracle/test001.dbf (1)
 Block: 1                Offsets:  484 to  499           Dba:0x00400001
------------------------------------------------------------------------
 5ce12a00 00800000 0edc723f 01000000 

 <32 bytes per line>

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




SQL> r
  1* select CHECKPOINT_CHANGE# from v$datafile_header

CHECKPOINT_CHANGE#
------------------
	   2810204
	   2810204
	   2810204
	   2810204
	   2810204
	   2810204

6 rows selected

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-01113: file 2 needs media recovery
ORA-01110: data file 2: '/home/oracle/test001.dbf'


SQL> alter database recover;

Database altered.

SQL> alter database open;

Database altered.

SQL> select * from t1;

	 N A
---------- ---
	 1 wen
	 2 wen
	 3 wen
	 4 wen
	 5 wen
	 6 wen
	 7 wen
	 8 wen
	 9 wen
	10 wen
	11 wen

	 N A
---------- ---
	12 wen
	13 wen
	14 wen
	15 wen
	16 wen
	17 wen
	18 wen
	19 wen

19 rows selected.

七、总结

可以看到数据已经恢复到了关库冷备时候的数据,后面的删除语句没有生效,已经通过修改文件头的scn,跳过了删除据的redo,也说明这种方法可能会使数据不一致,适用于历史表空间数据,其他情况谨慎使用。

你可能感兴趣的:(Oracle,bbed,oracle)