Oracle DG环境中的gap处理办法总结

2023-04-27 13:32:41 浏览数 (1)

概述

当主库的某些日志没有成功传送到备库,那么这时候就发生了归档裂缝(Archive Gap)。目前Oracle提供了两种日志GAP的检测和处理机制,分别是自动GAP处理(Automatic Gap Resolution)和FAL进程GAP处理(FAL Gap Resolution)。自动GAP处理即主库上的ARCn进程会每分钟检查备库上的日志GAP情况并做相应处理。FAL(Fetch Archive Log)是通过配置FAL_SERVER和FAL_CLIENT实现GAP检测的一种机制,它是备库主动发起的“取”日志的过程。备库就是FAL_CLIENT,它从FAL_SERVER中取这些GAP。Oracle会首先尝试使用FAL进程处理GAP,当发现FAL机制并没有配置生效的时候,进而尝试使用自动GAP处理。FAL进程只在物理备库存在。FAL进程提供了一个CLIENT/SERVER的机制,用来解决检测在主库产生的连续的归档日志,而在备库接受的归档日志不连续的问题。该进程只有在需要的时候才会启动,而当工作完成后就关闭了,因此在正常情况下,该进程是无法看见的。

大致可以分为以下几种情况来处理:

1、主库归档日志存在,可以通过配置Fetch Archive Log(FAL)参数,自动解决归档GAP,或将归档日志拷贝到从库手工注册处理。

2、主库归档日志丢失,需要人工干预来修复,可以分为如下几种情况:

① 12c版本之前的常规处理步骤:

1.在主库上创建一个备库的控制文件 2.以备库的当前SCN号为起点,在主库上做一个增量备份 3.将增量备份拷贝到备库上 4.使用新的控制文件将备库启动到mount状态 5.将增量备份注册到RMAN的catalog,取消备库的恢复应用,恢复增量备份 6.开启备库的恢复进程

② 从12c开始RECOVER … FROM SERVICE

③ 从18c开始RECOVER STANDBY DATABASE FROM SERVICE

Oracle随着版本的升级,逐渐将步骤缩减,进行封装,18C之后可谓是达到了所谓的一键刷新,恢复DG同步。

处理GAP方法1:手动拷贝归档到备库进行手工注册处理,归档未丢失

(一)物理DG手动解决GAP

在一些特殊情况下,如果GAP不能自动解决,那么就需要手工执行中断恢复。通过查询视图V$ARCHIVE_GAP可以确定断档的是哪些日志。

代码语言:javascript复制
SQL> SELECT * FROM V$ARCHIVE_GAP;
THREAD#    LOW_SEQUENCE#  HIGH_SEQUENCE#
----------- ------------- --------------
1                     10              12

可以看到,当前物理备库丢失日志文件从线程1的序号10到序号12。接下来确定归档日志文件的路径:(假设在主数据库上的本地归档目的地是LOG_ARCHIVE_DEST_1):

代码语言:javascript复制
SQL> SELECT NAME FROM V$ARCHIVED_LOG WHERE THREAD#=1 AND DEST_ID=1 AND SEQUENCE# BETWEEN 10 AND 12;
NAME
-----------------------------------------------------------------  
/arch/thread1_dest/arch_1_10.arc
/arch/thread1_dest/arch_1_11.arc
/arch/thread1_dest/arch_1_12.arc

接下来,复制这些日志文件到物理备库,然后在物理备库上使用“ALTER DATABASE REGISTER LOGFILE”语句来注册这些归档日志,如下所示:

代码语言:javascript复制
SQL> ALTER DATABASE REGISTER LOGFILE  '/arch/thread1_dest/arch_1_10.arc';

-- 也可以使用如下的SQL语句:
ALTER DATABASE REGISTER OR REPLACE  LOGFILE '/arch/thread1_dest/arch_1_10.arc';
ALTER DATABASE REGISTER OR REPLACE  PHYSICAL LOGFILE '/arch/thread1_dest/arch_1_11.arc';

在物理备库上注册这些日志文件之后,重新打开重做应用进程。如果断档的归档日志较多,那么可以使用如下的SQL语句来生成要执行的SQL语句(如下的SQL也适用于逻辑DG):

代码语言:javascript复制
SELECT 'ALTER DATABASE REGISTER OR REPLACE LOGICAL LOGFILE ''/arch/thread1_dest/arch_1_' || a ||'.arc'';' LOGICAL_DG,
        'ALTER DATABASE REGISTER OR REPLACE  LOGFILE ''/arch/thread1_dest/arch_1_' || a ||'.arc'';' PHYSICAL_DG
  FROM (SELECT LEVEL A FROM DUAL CONNECT BY LEVEL <= 12)
 WHERE A >= 10 ;

