Hot Backups
Oracle introduced hot backups early on version 6, allowing us to start to have the ability to make hot backups of our database tablespaces without the need to shutdown the database as earlier versions forced us to do, also on version 10g Oracle gave us the possibility to start doing hot backups of the entire database making our life a little more easy. When executing a user managed backup online, Oracle stops recording checkpoints to all associated datafiles.
Let’s take a deep look on what’s happening internally in the database when we use the BEGIN BACKUP option:
- A hot backup flag in the datafiles header is set
- A checkpoint occurs, flashing all dirty blocks from memory to disk and synchronizing all datafiles headers to a same SCN and freezing the headers for consistency protection and recoverability.
Hot backups (inconsistent backups due to fractured data blocks) can be made to the whole database or at tablespace level and it will require a recovery process after restore the backup. Due to this, is very important to ensure that your archivelog files are being backed up all the time.
Note: When doing a hot backup of your database, do not include the temporary tablespace. This tablespace is not recorded in the control files, and should be recreated when restoring and recovering a database.
Hot Backup of a Whole Database
You can make a full backup of your database easily placing first your database to be in backup mode using the “ALTER DATABASE BEGIN BACKUP;” statement at SQL*PLUS. This feature was introduced on version 10g of the Oracle database. This is the most common type of user managed backup that is used for many DBAs around the world.
Now let’s go through all steps required to perform this backup with the database open:
1. Place your entire database in backup mode;
SQL> ALTER DATABASE BEGIN BACKUP;
2. Backup all datafiles and the PFILE or SPFILE. This backup is a simple physical copy of files at OS level.
$ cp $ORACLE_BASE/oradata/cdb1/*.dbf /stage/backup
$ cp $ORACLE_HOME/dbs/spfilecdb1.ora /stage/backup
3. Take the database out of the backup mode;
SQL> ALTER DATABASE END BACKUP;
4. Archive all unarchived redo logs so any redo required to recover the database are archived;
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
5. Make a backup of all archived redolog files (OS level).
$ cp $ORACLE_BASE/fast_recovery_area/*.arc /stage/backup
6. Create a copy of your control file using the statement “ALTER DATABASE”.
SQL> ALTER DATABASE BACKUP CONTROLFILE TO’/stage/backup/control.ctf’;
Note: The steps 4 and 5 above are required due that all redo generated during the backup must be available to be applied in case of a recovery, also in this example I’m suggesting the backup of the controlfile using the statement “ALTER DATABASE BACKUP CONTROLFILE TO” to avoid any risk of overwrite the original controlfile during the recovery process.
Hot Backup of Tablespaces
Now it’s time to learn how to do a hot backup of one or more tablespaces, in other worlds, do a backup of your tablespaces with your database running. The beauty about this is that you can make an inconsistent backup of some pieces of your database at different times without affect the availability of your database. On this option you will need to switch each tablespace you want to make an online backup into backup mode before manually copying all datafiles involved to a secondary storage location on your machine, network or tape. Everytime a tablespace is placed in backup mode, the database copies the whole changed data blocks into the redo stream to make all data consistent until that point.
Tip: Avoid parallel online tablespace backups (placing more than one tablespace in backup mode at the same time) due that it will generate performance problems to your database; also always make the backup of a tablespace on low load periords, never when it is being heavly used by the users.
To do an online backup of one or more tablespace you will just need to follow these simple steps:
1. Identify the tablespace’s datafiles by quering the DBA_DATA_FILES view;
SQL> SELECT tablespace_name, file_name
2 FROM sys.dba_data_files
3 WHERE tablespace_name = ‘EXAMPLE’;
2. Place the tablespace in backup mode;
SQL> ALTER TABLESPACE example BEGIN BACKUP;
3. Backup all datafiles associated with the tablespace that you placed in backup mode. This backup is a physical copy of the datafiles at OS level;
$ cp $ORACLE_BASE/oradata/cdb1/pdb1/example_01.dbf /stage/backup
4. Place the tablespace back in normal mode;
SQL> ALTER TABLESPACE example END BACKUP;
5. If you need to backup another tablespace, go back to the step 1, if not proceed to step 5;
6. Archive all unarchived redo logs so any redo required to recover the database are archived;
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
7. Make a backup of all archived redolog files (OS level).
$ cp $ORACLE_BASE/fast_recovery_area/*.arc /stage/backup
Note: If you forget to take your tablespace(s) off backup mode, it will generate performance problems and also will raise an error (ORA-01149) if trying to SHUTDOWN the database.
Check Datafile Status
It’s always useful to know how to check whether a datafile is part of a current online tablespace backup or even to determine whether you forgot any tablespaces in backup mode, this can be very easily accomplished by querying the V$BACKUP view, where the STATUS column value determine if a datafile is currently in backup mode or not (ACTIVE means in backup mode and NOT ACTIVE means not in backup mode).
Note: The V$BACKUP view is not useful if the control file currently in use by the database is a restored or newly created one, because a restored or re-created control file does not contain the information necessary to populate the V$BACKUP view accurately.
Here is a useful query that can be used over the V$BACKUP view to check the status of all datafiles at root or PDB level:
SQL> SET PAGESIZE 300
SQL> SET LINESIZE 300
SQL> COLUMN tb_name FORMAT A10
SQL> COLUMN df_name FORMAT A50
SQL> COLUMN status FORMAT A10
SQL> SELECT b.name AS “TB_NAME”, a.file# as “DF#”, a.name AS “DF_NAME”, c.status
2 FROM V$DATAFILE a, V$TABLESPACE b, V$BACKUP c
3 WHERE a.TS#=b.TS#
4 AND c.FILE#=a.FILE#
5 AND c.STATUS=’ACTIVE’;
TB_NAME DF# DF_NAME STATUS
————– ———- ———————————————– ——-
EXAMPLE 11 /u01/app/oracle/oradata/cdb1/pdb1/example01.dbf ACTIVE
SQL>
Control File Backup
Have a valid backup of the control file of a database is crucial for a successful recovery of a database, due to this, we will take a closer look on the available methods to make a user managed backup of a control file.
Tip: Always do a backup of the control file after making a structural change to a database operating in ARCHIVELOG mode, such as creating, deleting or modifying a tablespace.
Binary Backup
You can generate a binary copy of the control file via a simple SQL statement. A binary copy of the control file contains additional information such as the archived redo log history, offline range for read-only and offline tablespaces, temp files entries (since 10.2) and RMAN backup sets and copies data.
Here is one example of the SQL statement used to create a binary copy of the control file:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO ‘[filename_including_location] ‘;
You can also specify REUSE at the end of the above statement to allow the new control file to overwrite one that currently exists with the same name.
Text File Backup
You can generate a text file that contains a CREATE CONTROLFILE statement based on the current control file in use by the database via a simple SQL statement that can be executed on a database in MOUNT or OPEN state. This file is generated as a trace file and can be easily edited to create a proper script to be used to create a new control file.
Some example of the SQL statement used to create a trace file based on the current control file in use are:
- To generate a trace file with the RESETLOGS and NORESETLOGS statements in the trace subdirectory (you can also easily identify the name and location of the generated file by reading the database alert log), use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE;
- To generate a trace file in the trace subdirectory with the RESETLOGS option only use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE RESETLOGS;
- To generate a trace file in the trace subdirectory with the NORESETLOGS option only use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE NORESETLOGS;
- To generate a trace file in a specific location with the RESETLOGS and NORESETLOGS statements use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE AS ‘/stage/backup/ctlf.sql’;
- To generate a trace file in a specific location with the RESETLOGS and NORESETLOGS statements overwriting an existing file, use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE AS ‘/stage/backup/ctlf.sql’ REUSE;
- To generate a trace file with the RESETLOGS option only use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE TRACE AS ‘/stage/backup/ctlf.sql’ RESETLOGS;
- To generate a trace file with the NORESETLOGS option only use:
SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE TRACE AS ‘/stage/backup/ctlf.sql’ NORESETLOGS;
In the next post you will find some more interesting information regarding this topic
Cheers,
Francisco