@echo off

SET ORACLE_HOME=C:\oracle\product\11.2.0\dbhome_1
SET NLS_LANG=AMERICAN_AMERICA.WE8MSWIN1252
SET ORACLE_SID=%1%
SET DB_PWD=%2%

SET PATH=%ORACLE_HOME%\BIN;%PATH%

SET LOG_DEST=C:\LBS_SCRIPTS\RMAN_LOG\
SET TMPDIR=C:\LBS_SCRIPTS\TEMP\

REM +--------------------------------------------------------------------------+
REM | START                                                                    |
REM +--------------------------------------------------------------------------+

cd %TMPDIR%

REM +--------------------------------------------------------------------------+
REM | SET BACKUP TAG AND LEVEL (LEVEL 0=Full, 1=Incremental, 2=Differential    |
REM +--------------------------------------------------------------------------+

for /F "tokens=1 delims=/ " %%A in ('Date /t') do ( 
  SET DAY_OF_WK=%%A
)

IF %DAY_OF_WK%==Sat (
  echo SAT
  SET BK_TAG=sat_incr_0
  SET BK_LVL=0
)
IF %DAY_OF_WK%==Sun (
  echo SUN
  SET BK_TAG=sun_incr_2
  SET BK_LVL=2
)
IF %DAY_OF_WK%==Mon (
  echo MON 
  SET BK_TAG=mon_incr_2
  SET BK_LVL=2
)
IF %DAY_OF_WK%==Tue (
  echo TUE
  SET BK_TAG=tue_incr_2
  SET BK_LVL=2
)
IF %DAY_OF_WK%==Wed (
  echo WED
  SET BK_TAG=wed_incr_1
  SET BK_LVL=1
)
IF %DAY_OF_WK%==Thu (
  echo THU
  SET BK_TAG=thu_incr_2
  SET BK_LVL=2
)
IF %DAY_OF_WK%==Fri (
  echo FRI
  SET BK_TAG=fri_incr_2
  SET BK_LVL=2
)


SET LOG_FILE=%LOG_DEST%RMAN_%ORACLE_SID%_%DAY_OF_WK%.log

REM +--------------------------------------------------------------------------+
REM | GENERATE RMAN SCRIPT                                                     |
REM +--------------------------------------------------------------------------+

echo run { > RMAN_%ORACLE_SID%.rcf
echo   BACKUP INCREMENTAL LEVEL=%BK_LVL% TAG %BK_TAG% DATABASE PLUS ARCHIVELOG DELETE INPUT; >> RMAN_%ORACLE_SID%.rcf
echo   BACKUP CURRENT CONTROLFILE; >> RMAN_%ORACLE_SID%.rcf
echo } >> RMAN_%ORACLE_SID%.rcf
echo ALLOCATE CHANNEL FOR MAINTENANCE TYPE DISK; >> RMAN_%ORACLE_SID%.rcf
echo CROSSCHECK BACKUP; >> RMAN_%ORACLE_SID%.rcf
echo RELEASE CHANNEL; >> RMAN_%ORACLE_SID%.rcf
echo delete backup completed before 'sysdate-6'; >> RMAN_%ORACLE_SID%.rcf
echo EXIT >> RMAN_%ORACLE_SID%.rcf

REM +--------------------------------------------------------------------------+
REM | RUN RMAN                                                                 |
REM +--------------------------------------------------------------------------+

rman target sys/%DB_PWD%@%ORACLE_SID% cmdfile=%TMPDIR%RMAN_%ORACLE_SID%.rcf msglog=%LOG_FILE%

REM +--------------------------------------------------------------------------+
REM | SEND EMAIL                                                               |
REM +--------------------------------------------------------------------------+

SET EmailDistList=oradba@emeralit.com

SET SUBJ=RMAN DAILY BACKUP: %ORACLE_SID% - %BK_TAG%
findstr /I /C:"ERROR" %LOG_FILE% > nul
if %ERRORLEVEL% == 0 Set SUBJ=[ERROR] %SUBJ%

C:/lbs_scripts/blat262/full/blat "%LOG_FILE%" -to "%EmailDistList%" -f support@emeralit.com -server 192.168.90.40 -port 25 -subject "%SUBJ%"

REM +--------------------------------------------------------------------------+
REM | END                                                                      |
REM +--------------------------------------------------------------------------+

cd ..
:END