需要注意的是,视图V

ARCHIVE_GAP视图来确定下一个中断序号,如果有的话,重复这个过程直到没有更多的中断。

(二)逻辑DG手动解决GAP

在逻辑备库上查询DBA_LOGSTDBY_LOG视图可以确定是否有归档中断。例如,下面的查询指出断档号为16至18:

代码语言:javascript复制
SYS@orclasm > COLUMN FILE_NAME FORMAT a60
SYS@orclasm > SELECT THREAD#, SEQUENCE#, FILE_NAME
  2    FROM DBA_LOGSTDBY_LOG L
  3   WHERE NEXT_CHANGE# NOT IN (SELECT FIRST_CHANGE# FROM DBA_LOGSTDBY_LOG WHERE L.THREAD# = THREAD#)
  4   ORDER BY THREAD#, SEQUENCE#;
THREAD#    SEQUENCE# FILE_NAME  
--------- ---------- ---------------------------------------------  
1                 16 /arch/oracle/arch_1_16.arc
1                 18 /arch/oracle/arch_1_18.arc

接下来复制丢失的日志文件到逻辑备库,并在逻辑备库上使用“ALTER DATABASE REGISTER LOGICAL LOGFILE”来注册这些日志文件。例如:

代码语言:javascript复制
SQL> ALTER DATABASE REGISTER LOGICAL LOGFILE  '/arch/oracle/arch_1_16.arc';

在逻辑备库上注册这些日志文件之后,重启SQL应用。和物理DG一样,在逻辑备库上的DBA_LOGSTDBY_LOG视图只返回当前妨碍SQL应用继续的下一个中断。在解决指定的中断并重启SQL应用之后,再次在逻辑备库上查询DBA_LOGSTDBY_LOG视图,以确定下一个中断序号,如果有的话,重复这个过程直到没有更多的中断。

需要注意的是,如果需要的归档日志已经不在主库上了,但是有归档日志的RMAN备份,那么可以通过RMAN恢复把缺少的归档日志进行还原,如下所示:

代码语言:javascript复制
SET ARCHIVELOG DESTINATION TO '/arch';
RESTORE ARCHIVELOG  FROM LOGSEQ 7;

处理GAP方法2:主库基于SCN的增量备份来恢复DG(常规办法)

如果断档的归档日志已经丢失,且RMAN又没有备份,那么在Oracle 10g之前没有办法修复了,只能重建DG,但是从Oracle 10g开始可以采用主库基于SCN的增量备份来恢复DG,请参考:

https://www.xmmup.com/zaidghuanjingzhongzhukudiushiguidangduizhukujinxingjiyuscndezengliangbeifenlaihuifuwulidghuanjing.html

https://www.xmmup.com/dghuanjingzhukudiushiguidangqingkuangxiashujuwenjiandehuifu.html

https://www.xmmup.com/rachuanjingxiazhukudiushiguidangbeikudgdehuifuguocheng.html

https://www.xmmup.com/wulidgzaizhukudiushiguidangwenjiandeqingkuangxiadehuifu.html

从12c开始:RECOVER … FROM SERVICE

参考:https://www.xmmup.com/zaioracle-12czhongzairmanfangmianyounaxiezengqiangdexintexing.html#wu_tong_guo_wang_luo_yuan_cheng_hui_fu_shu_ju_ku_RestoreRecover_from_Service

从18c开始:RECOVER STANDBY DATABASE FROM SERVICE

Oracle在18c对(RESTORE/RECOVER …FROM SERVICE)功能进行了加强,推出了(RECOVER STANDBY DATABASE … FROM SERVICE)通过网络服务直接增量恢复备库数据,这个新特性也大大简化了DataGuard主备的问题修复。

示例

下面我们通过实验来进行展示:

首先,模拟备库断电,主库切几个最新的归档,然后手工删掉,重新开启DG同步。

代码语言:javascript复制
##备库:
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
shutdown immediate

##主库:
alter system switch logfile;多次切归档

##删除最近几个归档日志:
rm 1_34_1070147137.arc 
rm 1_33_1070147137.arc

##备库:
startup
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

##查看GAP
SQL> SELECT * FROM V$ARCHIVE_GAP;

   THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#
---------- ------------- --------------
         1            32             34

