Apache Log File Rotation on Solaris

Here are some quick commands to configure daily rotation of the two main Apache logging files, on a Solaris system that uses a nightly cron task to execute the logadm utility (this is enabled by default on a clean installation of Solaris).

Purge any existing entries

To make nice clean start, you may wish to remove any existing log rotation entries.

# logadm -r /log/apache/access_log
# logadm -r /log/apache/error_log

Commit new log rotation settings

Our requirements were that the log files in question be rotated each day, no matter what size they are, and that the resultant file be compressed and named after the current date.

logadm -c -p 1d -t '$file-%Y-%m-%d' -z 0 -w /log/apache/access_log
logadm -c -p 1d -t '$file-%Y-%m-%d' -z 0 -w /log/apache/error_log

The -c indicates that the log files should copied and truncated rather than renamed. The -w parameter ensures that all of the settings specified in the command be written to the central log rotation configuration file in /etc/logadm.conf

Purging MySQL Binary Logs

If you have binary logging enabled on your MySQL server (i.e. the log-bin parameter is set in your MySQL configuration file), then you may notice a buildup of rather large files in your MySQL data directory over time (e.g. mysql-bin.000013). Generally speaking, you only need to enable this binary logging if your server is acting as a Replication Master or if you need the ability to do point in time recovery from your latest backup.

In any case, here are some useful commands for purging your binary log files:

To delete all binary logs older than 7 days:

mysql> PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);

To purge all logs before a specific date:

mysql> PURGE MASTER LOGS BEFORE '2008-01-01 00:00:00';

To purge logs automatically (every Monday at 3am) you could use a Unix cron job:

0 3 * * mon mysql -uroot -e "PURGE BINARY LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 7 DAY);"