Friday, 27 March 2015

Cold and hot backups




Offline (Cold) Backups

An offline cold backup is a physical backup of the database after it has been shutdown using the SHUTDOWN NORMAL command. If the database is shutdown with the IMMEDIATE or ABORT option, it should be restarted in RESTRICT mode and then shutdown with the NORMAL option. To have a complete cold backup the following files must be backed up.
  • All datafiles.
  • All control files.
  • All online redo log files (optional).
  • The init.ora file (can be recreated manually).
The location of all database files can be found in the data dictionary views, DBA_DATA_FILES, V$DATAFILE, V$LOGFILE and V$CONTROLFILE. These views can be queried even when the database is mounted and not open.

A cold backup of the database is an image copy of the database at a point in time. The database is consistent and restorable. This image copy can be used to move the database to another computer provided the same operating system is being used. If the database is in ARCHIVELOG mode, the cold backup would be the starting point for a point-in-time recovery. All archive logfiles necessary would be applied to the database once it is restored from the cold backup. Cold backups are useful if your business requirements allow for a shut-down window to backup the database. If your database is very large or you have 24x7 processing, cold backups are not an option, and you must use online (hot) backups.

Create a directory /u01/coldbackup.

Copy pfile to backup directory. If you are using spfile create pfile as follow.

$sqlplus / as sysdba

SQL> create pfile from spfile.
SQL> exit

$cp /u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora /u01/coldbackup

Note down C/R/D files location

$sqlplus / as sysdba

SQL> select name from v$controlfile;

NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/control01.ctl
/u01/app/oracle/flash_recovery_area/orcl/control02.ctl


SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/redo03.log
/u01/app/oracle/oradata/orcl/redo02.log
/u01/app/oracle/oradata/orcl/redo01.log

SQL> select file_name from dba_data_files;

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/users01.dbf
/u01/app/oracle/oradata/orcl/undotbs01.dbf
/u01/app/oracle/oradata/orcl/sysaux01.dbf
/u01/app/oracle/oradata/orcl/system01.dbf
/u01/app/oracle/oradata/orcl/example01.dbf
/u01/app/oracle/oradata/sample01.dbf

Now shut down the database.

Sql>shut immediate


Copy the all C/R/D files to backup loaction

 

---------------------

Restore cold Backup in case of database corrupted.
-        
            Restore all files to corresponding locations and start database.


Online (Hot) Backups

When databases must remain operational 24 hours a day, 7 days a week, or have become so large that a cold backup would take too long, Oracle provides for online (hot) backups to be made while the database is open and being used. To perform a hot backup, the database must be in ARCHIVELOG mode. Unlike a cold backup, in which the whole database is usually backed up at the same time, tablespaces in a hot backup scenario can be backed up on different schedules. The other major difference between hot and cold backups is that before a tablespace can be backed up, the database must be informed when a backup is starting and when it is complete. This is done by executing two commands:

hot backup full database at a time:
----------------------------------------------------------
Check the database in archive log mode or not.

Sql>archive log list

If not in archivelog mode put the db in archive mode.
Unlike above scenario, we can put whole database into begin backup mode as bellow.

SQL> alter database begin backup;

Take all datafiles in the database into backup location and end the backup mode.

SQL>alter database end begin backup;

 In Server Manager, execute the ARCHIVE LOG LIST command to get the current log sequence number. This is the last logfile you will keep as part of the hot backup. Next, force a log switch so Oracle will archive the current redo log file.
 
SQL>Alter system switch logfile;

Backup all the archived log files, beginning with the log sequence.

  Backup the control file using the following command:
 
SQL>alter database backup controlfile to 'file_name';

Hotbackup with tablespace method.

In order to avoid large amount of log generation we can follow tablespace level online backup.
  1. Find the oldest online log sequence number with the following command:

Sql>Archive log list
  1. In Server Manager, put the tablespace you want to back up in BEGIN backup mode as follows:
     
Sql>alter tablespace tablespace_name begin backup;
  1. Backup all the database files associated with the tablespace using an operating system utility.
  • Set the tablespace in END backup mode by using the following command:
     
Sql>alter tablespace tablespace_name end backup;

Repeat Steps 2, 3 and 4 for each tablespace that you want to back up.
  • In Server Manager, execute the ARCHIVE LOG LIST command to get the current log sequence number. This is the last logfile you will keep as part of the hot backup. Next, force a log switch so Oracle will archive the current redo log file.
     
Sql>Alter system switch logfile;
  • Backup all the archived log files, beginning with the log sequence in step 1 to the log sequence in step 5.
  • Backup the control file using the following command:
     
Sql>alter database backup controlfile to 'file_name';

Note: The control file should always be backed up after any structural change to the database is made.

===================================================


Restoring Hot backup:

1. Copy the database files files from the backup location to its corresponding directories ( to the same directories from where datafiles copied while taking backup). Also copy the controlfile backup.

2. Rename it to control01.ctl as it was earlier.

Note- If you had another copy of the control file with the name control02.ctl, then just create a second copy of the control01.ctl and rename it control02.ctl

3. DO NOT COPY OR restore the REDO logs.

4. Start the database in mount mode

              SQL> startup mount;

