Log cleaner script

Log cleaner script

This script will clear the apache logs, cpanel logs and the logs in /var. It will take a zipped backup before clearing logs.

#!/bin/bash

# Script to Clear Logs

echo “Clearing Logs in /var…..”

cd /var/log

for i in `ls -lSh /var/log|grep -v gz|grep “\-rw”|head -15|awk {’print $9′}`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “Clearing Cpanel logs…..”

cd /usr/local/cpanel/logs

echo “access_log error_log login_log stats_log melange.log” > /tmp/LOG

for i in `cat /tmp/LOG|cut -f1`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “Clearing Apache logs…..”

cd /usr/local/apache/logs

echo “suexec_log ssl_engine_log access_log error_log” > /tmp/LOG

for i in `cat /tmp/LOG|cut -f1`

do

cp -p “$i” “$i”.bak

echo “”>$i

if [ -e “$i”.bak.gz ]

then

mv “$i”.bak.gz “$i”.bak1.gz

fi

gzip “$i”.bak

done

echo “/var/log /usr/local/apache/logs /usr/local/cpanel/logs” > /tmp/LOG

echo “Removing Old backup logs”

for i in `cat /tmp/LOG|cut -f1`

do

cd $i

rm -f *.bak1.gz

done

rm -f /tmp/LOG

echo “Restarting Syslog…”

service syslog restart

echo “EVERY THING CLEARED ”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s