@@header

set term off
/*
*
*  Author  : Vishal Gupta
*  Purpose : Display RMAN Backup size for a database
*  Arguments : 1 - Database name
*              2 - no of days
* 
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  01-Aug-08  Vishal Gupta  First Draft
*/
set term on

DEFINE database="%&&1%" 
DEFINE days="&&2"  

COLUMN start_time                   HEADING "StartTime"             FORMAT a9

COLUMN sum_mb_of_disk_control_file  HEADING "Disk|Control|(MB)"     FORMAT 9,999          
COLUMN sum_mb_of_disk_arch          HEADING "Disk|Arch|(MB)"        FORMAT 999,999
COLUMN sum_mb_of_disk_full          HEADING "Disk|Full|(MB)"        FORMAT 99,999,999
COLUMN sum_mb_of_disk_IncLevel1     HEADING "Disk|Inc 1|(MB)"       FORMAT 999,999          
COLUMN sum_mb_of_disk_IncLevel2     HEADING "Disk|Inc 2|(MB)"       FORMAT 999,999
COLUMN sum_mb_of_disk_others        HEADING "Disk|Others|(MB)"      FORMAT 99,999
COLUMN sum_mb_of_disk_daytotal      HEADING "Disk|DayTotal|(MB)"    FORMAT 99,999,999

COLUMN sum_mb_of_tape_control_file  HEADING "Tape|Control|(MB)"     FORMAT 99,999
COLUMN sum_mb_of_tape_arch          HEADING "Tape|Arch|(MB)"        FORMAT 999,999
COLUMN sum_mb_of_tape_full          HEADING "Tape|Full|(MB)"        FORMAT 99,999,999   
COLUMN sum_mb_of_tape_IncLevel1     HEADING "Tape|Inc 1|(MB)"       FORMAT 999,999       
COLUMN sum_mb_of_tape_IncLevel2     HEADING "Tape|Inc 2|(MB)"       FORMAT 999,999
COLUMN sum_mb_of_tape_others        HEADING "Tape|Others|(MB)"      FORMAT 99,999
COLUMN sum_mb_of_tape_daytotal      HEADING "Tape|DayTotal|(MB)"    FORMAT 99,999,999

BREAK ON REPORT

COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_control_file   FORMAT 9,999         ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_arch           FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_full           FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_IncLevel1      FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_IncLevel2      FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_others         FORMAT 99,999        ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_disk_daytotal       FORMAT 99,999,999    ON REPORT


COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_control_file   FORMAT 99,999        ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_arch           FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_full           FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_IncLevel1      FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_IncLevel2      FORMAT 99,999,999    ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_others         FORMAT 99,999        ON REPORT
COMPUTE SUM LABEL 'Total'   OF sum_mb_of_tape_daytotal       FORMAT 99,999,999    ON REPORT


SELECT a.db_name
     , TO_CHAR(TRUNC(a.START_TIME),'DD-MON-YY') Start_time
     , to_char(TRUNC(a.START_TIME),'Dy') Day
     , ROUND(SUM(DECODE(a.device_type, 'DISK', DECODE(a.backup_type,'ControlFile' ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_disk_control_file
     , ROUND(SUM(DECODE(a.device_type, 'DISK', DECODE(a.backup_type,'Arch'        ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_disk_arch
     , ROUND(SUM(DECODE(a.device_type, 'DISK', DECODE(a.backup_type,'Full'        ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_disk_full
     , ROUND(SUM(DECODE(a.device_type, 'DISK', DECODE(a.backup_type,'IncLevel1'   ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_disk_IncLevel1
     , ROUND(SUM(DECODE(a.device_type, 'DISK', DECODE(a.backup_type,'IncLevel2'   ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_disk_IncLevel2
     , ROUND(SUM(CASE WHEN (device_type = 'DISK'  AND backup_type IN ('ControlFile','Arch','Full','IncLevel1','IncLevel2') ) THEN 0
                      WHEN device_type = 'SBT_TAPE' THEN 0
                      ELSE a.bytes                                                    
                 END)/1024/1024)  sum_mb_of_disk_others
     , ROUND(SUM(CASE WHEN (device_type = 'DISK') THEN a.bytes
                      ELSE 0                                                    
                 END)/1024/1024)  sum_mb_of_disk_daytotal
     , ROUND(SUM(DECODE(a.device_type, 'SBT_TAPE', DECODE(a.backup_type,'ControlFile' ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_tape_control_file
     , ROUND(SUM(DECODE(a.device_type, 'SBT_TAPE', DECODE(a.backup_type,'Arch'        ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_tape_arch
     , ROUND(SUM(DECODE(a.device_type, 'SBT_TAPE', DECODE(a.backup_type,'Full'        ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_tape_full
     , ROUND(SUM(DECODE(a.device_type, 'SBT_TAPE', DECODE(a.backup_type,'IncLevel1'   ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_tape_IncLevel1
     , ROUND(SUM(DECODE(a.device_type, 'SBT_TAPE', DECODE(a.backup_type,'IncLevel2'   ,a.bytes,0)  ,0) )/1024/1024) sum_mb_of_tape_IncLevel2
     , ROUND(SUM(CASE WHEN (device_type = 'SBT_TAPE' AND backup_type  IN ('ControlFile','Arch','Full','IncLevel1','IncLevel2') ) THEN 0
                      WHEN device_type = 'DISK' THEN 0
                      ELSE a.bytes                                                    
                END)/1024/1024)  sum_mb_of_tape_others
     , ROUND(SUM(CASE WHEN (device_type = 'SBT_TAPE') THEN a.bytes
                 ELSE 0                                                    
                 END)/1024/1024)  sum_mb_of_tape_daytotal
FROM  (SELECT d.NAME db_name
           , bp.START_TIME
           , bp.device_type
           , DECODE(bp.backup_type
                     ,'L','Arch'
                     ,'D', DECODE(NVL(bp.incremental_level,-1)
                                   ,0,'Full'
                                   ,-1,'ControlFile'
                                   ,bp.incremental_level 
                                 )
                     ,'I', DECODE(bp.incremental_level                                   
                                   ,0,'Full'
                                   ,'IncLevel' ||bp.incremental_level 
                                 )
                     ,bp.backup_type
                    ) backup_type
           , bp.bytes
           , bp.incremental_level
      FROM rc_backup_piece bp
         , rc_database d
      WHERE d.db_key = bp.db_key
        AND d.name like '&database'
        AND TRUNC(bp.START_TIME) > TRUNC(sysdate - &days)
      ) a
group by a.DB_NAME
       , TRUNC(a.START_TIME)
ORDER BY a.DB_NAME
       , TRUNC(a.START_TIME);


@@footer

