Windows batch script to take RMAN backup of Oracle Database



Steps to Create the Script:

  • Open Notepad or any text editor.
  • Paste the following script into the file.
  • Save it as rman_backup.bat (make sure the extension is .bat).


Batch Script:

@echo off
SET ORACLE_HOME=C:\app\oracle\product\12.1.0\dbhome_1
SET ORACLE_SID=ORCL

REM Specify the location where you want to save the backup
SET BACKUP_DIR=C:\backup\oracle

REM Get current date in YYYYMMDD format
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
    set CURR_DATE=%%c%%a%%b
)

REM Create the backup directory if it does not exist
if not exist %BACKUP_DIR% mkdir %BACKUP_DIR%

REM Log the start time of the backup
echo Backup started at %date% %time% >> %BACKUP_DIR%\backup_log_%CURR_DATE%.txt

REM Run RMAN backup and include the date in the log filename
%ORACLE_HOME%\bin\rman target / log %BACKUP_DIR%\rman_backup_log_%CURR_DATE%.txt <<EOF
RUN {
    BACKUP DATABASE FORMAT '%BACKUP_DIR%\db_%d_%T.bkp';
    BACKUP ARCHIVELOG ALL FORMAT '%BACKUP_DIR%\arch_%d_%T.bkp';
    DELETE NOPROMPT OBSOLETE;
}
EOF

REM Log the end time of the backup
echo Backup finished at %date% %time% >> %BACKUP_DIR%\backup_log_%CURR_DATE%.txt

REM Print a message
echo RMAN backup completed. Check the log for details.


Explanation of the Script:

ORACLE_HOME: Set this to your Oracle installation directory.
ORACLE_SID: Set this to your Oracle database SID.
BACKUP_DIR: This is the directory where the backup files will be stored.
rman target /: This command connects to RMAN with the target database.
CURR_DATE: This is the date in YYYYMMDD format derived from the system date (date /t). The for /f loop extracts the day, month, and year and rearranges them into a suitable format.
Log File Naming: The log file name now includes the current date. For example, the RMAN log file will be named rman_backup_log_YYYYMMDD.txt, and the backup log will be named backup_log_YYYYMMDD.txt.

Backup commands:

BACKUP DATABASE: Takes a full backup of the database.
BACKUP ARCHIVELOG ALL: Backs up all the archived redo logs.
DELETE OBSOLETE: Cleans up old backups that are no longer needed.

Usage:

Replace ORACLE_HOME and ORACLE_SID with your actual Oracle home and database SID.
Customize the BACKUP_DIR to the path where you want the backup to be saved.
Double-click on the .bat file to run the RMAN backup.




Please do like and subscribe to my youtube channel: https://www.youtube.com/@foalabs If you like this post please follow,share and comment