Monday, November 30, 2020

Switch Database to Copy

Oracle database 'switch database to copy' can be used to avoid lengthy restore times and therefore minimize downtime in the event of a database disaster or to move from a slow running storage to a faster storage. 

A quite common scenario is that we have 3 disk groups, +DATA, +FRA and +REDO with different performance characteristics, like the following:

+DATA Diskgoup is on fast storage (10k rpm)

+FRA Diskgroup is on  slowest storage (7200 rpm)

+REDO Diskgroup is on very fast storage (15k rpm)

example, +PURE_STORAGE_FLASH fastest disk group mounted so we switch the DB to use this diskgroup.



In case +DATA is slow and want to move to a faster storage then use this Oracle feature of switch database to copy with near zero downtime and without any lengthy restore/recover procedure. 

Check RMAN backup status

$ rman target=/ catalog rman/pwd_here@mycatdb

Recovery Manager: Release 19.0.0.0.0 - Production on Mon Nov 30 2020 10:27:26 2020

Version 19.3.0.0.0

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

connected to target database: MYTESTDB (DBID=64278080088)

RMAN> list copy of database;

specification does not match any datafile copy in the repository

RMAN> list backup of database;

specification does not match any backup in the repository

Take RMAN backup copy

RMAN> run

{

  allocate channel c1 device type disk format '+PURE_STORAGE_FLASH/%U';

  recover copy of database with tag 'FLASHBLADE';

  backup incremental level 1 for recover of copy with tag 'FLASHBLADE' database;

}

Now Let's have a look at our backup using the RMAN tag 'FLASHBLADE'

RMAN> list copy of database tag 'FLASHBLADE';

List of Datafile Copies
Key     File S Completion Time Ckp SCN    Ckp Time        Sparse
967     1    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SYSTEM_FNO-1_onut4h02
        Tag: FLASHBLADE
961     2    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SOE_FNO-2_ohut4gkn
        Tag: FLASHBLADE
968     3    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SYSAUX_FNO-3_omut4gvr
        Tag: FLASHBLADE
966     4    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-UNDOTBS1_FNO-4_olut4gtq
        Tag: FLASHBLADE
962     5    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SOE_FNO-5_oiut4gmp
        Tag: FLASHBLADE
965     7    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-USERS_FNO-7_oout4h09
        Tag: FLASHBLADE
963     8    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SOE_FNO-8_ojut4gp4
        Tag: FLASHBLADE
964     9    A 30-NOV-2020       12760708   29-NOV-2020      NO    
        Name: +PURE_STORAGE_FLASH/MYTESTDB/data_D-MYTESTDB_I-3539279042_TS-SOE_FNO-9_okut4gr5
        Tag: FLASHBLADE

Now let’s create some data after our last incremental backup.

SQL> create table t1 as select * from SCOTT.EMP;

SQL> select count(*) from t1;
  COUNT(*)
      22

Before we perform our ‘switch database to copy’ let’s check our datafile locations.

RMAN> REPORT SCHEMA;

Database Details:
Hostname       : ed-ol-raclin01
Database Name  : mytestdb
Date Created   : 22/02/20 11:11:11
Date Started   : 29/11/20 11:11:11
DB Status      : OPEN
Space Allocated:    144.40 GB
Space Used     :     81.15 GB

Data File Name                                Tbsp      Data MB
--------------------------------------------- --------- -------
+DATA/MYTESTDB/DATAFILE/system.286.1025441287     SYSTEM        940
+DATA/MYTESTDB/DATAFILE/sysaux.287.1025441321     SYSAUX      1,310
+DATA/MYTESTDB/DATAFILE/soe.297.1025448855        SOE        30,720
+DATA/MYTESTDB/DATAFILE/users.289.1025441337      USERS          54
+DATA/MYTESTDB/DATAFILE/soe.298.1025449357        SOE        30,720
+DATA/MYTESTDB/DATAFILE/undotbs1.288.1025441337   UNDOTBS1   22,685
+DATA/MYTESTDB/DATAFILE/soe.299.1025452299        SOE        30,720
+DATA/MYTESTDB/DATAFILE/soe.300.1025456873        SOE        30,720
Temp File Name                                Temp MB
--------------------------------------------- ----------
+DATA/MYTESTDB/TEMPFILE/temp.294.1025441421       5,825
BCT File Name
------------------------------------------------------------
+DATA/MYTESTDB/CHANGETRACKING/ctf.296.1025442733
Control File Name
------------------------------------------------------------
+DATA/MYTESTDB/CONTROLFILE/current.290.1025441411
+FRA/MYTESTDB/CONTROLFILE/current.266.1025441411
REDO Member
------------------------------------------------------------
+DATA/MYTESTDB/ONLINELOG/group_3.293.1025441413
+FRA/MYTESTDB/ONLINELOG/group_3.269.1025441415
+DATA/MYTESTDB/ONLINELOG/group_2.292.1025441413
+FRA/MYTESTDB/ONLINELOG/group_2.268.1025441415
+DATA/MYTESTDB/ONLINELOG/group_1.291.1025441413
+FRA/MYTESTDB/ONLINELOG/group_1.267.1025441415

Simulate Disaster

$ sqlplus / as sysdba

SQL> shutdown abort;

SQL> startup mount;

ORACLE instance started.
Total System Global Area 1.6267E+11 bytes
Fixed Size   30140696 bytes
Variable Size 1.9864E+10 bytes
Database Buffers 1.4227E+11 bytes
Redo Buffers   506728448 bytes
Database mounted.

Switch Database to Copy
Now let’s switch the database to use our RMAN image copy backup.
RMAN> switch database to copy;

Recover Database
Now recover the database to apply all changes since the last Incremental backup.

RMAN> recover database;
media recovery complete, elapsed time: 00:00:05
Finished recover at 30-Nov-2020

Open Database
RMAN> alter database open;
Statement processed

Check the previously created test table 't1'.
SQL> select count(*) from t1;
  COUNT(*)
      22

Check Database Details
SELECT * FROM V$DATAFILE;

RMAN> REPORT SCHEMA;

Summary
By using the 'Switch database to copy' you will be able to minimize downtime by avoiding a 'restore database' and also this feature can be used to switch the database to a faster storages like FLASH or SSDs compared to databases running on SAS or SATA disks.