SQL> SELECT max(sequence#) from v$archived_log where applied='YES';

MAX(SEQUENCE#)
--------------
            31

SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;

PROCESS   STATUS          THREAD#  SEQUENCE#     BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH      CLOSING               1         38          1         40
ARCH      CLOSING               1         39          1          2
ARCH      CONNECTED             0          0          0          0
ARCH      CLOSING               1         37          1          2
RFS       IDLE                  0          0          0          0
RFS       IDLE                  0          0          0          0
RFS       IDLE                  1         40         83          1
MRP0      APPLYING_LOG          1         33          1     245760

8 rows selected.

12之前

当前GAP:32---34

1.在主库上创建一个备库的控制文件

代码语言:javascript复制
SQL> alter database create standby controlfile as '/tmp/standby.ctl';

Database altered.

2.以备库的当前SCN号为起点,在主库上做一个增量备份

代码语言:javascript复制
-- 备库
SQL> select  to_char(current_scn) from v$database;

TO_CHAR(CURRENT_SCN)
----------------------------------------
1086639

-- 主库
rman target /
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup INCREMENTAL from scn 1086639 database format '/tmp/incre_%U';
release channel c1;
release channel c2;
}

3.将增量备份拷贝到备库上

代码语言:javascript复制
-- 主库
[oracle@orcl:/tmp]$ scp incre_0* oracle@ORCL_SBDB:/home/oracle
oracle@ORCL_SBDB's password: 
incre_0cvsjs8b_1_1                                                                                                                                                         100%  144KB 144.0KB/s   00:00    
incre_0dvsjs9a_1_1                                                                                                                                                         100%  416KB 416.0KB/s   00:00    
incre_0evsjs9a_1_1                                                                                                                                                         100%  144KB 144.0KB/s   00:00    
incre_0fvsjs9b_1_1                                                                                                                                                         100% 9856KB   9.6MB/s   00:00    
[oracle@orcl:/tmp]$ scp standby.ctl oracle@ORCL_SBDB:/home/oracle        
oracle@ORCL_SBDB's password: 
standby.ctl

4.使用新的控制文件将备库启动到mount状态

代码语言:javascript复制
shutdown immediate
startup nomount

rman target /
RMAN> restore controlfile from '/home/oracle/standby.ctl';

Starting restore at 18-APR-21
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK

channel ORA_DISK_1: copied control file copy
output file name=/oradata/orcl/control01.ctl
output file name=/u01/app/oracle/fast_recovery_area/orcl/control02.ctl
Finished restore at 18-APR-21

alter database mount;

5.增量备份注册到RMAN的catalog,取消日志应用,恢复增量备份

代码语言:javascript复制
-- 备库
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

rman target /
RMAN> catalog start with '/home/oracle/';
YES

RMAN> recover database noredo;

Starting recover at 18-APR-21
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=29 device type=DISK
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00001: /oradata/orcl/system01.dbf
destination for restore of datafile 00004: /oradata/orcl/users01.dbf
destination for restore of datafile 00006: /oradata/orcl/test09.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/incre_0evsjs9a_1_1
channel ORA_DISK_1: piece handle=/home/oracle/incre_0evsjs9a_1_1 tag=TAG20220418T133122
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00002: /oradata/orcl/sysaux01.dbf
destination for restore of datafile 00003: /oradata/orcl/undotbs01.dbf
destination for restore of datafile 00005: /oradata/orcl/example01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/incre_0dvsjs9a_1_1
channel ORA_DISK_1: piece handle=/home/oracle/incre_0dvsjs9a_1_1 tag=TAG20220418T133122
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01

Finished recover at 18-APR-21

6.开启备库的恢复进程

代码语言:javascript复制
alter database open read only;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

--GAP已修复
SQL> SELECT * FROM V$ARCHIVE_GAP;

no rows selected

SQL> SELECT max(sequence#) from v$archived_log where applied='YES';

MAX(SEQUENCE#)
--------------
            41

SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;

PROCESS   STATUS          THREAD#  SEQUENCE#     BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH      CONNECTED             0          0          0          0
ARCH      CONNECTED             0          0          0          0
ARCH      CONNECTED             0          0          0          0
ARCH      CLOSING               1         41          1        257
RFS       IDLE                  0          0          0          0
RFS       IDLE                  0          0          0          0
RFS       IDLE                  1         42      19969          1
MRP0      APPLYING_LOG          1         42      19969     245760

8 rows selected.

12c开始

代码语言:javascript复制
SQL> SELECT * FROM V$ARCHIVE_GAP;

   THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#     CON_ID
---------- ------------- -------------- ----------
     1        30         31      1

SQL> SELECT max(sequence#) from v$archived_log where applied='YES';

MAX(SEQUENCE#)
--------------
        29

SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;

PROCESS   STATUS      THREAD#  SEQUENCE#     BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH      CONNECTED     0      0          0      0
DGRD      ALLOCATED     0      0          0      0
DGRD      ALLOCATED     0      0          0      0
ARCH      CONNECTED     0      0          0      0
ARCH      CLOSING       1     29          1      268
ARCH      CONNECTED     0      0          0      0
RFS          IDLE          1     34        184      1
RFS          IDLE          1      0          0      0
RFS          IDLE          0      0          0      0
RFS          IDLE          0      0          0      0
RFS          IDLE          0      0          0      0
MRP0      WAIT_FOR_GAP  1     30          0      0
DGRD      ALLOCATED     0      0          0      0

13 rows selected.

[oracle@ORCL_SBDB:/archivelog]$ ll
total 11508
-rw-r-----. 1 oracle oinstall 11634176 Apr 19 02:05 1_28_1069567741.arc
-rw-r-----. 1 oracle oinstall   137728 Apr 19 02:08 1_29_1069567741.arc
-rw-r-----. 1 oracle oinstall     1536 Apr 19 02:08 1_32_1069567741.arc
-rw-r-----. 1 oracle oinstall     3584 Apr 19 02:08 1_33_1069567741.arc

当前GAP:30---31,缺少归档30,31

1.记录主库和备库当前SCN号

代码语言:javascript复制
-- 主库
SQL> set line222
SQL> col HXFNM for a100
SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;

  FILE_NUM HXFNM                                                FHSCN
---------- ---------------------------------------------------------------------------------------------------- --------------------
     1 /oradata/ORCL/system01.dbf                                       2600522
     3 /oradata/ORCL/sysaux01.dbf                                       2600522
     4 /oradata/ORCL/undotbs01.dbf                                      2600522
     5 /oradata/ORCL/pdbseed/system01.dbf                               2155383
     6 /oradata/ORCL/pdbseed/sysaux01.dbf                               2155383
     7 /oradata/ORCL/users01.dbf                                        2600522
     8 /oradata/ORCL/pdbseed/undotbs01.dbf                              2155383
     9 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8                         2600522
    10 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8                         2600522
    11 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8                         2600522
    12 /oradata/ORCL/test09.dbf                                         2600522




-- 备库
SQL> set line222
SQL> col HXFNM for a100
SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;

  FILE_NUM HXFNM                                                FHSCN
---------- ---------------------------------------------------------------------------------------------------- --------------------
     1 /oradata/ORCL_SBDB/system01.dbf                                  2600488
     3 /oradata/ORCL_SBDB/sysaux01.dbf                                  2600488
     4 /oradata/ORCL_SBDB/undotbs01.dbf                                 2600488
     5 /oradata/ORCL_SBDB/pdbseed/system01.dbf                          2155383
     6 /oradata/ORCL_SBDB/pdbseed/sysaux01.dbf                          2155383
     7 /oradata/ORCL_SBDB/users01.dbf                                   2600488
     8 /oradata/ORCL_SBDB/pdbseed/undotbs01.dbf                         2155383
     9 /oradata/ORCL_SBDB/PDB01/o1_mf_system_j7                         2600488
    10 /oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j7                         2600488
    11 /oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_                         2600488
    12 /oradata/ORCL_SBDB/test09.dbf                                    2600488


SQL> SELECT CURRENT_SCN FROM V$DATABASE;

CURRENT_SCN
-----------
    2600487

2.使用recover standby using service恢复

采用rman的新功能,recover standby using service,通过RMAN连接到target备库,然后用主库的service执行恢复备库命令。

语法:

代码语言:javascript复制
RECOVER DATABASE FROM SERVICE < PRIMARY DB SERVICE NAME > NOREDO USING COMPRESSED BACKUPSET;

模拟GAP期间,有数据文件添加的情况:

代码语言:javascript复制
-- 主库添加数据文件
SQL> alter tablespace TEST add datafile '/oradata/ORCL/test10.dbf' size 100M autoextend off;

Tablespace altered.

--检查备库scn是否添加数据文件
SQL> select file# from v$datafile where creation_change# > =2600487;

     FILE#
----------
    13

-- 备库启动到nomount状态
shutdown immediate
startup nomount

--rman恢复
rman target /

##恢复控制文件
RMAN> restore standby controlfile from service orcl;

Starting restore at 19-APR-22
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=739 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/oradata/ORCL_SBDB/control01.ctl
output file name=/oradata/ORCL_SBDB/control02.ctl
Finished restore at 19-APR-22

##mount数据库
alter database mount;

##restore新添加的数据文件
run
{
SET NEWNAME FOR DATABASE TO '/oradata/ORCL_SBDB/%f_%U';
RESTORE DATAFILE 13 FROM SERVICE orcl;
}

executing command: SET NEWNAME

Starting restore at 19-APR-22
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=503 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00013 to /oradata/ORCL_SBDB/13_data_D-ORCL_TS-TEST_FNO-13
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
Finished restore at 19-APR-22

##由于主备的数据文件目录不一致,需要修改controlfile中数据文件位置
catalog start with '/oradata/ORCL_SBDB';
YES
SWITCH DATABASE TO COPY;

##rename tempfile && logfile
alter system set standby_file_management=MANUAL;
##logfile
alter database clear logfile group 1;
alter database clear logfile group 2;
alter database clear logfile group 3;
alter database clear logfile group 4;
alter database clear logfile group 5;
alter database clear logfile group 6;
alter database clear logfile group 7;
alter database rename file '/oradata/ORCL/redo03.log' to '/oradata/ORCL_SBDB/redo03.log';
alter database rename file '/oradata/ORCL/redo02.log' to '/oradata/ORCL_SBDB/redo02.log';
alter database rename file '/oradata/ORCL/redo01.log' to '/oradata/ORCL_SBDB/redo01.log';
alter database rename file '/oradata/ORCL/standby_redo04.log' to '/oradata/ORCL_SBDB/standby_redo04.log';
alter database rename file '/oradata/ORCL/standby_redo05.log' to '/oradata/ORCL_SBDB/standby_redo05.log';
alter database rename file '/oradata/ORCL/standby_redo06.log' to '/oradata/ORCL_SBDB/standby_redo06.log';
alter database rename file '/oradata/ORCL/standby_redo07.log' to '/oradata/ORCL_SBDB/standby_redo07.log';
##tempfile
alter database rename file '/oradata/ORCL/temp01.dbf' to '/oradata/ORCL_SBDB/temp01.dbf';
alter database rename file '/oradata/ORCL/pdbseed/temp012022-04-11_06-13-50-844-AM.dbf' to '/oradata/ORCL_SBDB/pdbseed/temp012022-04-11_06-13-50-844-AM.dbf';
alter database rename file '/oradata/ORCL/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf' to '/oradata/ORCL_SBDB/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf';

alter system set standby_file_management=AUTO;

##恢复数据库
RMAN> recover database from service orcl noredo using compressed backupset;

Starting recover at 19-APR-22
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=2 device type=DISK
skipping datafile 5; already restored to SCN 2155383
skipping datafile 6; already restored to SCN 2155383
skipping datafile 8; already restored to SCN 2155383
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00001: /oradata/ORCL_SBDB/system01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00003: /oradata/ORCL_SBDB/sysaux01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00004: /oradata/ORCL_SBDB/undotbs01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00007: /oradata/ORCL_SBDB/users01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00009: /oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00010: /oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00011: /oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
destination for restore of datafile 00012: /oradata/ORCL_SBDB/test09.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01

Finished recover at 19-APR-22

Notes:如果主备库文件目录不一致,则需要catalog切换控制文件中路径,否则报错:

代码语言:javascript复制
RMAN> recover database from service orcl noredo using compressed backupset;

Starting recover at 19-APR-22
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using compressed network backup set from service orcl
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 04/19/2022 02:25:29
ORA-19625: error identifying file /oradata/ORCL/system01.dbf
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 7

3.开启备库日志应用

代码语言:javascript复制
--此时主备库的sch已恢复一致
SQL> col HXFNM for a100
SQL> set line222
SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;

  FILE_NUM HXFNM                                                FHSCN
---------- ---------------------------------------------------------------------------------------------------- --------------------
     1 /oradata/ORCL_SBDB/system01.dbf                                  2603512
     3 /oradata/ORCL_SBDB/sysaux01.dbf                                  2603514
     4 /oradata/ORCL_SBDB/undotbs01.dbf                                 2603516
     5 /oradata/ORCL_SBDB/pdbseed/system01.dbf                          2155383
     6 /oradata/ORCL_SBDB/pdbseed/sysaux01.dbf                          2155383
     7 /oradata/ORCL_SBDB/users01.dbf                                   2603518
     8 /oradata/ORCL_SBDB/pdbseed/undotbs01.dbf                         2155383
     9 /oradata/ORCL_SBDB/PDB01/o1_mf_system_j7                         2603521
    10 /oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j7                         2603524
    11 /oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_                         2603527
    12 /oradata/ORCL_SBDB/test09.dbf                                    2603530

11 rows selected.

-- 主库需要切几次归档
ALTER SYSTEM ARCHIVE LOG CURRENT;

或者

ALTER SYSTEM SWITCH LOGFILE;

--开启备库应用日志
alter database open;
alter pluggable database all open;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

4、测试同步情况

代码语言:javascript复制
--查看standby日志是否正常
set line 222
col member for a60
select t1.group#,t1.thread#,t1.bytes/1024/1024,t1.status,t2.member from gv$standby_log t1,gv$logfile t2 where t1.group#=t2.group#;


    GROUP#    THREAD# T1.BYTES/1024/1024 STATUS     MEMBER
---------- ---------- ------------------ ---------- ------------------------------------------------------------
     4      1            120 ACTIVE     /oradata/ORCL/standby_redo04.log
     5      1            120 UNASSIGNED /oradata/ORCL/standby_redo05.log
     6      1            120 UNASSIGNED /oradata/ORCL/standby_redo06.log
     7      1            120 UNASSIGNED /oradata/ORCL/standby_redo07.log

-- 主库插入数据
sqlplus test/test@pdb01
insert into test values (999);
commit;

-- 备库查询
SQL> alter session set container=pdb01;
SQL> select * from test.test;

    ID
----------
     1
     2
     999

-- 备库已同步

18c开始

将RECOVER STANDBY DATABASE命令与FROM SERVICE子句一起使用,以通过对主数据库进行的更改来刷新物理备用数据库。

备库可以直接在开启状态进行刷新。

语法:

代码语言:javascript复制
RECOVER STANDBY DATABASE FROM SERVICE primary_db;

模拟GAP期间,有数据文件添加的情况:

代码语言:javascript复制
-- 备库存在GAP
SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;

PROCESS   STATUS      THREAD#  SEQUENCE#     BLOCK#     BLOCKS
--------- ------------ ---------- ---------- ---------- ----------
ARCH      CONNECTED     0      0          0      0
DGRD      ALLOCATED     0      0          0      0
DGRD      ALLOCATED     0      0          0      0
ARCH      CONNECTED     0      0          0      0
ARCH      CONNECTED     0      0          0      0
ARCH      CONNECTED     0      0          0      0
RFS          IDLE          1      0          0      0
RFS          IDLE          1     72        119      1
RFS          IDLE          0      0          0      0
RFS          IDLE          0      0          0      0
MRP0      WAIT_FOR_GAP  1     70          0      0

11 rows selected.

SQL> SELECT max(sequence#) from v$archived_log where applied='YES';

MAX(SEQUENCE#)
--------------
        69

SQL> select * from v$archive_gap;

   THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#     CON_ID
---------- ------------- -------------- ----------
     1        70         70      1

-- 主库添加数据文件
SQL> alter tablespace TEST add datafile '/oradata/ORCL/test10.dbf' size 100M autoextend off;

Tablespace altered.

1、执行RECOVER STANDBY DATABASE FROM SERVICE刷新备库:

通过执行过程可以发现:RECOVER STANDBY DATABASE命令重新启动备用实例,从主数据库刷新控制文件,并自动重命名数据文件,临时文件和联机日志。 它可以还原添加到主数据库中的新数据文件,并还原到当前时间的备用数据库。

代码语言:javascript复制
##备库
##取消日志应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

##开始刷新备库
rman target /
RMAN> RECOVER STANDBY DATABASE FROM SERVICE orcl;

Starting recover at 19-APR-22
using target database control file instead of recovery catalog
Oracle instance started

Total System Global Area    6355441944 bytes

Fixed Size                     9141016 bytes
Variable Size                671088640 bytes
Database Buffers            2667577344 bytes
Redo Buffers                   7634944 bytes

contents of Memory Script:
{
   restore standby controlfile from service  'orcl';
   alter database mount standby database;
}
executing Memory Script

Starting restore at 19-APR-22
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=502 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
output file name=/oradata/ORCL_SBDB/control01.ctl
output file name=/oradata/ORCL_SBDB/control02.ctl
Finished restore at 19-APR-22

released channel: ORA_DISK_1
Statement processed
Executing: alter system set standby_file_management=manual

contents of Memory Script:
{
set newname for tempfile  1 to 
 "/oradata/ORCL_SBDB/temp01.dbf";
set newname for tempfile  2 to 
 "/oradata/ORCL_SBDB/pdbseed/temp012022-04-11_06-13-50-844-AM.dbf";
set newname for tempfile  3 to 
 "/oradata/ORCL_SBDB/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf";
   switch tempfile all;
set newname for datafile  1 to 
 "/oradata/ORCL_SBDB/system01.dbf";
set newname for datafile  3 to 
 "/oradata/ORCL_SBDB/sysaux01.dbf";
set newname for datafile  4 to 
 "/oradata/ORCL_SBDB/undotbs01.dbf";
set newname for datafile  5 to 
 "/oradata/ORCL_SBDB/pdbseed/system01.dbf";
set newname for datafile  6 to 
 "/oradata/ORCL_SBDB/pdbseed/sysaux01.dbf";
set newname for datafile  7 to 
 "/oradata/ORCL_SBDB/users01.dbf";
set newname for datafile  8 to 
 "/oradata/ORCL_SBDB/pdbseed/undotbs01.dbf";
set newname for datafile  9 to 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf";
set newname for datafile  10 to 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf";
set newname for datafile  11 to 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf";
set newname for datafile  12 to 
 "/oradata/ORCL_SBDB/test09.dbf";
set newname for datafile  14 to 
 "/oradata/ORCL/test10.dbf";
   restore from service  'orcl' datafile
    14;
   catalog datafilecopy  "/oradata/ORCL_SBDB/system01.dbf", 
 "/oradata/ORCL_SBDB/sysaux01.dbf", 
 "/oradata/ORCL_SBDB/undotbs01.dbf", 
 "/oradata/ORCL_SBDB/pdbseed/system01.dbf", 
 "/oradata/ORCL_SBDB/pdbseed/sysaux01.dbf", 
 "/oradata/ORCL_SBDB/users01.dbf", 
 "/oradata/ORCL_SBDB/pdbseed/undotbs01.dbf", 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf", 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf", 
 "/oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf", 
 "/oradata/ORCL_SBDB/test09.dbf", 
 "/oradata/ORCL/test10.dbf";
   switch datafile all;
}
executing Memory Script

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed tempfile 1 to /oradata/ORCL_SBDB/temp01.dbf in control file
renamed tempfile 2 to /oradata/ORCL_SBDB/pdbseed/temp012022-04-11_06-13-50-844-AM.dbf in control file
renamed tempfile 3 to /oradata/ORCL_SBDB/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf in control file

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting restore at 19-APR-22
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=504 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00014 to /oradata/ORCL/test10.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
Finished restore at 19-APR-22

cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/system01.dbf RECID=4 STAMP=1070263316
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/sysaux01.dbf RECID=5 STAMP=1070263317
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/undotbs01.dbf RECID=6 STAMP=1070263317
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/pdbseed/system01.dbf RECID=7 STAMP=1070263317
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/pdbseed/sysaux01.dbf RECID=8 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/users01.dbf RECID=9 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/pdbseed/undotbs01.dbf RECID=10 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf RECID=11 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf RECID=12 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf RECID=13 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL_SBDB/test09.dbf RECID=14 STAMP=1070263318
cataloged datafile copy
datafile copy file name=/oradata/ORCL/test10.dbf RECID=15 STAMP=1070263318

datafile 14 switched to datafile copy
input datafile copy RECID=15 STAMP=1070263318 file name=/oradata/ORCL/test10.dbf
datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=1070263316 file name=/oradata/ORCL_SBDB/system01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=1070263317 file name=/oradata/ORCL_SBDB/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=1070263317 file name=/oradata/ORCL_SBDB/undotbs01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=7 STAMP=1070263317 file name=/oradata/ORCL_SBDB/pdbseed/system01.dbf
datafile 6 switched to datafile copy
input datafile copy RECID=8 STAMP=1070263318 file name=/oradata/ORCL_SBDB/pdbseed/sysaux01.dbf
datafile 7 switched to datafile copy
input datafile copy RECID=9 STAMP=1070263318 file name=/oradata/ORCL_SBDB/users01.dbf
datafile 8 switched to datafile copy
input datafile copy RECID=10 STAMP=1070263318 file name=/oradata/ORCL_SBDB/pdbseed/undotbs01.dbf
datafile 9 switched to datafile copy
input datafile copy RECID=11 STAMP=1070263318 file name=/oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf
datafile 10 switched to datafile copy
input datafile copy RECID=12 STAMP=1070263318 file name=/oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf
datafile 11 switched to datafile copy
input datafile copy RECID=13 STAMP=1070263318 file name=/oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
datafile 12 switched to datafile copy
input datafile copy RECID=14 STAMP=1070263318 file name=/oradata/ORCL_SBDB/test09.dbf
Executing: alter database rename file '/oradata/ORCL/redo01.log' to '/oradata/ORCL_SBDB/redo01.log'
Executing: alter database rename file '/oradata/ORCL/redo02.log' to '/oradata/ORCL_SBDB/redo02.log'
Executing: alter database rename file '/oradata/ORCL/redo03.log' to '/oradata/ORCL_SBDB/redo03.log'

contents of Memory Script:
{
  recover database from service  'orcl';
}
executing Memory Script

Starting recover at 19-APR-22
using channel ORA_DISK_1
skipping datafile 5; already restored to SCN 2155383
skipping datafile 6; already restored to SCN 2155383
skipping datafile 8; already restored to SCN 2155383
skipping datafile 14; already restored to SCN 2658548
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00001: /oradata/ORCL_SBDB/system01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00003: /oradata/ORCL_SBDB/sysaux01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00004: /oradata/ORCL_SBDB/undotbs01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00007: /oradata/ORCL_SBDB/users01.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00009: /oradata/ORCL_SBDB/PDB01/o1_mf_system_j749f5d5_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00010: /oradata/ORCL_SBDB/PDB01/o1_mf_sysaux_j749f5fw_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00011: /oradata/ORCL_SBDB/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: using network backup set from service orcl
destination for restore of datafile 00012: /oradata/ORCL_SBDB/test09.dbf
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01

starting media recovery

media recovery complete, elapsed time: 00:00:00
Finished recover at 19-APR-22
Executing: alter system set standby_file_management=auto
Finished recover at 19-APR-22

2、rename standby log

代码语言:javascript复制
--刷新过后,redo log路径已修改,standby log路径未修改。
SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------
/oradata/ORCL_SBDB/redo03.log
/oradata/ORCL_SBDB/redo02.log
/oradata/ORCL_SBDB/redo01.log
/oradata/ORCL/standby_redo04.log
/oradata/ORCL/standby_redo05.log
/oradata/ORCL/standby_redo06.log
/oradata/ORCL/standby_redo07.log


--rename tempfile && logfile
alter system set standby_file_management=MANUAL;

--clear log
alter database clear logfile group 4;
alter database clear logfile group 5;
alter database clear logfile group 6;
alter database clear logfile group 7;
--standby log
alter database rename file '/oradata/ORCL/standby_redo04.log' to '/oradata/ORCL_SBDB/standby_redo04.log';
alter database rename file '/oradata/ORCL/standby_redo05.log' to '/oradata/ORCL_SBDB/standby_redo05.log';
alter database rename file '/oradata/ORCL/standby_redo06.log' to '/oradata/ORCL_SBDB/standby_redo06.log';
alter database rename file '/oradata/ORCL/standby_redo07.log' to '/oradata/ORCL_SBDB/standby_redo07.log';

3、主库切日志,备库开启日志应用

代码语言:javascript复制
-- 主库需要切几次归档
ALTER SYSTEM ARCHIVE LOG CURRENT;

或者

ALTER SYSTEM SWITCH LOGFILE;

--开启备库应用日志
alter database open;
alter pluggable database all open;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

4、测试同步情况

代码语言:javascript复制
--查看standby日志是否正常
set line222
col member for a60
select t1.group#,t1.thread#,t1.bytes/1024/1024,t1.status,t2.member from gv$standby_log t1,gv$logfile t2 where t1.group#=t2.group#;


    GROUP#    THREAD# T1.BYTES/1024/1024 STATUS     MEMBER
---------- ---------- ------------------ ---------- ------------------------------------------------------------
     4      1            120 ACTIVE     /oradata/ORCL/standby_redo04.log
     5      1            120 UNASSIGNED /oradata/ORCL/standby_redo05.log
     6      1            120 UNASSIGNED /oradata/ORCL/standby_redo06.log
     7      1            120 UNASSIGNED /oradata/ORCL/standby_redo07.log

-- 主库插入数据
sqlplus test/test@pdb01
insert into test values (999);
commit;

-- 备库查询
SQL> alter session set container=pdb01;

Session altered.

SQL> select * from test.test;

    ID
----------
     1
     2
       999
       999
       233
      7788

6 rows selected.

-- 备库已同步

参考

Restoring and Recovering Files Over the Network(DG)

Restoring and Recovering Files Over the Network(RMAN)

Rolling Forward a Standby With One Command 18C

0 人点赞