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 offSET ORACLE_HOME=C:\app\oracle\product\12.1.0\dbhome_1SET ORACLE_SID=ORCLREM Specify the location where you want to save the backupSET BACKUP_DIR=C:\backup\oracleREM Get current date in YYYYMMDD formatfor /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 existif not exist %BACKUP_DIR% mkdir %BACKUP_DIR%REM Log the start time of the backupecho Backup started at %date% %time% >> %BACKUP_DIR%\backup_log_%CURR_DATE%.txtREM 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 <<EOFRUN {BACKUP DATABASE FORMAT '%BACKUP_DIR%\db_%d_%T.bkp';BACKUP ARCHIVELOG ALL FORMAT '%BACKUP_DIR%\arch_%d_%T.bkp';DELETE NOPROMPT OBSOLETE;}EOFREM Log the end time of the backupecho Backup finished at %date% %time% >> %BACKUP_DIR%\backup_log_%CURR_DATE%.txtREM Print a messageecho 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.
Post a Comment
Post a Comment