Home » CMD Script » Oracle XE log rotate

Oracle XE log rotate

I recently decided to install Oracle XE (Express Edition) 11gR2 on a Windows Server 2008R2 platform.  That platform is x64 only.  OracleXE 11gR2 is only available in a 32bit version.  I thought that shouldn’t be a problem since 2008R2 has the ability to run 32bit programs.  Well, that was just the first of a few eye-opening misconceptions.  Once the installation was complete I upgraded the APEX environment to the latest version (v.4.1.1) following Mike Smithers posting. Much thanks to him for that.

Everything appeared to run just fine.  I decided to take a look at the alert_xe.log to see what, if anything, was going on with the database.  What I found was numerous messages like the following;

OER 7451 in Load Indicator : Error Code = OSD-04500: illegal option specified
O/S-Error: (OS 1) Incorrect function. !

The messages were posting about every 10 seconds so it was going to generate a large log file in a matter of time.  Being the enterprising individual that I am, I figured I’d just write a CMD script that would shut the database down, rotate the log file and then start the database back up.  First, being the lazy person, I decided to see if anyone had created some sort of log rotation program for windows.  I eventually stumbled upon the datori blog.  That solution didn’t quite work for me so I decided to create my own script.  I used some of his ideas but eventually ended up with the following code.

DISCLAIMER – Use this at your own risk.  I do not take responsibility for anything you do with this script.  I would encourage you to understand what it’s doing and modify/tweak it for your use.

@echo off
:: EnableDelayedExpansion is necessary to get the variables to expand as expected inside the FOR loops
setlocal EnableDelayedExpansion
::TITLE log_rotate.cmd
::  Log file rotate/rename program.
::  log_rotate.cmd <full\path\logfile.name>
::  EXAMPLE; log_rotate.cmd D:\oraclexe\xe\alert_xe.log
::  This script accepts one parameter in which should be the name of the log file to rotate.
::  The schedule of the script will dictate how frequently the files are rotated.  It will rotate
::  the files EVERY time it is executed.  There is no logic as to only daily or such.
::  The file compression is handled by ZIP and hard-coded toward the bottom.  The ZIP command also 
::  removes the file that is being zipped so only the zipped file remains.
::  The script should handle mixed zipped and not zipped files as well as missing files.
::  It will not run if the main log file that is passed in as a parameter does not exist.  This will
::  keep the log files from being rotated out unknowingly (such as a crash or something).
::  It currently does NOT recreate the empty log file.
::  BMajor    2012-JUL-10    Initial Creation

:: Set the following variables as needed
:: Compress/Zip archived files - Valid choices are Y or N (test only checks for Y though)
:: Number of copies to keep - Select a number based on the frequency of the execution

:: Log Start
echo Start %0: %TIME%

:: Check for valid input
if [%1] EQU [] (
  echo USAGE: %0 Drive:\Full\Path\filename.log
  goto :eoj

:: Set variable to parameter passed on command line
Set LOGFILE=%~nx1

:: Parse out input parameter to determine full working path without the filename.
:: Should be something like D:\oraclexe\work
Set WORKDIR=%~p1
Set WORKDRV=%~d1

:: Echo all variables set

:: Change to working drive
:: Change to working directory

:: Check for the existence of the main log file first.
:: If it doesn't exist then we don't want to rotate the files.
if not exist %LOGFILE% (
  echo ERROR:%LOGFILE% does not exist...exiting
  goto :eoj

:: Delete the last file in the sequence
::   Delete filename.log.%KEEP_COPIES%
::   Two delete tests/statements are used in case the file does not have a ZIP extension.
echo Remove last file in sequence, if it exists.
if exist %LOGFILE%.%KEEP_COPIES% (
  echo Found %LOGFILE%.%KEEP_COPIES%...deleting
if exist %LOGFILE%.%KEEP_COPIES%.zip (
  echo Found %LOGFILE%.%KEEP_COPIES%.zip...deleting
  del /q %LOGFILE%.%KEEP_COPIES%.zip

:: Rotate non-current log files (may or may not be named with a ZIP extension)
::   Rename filename.log.2.zip to filename.log.3.zip (etc, etc) starting with the 
::   highest number and working down to the lowest
:: The ! is used since we are inside a FOR loop using variables.
echo Rotate non-current log files.
for /l %%i in (%KEEP_COPIES%,-1,2) do (
  set /a i=%%i 
  set /a j=i-1
  if exist %LOGFILE%.!j! (
    echo Found %LOGFILE%.!j!...renaming to %LOGFILE%.!i!
    ren %LOGFILE%.!j! %LOGFILE%.!i!
  if exist %LOGFILE%.!j!.zip (
    echo Found %LOGFILE%.!j!.zip...renaming to %LOGFILE%.!i!.zip
    ren %LOGFILE%.!j!.zip %LOGFILE%.!i!.zip

:: Rotate current log file
:: ZIP rotated log file if COMPRESS=Y
echo Rotate current log file.
if exist %LOGFILE% (
  echo Found %LOGFILE%...renaming to %LOGFILE%.1
if /i %COMPRESS%==Y (
  echo "COMPRESS set ON"
  if exist %LOGFILE%.1 (
    echo Found %LOGFILE%.1...Compressing
    zip -m %LOGFILE%.1.zip %LOGFILE%.1

:: End
echo End %0: %TIME%
:: exit

I have a wrapper script that I use to call this script and pass into it the OracleXE log file.  I then schedule the wrapper script with “Task Scheduler” for once a week.  

Please drop a comment if this has in some way helped you out.

Posted in CMD Script, Oracle XE, Windows and tagged as , ,

Leave a Reply

Your email address will not be published. Required fields are marked *