5.Recover your database using the following.

             SQL> Recover database using backup controlfile until cancel;

Note- the oracle system will suggest an ‘archive log file name’ to use for recovery, if you have copied the backup logs to the same location which was being used for storing the logs by the database, then u can just keep on pressing enter. Or you may give the full path to the log file.
 

 

RMAN configuration settings



 

Configuring a Redundancy-Based Retention Policy

The REDUNDANCY parameter of the CONFIGURE RETENTION POLICY command specifies how many full or level 0 backups of each datafile and control file that RMAN should keep.

In other words, if the number of full or level 0 backups for a specific datafile or control file exceeds the REDUNDANCY setting, then RMAN considers the extra backups as obsolete. The default retention policy is REDUNDANCY 1.

RMAN> show RETENTION POLICY;

RMAN configuration parameters for database with db_unique_name ORCL are:

CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default

Now take full backup or 0 level backup two times.

RMAN> backup database;

Starting backup at 26-FEB-15

using channel ORA_DISK_1

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf

input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf

input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf

input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example01.dbf

input datafile file number=00006 name=/u01/app/oracle/oradata/sample01.dbf

input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf

channel ORA_DISK_1: starting piece 1 at 26-FEB-15

channel ORA_DISK_1: finished piece 1 at 26-FEB-15

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_nnndf_TAG20150226T083509_bgx3c816_.bkp tag=TAG20150226T083509 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:08:11

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

including current control file in backup set

including current SPFILE in backup set

channel ORA_DISK_1: starting piece 1 at 26-FEB-15

channel ORA_DISK_1: finished piece 1 at 26-FEB-15

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_ncsnf_TAG20150226T083509_bgx3tlr4_.bkp tag=TAG20150226T083509 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 26-FEB-15.

-------------------------------------------

RMAN> backup database;

Starting backup at 26-FEB-15

using channel ORA_DISK_1

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf

input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf

input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01

input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example0

input datafile file number=00006 name=/u01/app/oracle/oradata/sample01.dbf

input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.

channel ORA_DISK_1: starting piece 1 at 26-FEB-15

channel ORA_DISK_1: finished piece 1 at 26-FEB-15

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/

channel ORA_DISK_1: backup set complete, elapsed time: 00:02:15

channel ORA_DISK_1: starting full datafile backup set

channel ORA_DISK_1: specifying datafile(s) in backup set

including current control file in backup set

including current SPFILE in backup set

channel ORA_DISK_1: starting piece 1 at 26-FEB-15

channel ORA_DISK_1: finished piece 1 at 26-FEB-15

piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 26-FEB-15

Check the backups by listing

RMAN> list backup summary;

List of Backups

===============

Key     TY LV S Device Type Completion Time #Pieces #Copies Compressed Tag

------- -- -- - ----------- --------------- ------- ------- ---------- ---

10      B  F  A DISK        26-FEB-15       1       1       NO         TAG2

11      B  F  A DISK        26-FEB-15       1       1       NO         TAG2

12      B  F  A DISK        26-FEB-15       1       1       NO         TAG2

13      B  F  A DISK        26-FEB-15       1       1       NO         TAG2

Now we have two full backups, due to retention policy is one backup(which is taken first) will become obsolete. We can check as bellow.

RMAN> report obsolete;

RMAN retention policy will be applied to the command

RMAN retention policy is set to redundancy 1

Report of obsolete backups and copies

Type                 Key    Completion Time    Filename/Handle

-------------------- ------ ------------------ --------------------

Backup Set           10     26-FEB-15

  Backup Piece       10     26-FEB-15          /u01/app/oracle/flash_recove

Backup Set           11     26-FEB-15

  Backup Piece       11     26-FEB-15          /u01/app/oracle/flash_recove

 

About listed backup is not required for restore and RMAN will not using for any future restore.

If we need to release the space in mount point. We can delete backups by using RMAN command as bellow.

RMAN> delete obsolete;

RMAN retention policy will be applied to the command

RMAN retention policy is set to redundancy 1

using channel ORA_DISK_1

Deleting the following obsolete backups and copies:

Type                 Key    Completion Time    Filename/Handle

-------------------- ------ ------------------ --------------------

Backup Set           10     26-FEB-15

  Backup Piece       10     26-FEB-15          /u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_nnndf_TAG20150226T083509_bgx3c816_.bkp

Backup Set           11     26-FEB-15

  Backup Piece       11     26-FEB-15          /u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_ncsnf_TAG20150226T083509_bgx3tlr4_.bkp

 

Do you really want to delete the above objects (enter YES or NO)? yes

deleted backup piece

backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_nnndf_TAG20150226T083509_bgx3c816_.bkp RECID=10 STAMP=872670912

deleted backup piece

backup piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2015_02_26/o1_mf_ncsnf_TAG20150226T083509_bgx3tlr4_.bkp RECID=11 STAMP=872671402

Deleted 2 objects

If we need to change the retention policy, we can do as follow.

RMAN>CONFIGURE RETENTION POLICY TO REDUNDANCY 2;

 

Configuring a Recovery Window-Based Retention Policy

The RECOVERY WINDOW parameter of the CONFIGURE command specifies the number of days between the current time and the earliest point of recoverability. 

RMAN>CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;