Oracle Database 19c Table Space Point in Time Recovery for Pluggable Database
In this post I am creating a scenario for table space point in time recovery.
RMAN automatic Tablespace Point-In-Time Recovery ( TSPITR) enables you to quickly recover one or more tablespaces in an Oracle database to an earlier time, without affecting the state of the rest of the tablespaces and other objects in the database.
Prerequisites :-
1) Enable Database in Archive mode.
2) Take a full database backup
3) Make sure the table space is not default permanent table space
Change it as below
alter database default tablespace system;
Steps:
1) Lets connect to my pluggable database ORCLPDB and create few objects.
First I am creating a user in my PDB database.
[oracle@dbserver arch]$ sqlplus '/as sysdba'SQL*Plus: Release 19.0.0.0.0 - Production on Sat Jun 10 07:02:23 2023Version 19.14.0.0.0Copyright (c) 1982, 2021, Oracle. All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.14.0.0.0SQL> show pdbsCON_ID CON_NAME OPEN MODE RESTRICTED---------- ------------------------------ ---------- ----------2 PDB$SEED READ ONLY NO3 ORCLPDB READ WRITE NOSQL> alter session set container=ORCLPDB;Session altered.SQL> create user himanshu identified by oracle123;User created.SQL> grant connect,resource to himanshu;Grant succeeded.SQL> ALTER USER himanshu quota unlimited on users;User altered.
Now lets connect to the new user and create a table with some data.
[oracle@dbserver ~]$ sqlplus himanshu/oracle123@orclpdbConnected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.14.0.0.0SQL> show userUSER is "HIMANSHU"SQL> create table emp(name varchar2(20));Table created.SQL> BEGINFOR k IN 1..1100 LOOPInsert into emp values('dummyuser');END LOOP;END;/ 2 3 4 5 6PL/SQL procedure successfully completed.SQL> commit;Commit complete.SQL> select count(*) from himanshu.emp;COUNT(*)----------1100
I created a backup table as EMP_RESTORE as well.
2) Now lets see the object in USERS Tablespace
oracle@dbserver ~]$ sqlplus '/as sysdba'SQL*Plus: Release 19.0.0.0.0 - Production on Sun Jun 11 06:19:22 2023Version 19.14.0.0.0Copyright (c) 1982, 2021, Oracle. All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.14.0.0.0SQL> show pdbsCON_ID CON_NAME OPEN MODE RESTRICTED---------- ------------------------------ ---------- ----------2 PDB$SEED READ ONLY NO3 ORCLPDB READ WRITE NOSQL> alter session set container=ORCLPDB;Session altered.SQL> select OWNER,SEGMENT_NAME,SEGMENT_TYPE from dba_segments where TABLESPACE_NAME='USERS';OWNER SEGMENT_NAME SEGMENT_TYPE--------------- -------------------- ------------------HIMANSHU EMP TABLEHIMANSHU EMP_RESTORE TABLE
3) Check the current sequence number
SQL> archive log listDatabase log mode Archive ModeAutomatic archival EnabledArchive destination /u01/app/oracle/product/19c/dbhome_1/dbs/archOldest online log sequence 24Next log sequence to archive 26Current log sequence 26
4) Lets drop the users table space in PDB. Make sure it is not the default permanent table space.
SQL> drop tablespace users including contents and datafiles;Tablespace dropped.SQL> select tablespace_name from dba_tablespaces;TABLESPACE_NAME------------------------------SYSTEMSYSAUXUNDOTBS1TEMP
It is dropped now.
5) Make sure archives being backed up.
[oracle@dbserver ~]$ rman target /Recovery Manager: Release 19.0.0.0.0 - Production on Sun Jun 11 06:26:11 2023Version 19.14.0.0.0Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.connected to target database: ORCL (DBID=1666823206)RMAN> backup archivelog all;Starting backup at 11-JUN-23current log archivedusing target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=144 device type=DISKchannel ORA_DISK_1: starting archived log backup setchannel ORA_DISK_1: specifying archived log(s) in backup setinput archived log thread=1 sequence=20 RECID=4 STAMP=1139125489input archived log thread=1 sequence=21 RECID=5 STAMP=1139125529input archived log thread=1 sequence=22 RECID=6 STAMP=1139125874input archived log thread=1 sequence=23 RECID=7 STAMP=1139205619input archived log thread=1 sequence=24 RECID=8 STAMP=1139206378input archived log thread=1 sequence=25 RECID=9 STAMP=1139206447input archived log thread=1 sequence=26 RECID=10 STAMP=1139207178channel ORA_DISK_1: starting piece 1 at 11-JUN-23channel ORA_DISK_1: finished piece 1 at 11-JUN-23piece handle=/u01/app/oracle/product/19c/dbhome_1/dbs/0l1udr0b_21_1_1 tag=TAG20230611T062619 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:03Finished backup at 11-JUN-23Starting Control File and SPFILE Autobackup at 11-JUN-23piece handle=/u01/app/oracle/product/19c/dbhome_1/dbs/c-1666823206-20230611-01 comment=NONEFinished Control File and SPFILE Autobackup at 11-JUN-23
6) Now let's identify the log sequence timestamp.
[oracle@dbserver ~]$ sqlplus '/as sysdba'SQL*Plus: Release 19.0.0.0.0 - Production on Sun Jun 11 06:28:13 2023Version 19.14.0.0.0Copyright (c) 1982, 2021, Oracle. All rights reserved.Connected to:Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - ProductionVersion 19.14.0.0.0SQL> select sequence#,first_change#, to_char(first_time,'HH24:MI:SS') from v$log order by 3;SEQUENCE# FIRST_CHANGE# TO_CHAR(---------- ------------- --------25 2922665 06:12:5826 2922724 06:14:0727 2923323 06:26:18
7) As per information we know table space was dropped around some where before 6:26.
We can restore until timestamp or until sequence. I will table sequence 26 as the recovery point.
Make sure that the pluggable database is open in read write mode.
Make a temporary folder.
mkdir -p /u01/app/backup
[oracle@dbserver ~]$ rman target /Recovery Manager: Release 19.0.0.0.0 - Production on Sun Jun 11 08:05:09 2023Version 19.14.0.0.0Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.connected to target database: ORCL (DBID=1666823206)RMAN> recover tablespace ORCLPDB:USERS until sequence 26 auxiliary destination '/u01/app/backup';Starting recover at 11-JUN-23using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=275 device type=DISKRMAN-05026: warning: presuming following set of tablespaces applies to specified point-in-timeList of tablespaces expected to have UNDO segmentsTablespace SYSTEMTablespace ORCLPDB:SYSTEMTablespace UNDOTBS1Tablespace ORCLPDB:UNDOTBS1Creating automatic instance, with SID='tgEF'initialization parameters used for automatic instance:db_name=ORCLdb_unique_name=tgEF_pitr_ORCLPDB_ORCLcompatible=19.0.0db_block_size=8192db_files=200diagnostic_dest=/u01/app/oracle_system_trig_enabled=FALSEsga_target=4720Mprocesses=200db_create_file_dest=/u01/app/backuplog_archive_dest_1='location=/u01/app/backup'enable_pluggable_database=true_clone_one_pdb_recovery=true#No auxiliary parameter file usedstarting up automatic instance ORCLOracle instance startedTotal System Global Area 4949277048 bytesFixed Size 9144696 bytesVariable Size 889192448 bytesDatabase Buffers 4043309056 bytesRedo Buffers 7630848 bytesAutomatic instance createdList of tablespaces that have been dropped from the target database:Tablespace ORCLPDB:USERScontents of Memory Script:{# set requested point in timeset until logseq 26 thread 1;# restore the controlfilerestore clone controlfile;# mount the controlfilesql clone 'alter database mount clone database';# archive current online logsql 'alter system archive log current';# avoid unnecessary autobackups for structural changes during TSPITRsql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;';}executing Memory Scriptexecuting command: SET until clauseStarting restore at 11-JUN-23allocated channel: ORA_AUX_DISK_1channel ORA_AUX_DISK_1: SID=9 device type=DISKchannel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: restoring control filechannel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/19c/dbhome_1/dbs/c-1666823206-20230610-02channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/19c/dbhome_1/dbs/c-1666823206-20230610-02 tag=TAG20230610T074531channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01output file name=/u01/app/backup/ORCL/controlfile/o1_mf_l8c023tm_.ctlFinished restore at 11-JUN-23sql statement: alter database mount clone databasesql statement: alter system archive log currentsql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end;contents of Memory Script:{# set requested point in timeset until logseq 26 thread 1;# set destinations for recovery set and auxiliary set datafilesset newname for clone datafile 1 to new;set newname for clone datafile 9 to new;set newname for clone datafile 4 to new;set newname for clone datafile 11 to new;set newname for clone datafile 3 to new;set newname for clone datafile 10 to new;set newname for clone tempfile 1 to new;set newname for clone tempfile 3 to new;set newname for datafile 12 to"/u01/app/oracle/oradata/ORCL/orclpdb/users01.dbf";# switch all tempfilesswitch clone tempfile all;# restore the tablespaces in the recovery set and the auxiliary setrestore clone datafile 1, 9, 4, 11, 3, 10, 12;switch clone datafile all;}executing Memory Scriptexecuting command: SET until clauseexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEexecuting command: SET NEWNAMErenamed tempfile 1 to /u01/app/backup/ORCL/datafile/o1_mf_temp_%u_.tmp in control filerenamed tempfile 3 to /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_temp_%u_.tmp in control fileStarting restore at 11-JUN-23using channel ORA_AUX_DISK_1channel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_AUX_DISK_1: restoring datafile 00001 to /u01/app/backup/ORCL/datafile/o1_mf_system_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/app/backup/ORCL/datafile/o1_mf_undotbs1_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/app/backup/ORCL/datafile/o1_mf_sysaux_%u_.dbfchannel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/19c/dbhome_1/dbs/0g1udq7e_16_1_1channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/19c/dbhome_1/dbs/0g1udq7e_16_1_1 tag=TAG20230611T061301channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:45channel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_AUX_DISK_1: restoring datafile 00009 to /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_system_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00011 to /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_undotbs1_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00010 to /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_sysaux_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00012 to /u01/app/oracle/oradata/ORCL/orclpdb/users01.dbfchannel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/19c/dbhome_1/dbs/0h1udq8h_17_1_1channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/19c/dbhome_1/dbs/0h1udq8h_17_1_1 tag=TAG20230611T061301channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15Finished restore at 11-JUN-23datafile 1 switched to datafile copyinput datafile copy RECID=10 STAMP=1139213190 file name=/u01/app/backup/ORCL/datafile/o1_mf_system_l8c02bjm_.dbfdatafile 9 switched to datafile copyinput datafile copy RECID=11 STAMP=1139213190 file name=/u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_system_l8c03xxk_.dbfdatafile 4 switched to datafile copyinput datafile copy RECID=12 STAMP=1139213190 file name=/u01/app/backup/ORCL/datafile/o1_mf_undotbs1_l8c02bjz_.dbfdatafile 11 switched to datafile copyinput datafile copy RECID=13 STAMP=1139213190 file name=/u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_undotbs1_l8c043jh_.dbfdatafile 3 switched to datafile copyinput datafile copy RECID=14 STAMP=1139213190 file name=/u01/app/backup/ORCL/datafile/o1_mf_sysaux_l8c02bjs_.dbfdatafile 10 switched to datafile copyinput datafile copy RECID=15 STAMP=1139213190 file name=/u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_sysaux_l8c03qm4_.dbfcontents of Memory Script:{# set requested point in timeset until logseq 26 thread 1;# online the datafiles restored or switchedsql clone "alter database datafile 1 online";sql clone 'ORCLPDB' "alter database datafile9 online";sql clone "alter database datafile 4 online";sql clone 'ORCLPDB' "alter database datafile11 online";sql clone "alter database datafile 3 online";sql clone 'ORCLPDB' "alter database datafile10 online";sql clone 'ORCLPDB' "alter database datafile12 online";# recover and open resetlogsrecover clone database tablespace "ORCLPDB":"USERS", "SYSTEM", "ORCLPDB":"SYSTEM", "UNDOTBS1", "ORCLPDB":"UNDOTBS1", "SYSAUX", "ORCLPDB":"SYSAUX" delete archivelog;alter clone database open resetlogs;}executing Memory Scriptexecuting command: SET until clausesql statement: alter database datafile 1 onlinesql statement: alter database datafile 9 onlinesql statement: alter database datafile 4 onlinesql statement: alter database datafile 11 onlinesql statement: alter database datafile 3 onlinesql statement: alter database datafile 10 onlinesql statement: alter database datafile 12 onlineStarting recover at 11-JUN-23using channel ORA_AUX_DISK_1starting media recoveryarchived log for thread 1 with sequence 22 is already on disk as file /u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_22_1139042473.dbfarchived log for thread 1 with sequence 23 is already on disk as file /u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_23_1139042473.dbfarchived log for thread 1 with sequence 24 is already on disk as file /u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_24_1139042473.dbfarchived log for thread 1 with sequence 25 is already on disk as file /u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_25_1139042473.dbfarchived log file name=/u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_22_1139042473.dbf thread=1 sequence=22archived log file name=/u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_23_1139042473.dbf thread=1 sequence=23archived log file name=/u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_24_1139042473.dbf thread=1 sequence=24archived log file name=/u01/app/oracle/product/19c/dbhome_1/dbs/arch/1_25_1139042473.dbf thread=1 sequence=25media recovery complete, elapsed time: 00:00:01Finished recover at 11-JUN-23database openedcontents of Memory Script:{sql clone 'alter pluggable database ORCLPDB open';}executing Memory Scriptsql statement: alter pluggable database ORCLPDB opencontents of Memory Script:{# make read only the tablespace that will be exportedsql clone 'ORCLPDB' 'alter tablespaceUSERS read only';# create directory for datapump importsql 'ORCLPDB' "create or replace directoryTSPITR_DIROBJ_DPDIR as ''/u01/app/backup''";# create directory for datapump exportsql clone 'ORCLPDB' "create or replace directoryTSPITR_DIROBJ_DPDIR as ''/u01/app/backup''";}executing Memory Scriptsql statement: alter tablespace USERS read onlysql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/app/backup''sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/app/backup''Performing export of metadata...EXPDP> Starting "SYS"."TSPITR_EXP_tgEF_fDui":EXPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/TABLE_STATISTICSEXPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKEREXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLKEXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLKEXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLEEXPDP> Master table "SYS"."TSPITR_EXP_tgEF_fDui" successfully loaded/unloadedEXPDP> ******************************************************************************EXPDP> Dump file set for SYS.TSPITR_EXP_tgEF_fDui is:EXPDP> /u01/app/backup/tspitr_tgEF_29031.dmpEXPDP> ******************************************************************************EXPDP> Datafiles required for transportable tablespace USERS:EXPDP> /u01/app/oracle/oradata/ORCL/orclpdb/users01.dbfEXPDP> Job "SYS"."TSPITR_EXP_tgEF_fDui" successfully completed at Sun Jun 11 08:07:26 2023 elapsed 0 00:00:36Export completedcontents of Memory Script:{# shutdown clone before importshutdown clone abort}executing Memory ScriptOracle instance shut downPerforming import of metadata...IMPDP> Master table "SYS"."TSPITR_IMP_tgEF_Blzj" successfully loaded/unloadedIMPDP> Starting "SYS"."TSPITR_IMP_tgEF_Blzj":IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLKIMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLEIMPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/TABLE_STATISTICSIMPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKERIMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLKIMPDP> Job "SYS"."TSPITR_IMP_tgEF_Blzj" successfully completed at Sun Jun 11 08:07:58 2023 elapsed 0 00:00:26Import completedcontents of Memory Script:{# make read write and offline the imported tablespacessql 'ORCLPDB' 'alter tablespaceUSERS read write';sql 'ORCLPDB' 'alter tablespaceUSERS offline';# enable autobackups after TSPITR is finishedsql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';}executing Memory Scriptsql statement: alter tablespace USERS read writesql statement: alter tablespace USERS offlinesql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;Removing automatic instanceAutomatic instance removedauxiliary instance file /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_temp_l8c04l69_.tmp deletedauxiliary instance file /u01/app/backup/ORCL/datafile/o1_mf_temp_l8c04jdr_.tmp deletedauxiliary instance file /u01/app/backup/ORCL/onlinelog/o1_mf_3_l8c04b6y_.log deletedauxiliary instance file /u01/app/backup/ORCL/onlinelog/o1_mf_2_l8c04b6f_.log deletedauxiliary instance file /u01/app/backup/ORCL/onlinelog/o1_mf_1_l8c04b61_.log deletedauxiliary instance file /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_sysaux_l8c03qm4_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/datafile/o1_mf_sysaux_l8c02bjs_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_undotbs1_l8c043jh_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/datafile/o1_mf_undotbs1_l8c02bjz_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/FDAFACE81836315CE055000017019382/datafile/o1_mf_system_l8c03xxk_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/datafile/o1_mf_system_l8c02bjm_.dbf deletedauxiliary instance file /u01/app/backup/ORCL/controlfile/o1_mf_l8c023tm_.ctl deletedauxiliary instance file tspitr_tgEF_29031.dmp deletedFinished recover at 11-JUN-23
8) Rman uses transportable tablespace mechanism to plug the dropped tablespace back in to the database.
SQL> select tablespace_name,status,plugged_in from dba_tablespaces;TABLESPACE_NAME STATUS PLU------------------------------ --------- ---SYSTEM ONLINE NOSYSAUX ONLINE NOUNDOTBS1 ONLINE NOTEMP ONLINE NOUSERS OFFLINE YESSQL> alter tablespace users online;Tablespace altered.SQL> select tablespace_name,status,plugged_in from dba_tablespaces;TABLESPACE_NAME STATUS PLU------------------------------ --------- ---SYSTEM ONLINE NOSYSAUX ONLINE NOUNDOTBS1 ONLINE NOTEMP ONLINE NOUSERS ONLINE YES
9) Let's validate the data from the tables;
SQL> select OWNER,SEGMENT_NAME,SEGMENT_TYPE from dba_segments where TABLESPACE_NAME='USERS';OWNER SEGMENT_NAME SEGMENT_TYPE--------------- --------------- ------------------HIMANSHU EMP TABLEHIMANSHU EMP_RESTORE TABLESQL> select count(*) from himanshu.emp;COUNT(*)----------1100SQL> select count(*) from himanshu.EMP_RESTORE;COUNT(*)----------1100
Post a Comment
Post a Comment