Aug 23, 2017

Configure LOGROTATE on Linux to clean DB Alert logs

Ensure logrotate is available on your machine. 

$ type logrotate logrotate is hashed (/usr/sbin/logrotate) 

Create dedicated directory for logrotate configuration file [Optional, but recommended] 
$ pwd 
/home/oracle 
$ mkdir logrotate 


Create configuration file 
$ cd logrotate 
$ vi db_alert_log.conf /oradba/app/oracle/diag/rdbms/gnsh1/gnsh1/trace/alert_gnsh1.log {
           daily
           rotate 5
           olddir /home/oracle/backup_logs/11g_alert_log
           compress
           dateext
           copytruncate
           nocreate
       }  
/oradba/app/oracle12c/diag/rdbms/gnsh2/gnsh2/trace/alert_gnsh2.log {
           daily
           rotate 5
           olddir /home/oracle/backup_logs/12c_alert_log
           compress
           dateext
           copytruncate
           nocreate
       }

The above script instructs logrotate to work upon respective logfiles mentioned in it.  Logrotate would rotate the log "daily".
It will "rotate" for 5 days before  it deletes old logs.
"olddir" is optional  as it provides the path where the old logs are created. In case this option is not mentioned, then old logs are created at same location as the original.
"compress" would by default compress the old logs using gzip.
"dateext" is used to archive old versions of log files adding a daily extension like YYYYMMDD instead of simply adding a number.
"copytruncate" will truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a  new  one.
Finally, "nocreate" will not create new log files as we are truncating the original file.
Cronjob has being configured to run logrotate every day @6am
 

00 06 * * * logrotate -s /home/oracle/scripts/logrotate/db_alert_log_stat.f -f /home/oracle/scripts/logrotate/db_alert_log.conf 

The "-s" option is used to provide alternate state file. the default state file is /var/lib/logrotate.status. As we are executing logrotate as "ORACLE" user , we need to specify this option. In case we don't use this option, logrotate will generate below error :-

error: error creating unique temp file: Permission denied

No comments:

Post a Comment