Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT
Linux: Fedora Core release 1 (Yarrow)
iPlanet: iPlanet-WebServer-Enterprise/6.0SP1
Log Path: /usr/iplanet/servers/https-company/logs

I have iPlanet log rotation enabled rotating files on a daily basis.

The rotated logs are NOT compressed & are taking up too much space.

I need a script that will run daily that will do 3 things:

  1. Check the folder for uncompressed log files, compress them into a ZIP file
  2. Delete the uncompressed log files after they are compressed
  3. Check the folder & delete the ZIP log files that are OLDER than 12 months.

Assumes zip is on your machine as /usr/bin/zip and you use sendmail (mailx is a front end for sendmail).

The while read loops are there to allow trapping errors.

#!/bin/bash
cd /usr/iplanet/servers/https-company/logs
# keep today's log files as text
find . -name '*.log' -mtime +1 -type f |
while read fname
do
   /usr/bin/zip ${fname}.zip $fname
   if [ ? -ne o ] ; then
      echo "fatal error compressing log $fname" | mailx -s 'Log Error' zachs@my.com
      exit 1
   fi
   rm $fname || echo "fatal error $fname" | mailx -s 'Log Error' zachs@my.com && exit 1
done
# delete old zip files after one year
find . -type f -name '*.zip' -mtime +365 | 
while read fname 
do
  rm $fname || echo "fatal error $fname" | mailx -s 'Log Error' zachs@my.com && exit 1
done
# good exit
echo "log maintenance completed" | mailx -s 'Logs ok' zachs@my.com
exit 0

this post was removed because I "fixed" the issues

The script was able to zip a file but i think it needs some help in finding the "log" file...?

because the access & error logs don't have the file extension noted in the script '*.log'

When I do a ls -l in that folder there are no file extensions on the log files.... I need to fix the script to find these files to compress & delete

Let me know what you think? Thanks!

PS - I got the email to send so we're almost there! :slight_smile:

-rw-r--r-- 1 deals deals 1046826 Dec 11 08:59 access
-rw-r--r-- 1 deals deals 92941280 Dec 7 00:00 access.201212070000
-rw-r--r-- 1 deals deals 3044935 Dec 8 00:00 access.201212080000
-rw-r--r-- 1 deals deals 2711784 Dec 8 23:59 access.201212090000
-rw-r--r-- 1 deals deals 2653255 Dec 9 23:59 access.201212100000
-rw-r--r-- 1 deals deals 2688456 Dec 11 00:00 access.201212110000
drwxr-xr-x 2 root root 4096 Jul 24 2007 archive
-rw-r--r-- 1 deals deals 24136805 Dec 11 08:59 errors
-rw-r--r-- 1 deals deals 147018158 Dec 7 00:00 errors.201212070000
-rw-r--r-- 1 deals deals 88812144 Dec 7 23:59 errors.201212080000
-rw-r--r-- 1 deals deals 50790062 Dec 8 23:58 errors.201212090000
-rw-r--r-- 1 deals deals 47430403 Dec 9 23:59 errors.201212100000
-rw-r--r-- 1 deals deals 47537385 Dec 11 00:00 errors.201212110000

---------- Post updated at 10:28 AM ---------- Previous update was at 10:25 AM ----------

#!/bin/bash
cd /usr/iplanet/servers/https-company/logs
# keep today's log files as text
find . -name '*.log' -mtime +1 -type f |
while read fname
do
   /usr/bin/zip ${fname}.zip $fname
   if [ ? -ne o ] ; then
      echo "fatal error compressing log $fname" | /bin/mail -s 'Log Error' zachs@domain.com
      exit 1
   fi
   rm $fname || echo "fatal error $fname" | /bin/mail -s 'Log Error' zachs@domain.com && exit 1
done
# delete old zip files after one year
find . -type f -name '*.zip' -mtime +365 | 
while read fname 
do
  rm $fname || echo "fatal error $fname" | /bin/mail -s 'Log Error' zachs@domain.com && exit 1
done
# good exit
echo "log maintenance completed" | /bin/mail -s 'Logs ok' zachs@domain.com
exit 0

If you're only putting one file in each zip, there's no point using old-fashioned pkware pkzip, you might as well use a stream compressor like gzip. gzip will also delete the file by itself if it succeeds (and not if it fails).

You don't need to use $?, you can put the command directly into the if-statement.

I'm also not convinced your || && chain does precisely what you want, I'd make it an if.

This should match any log files ending in a dot then a few numbers, and avoid already-compressed logs.

#!/bin/bash
cd /usr/iplanet/servers/https-company/logs
# keep today's log files as text
find . -name '*.[0-9]*' '!' -name '*.gz' -mtime +1 -type f |
while read fname
do
    if ! gzip "$fname"
    then
        echo "fatal error compressing log $fname" | /bin/mail -s 'Log Error' zachs@domain.com
        exit 1
   fi
done

# delete old zip files after one year
find . -type f -name '*.gz' -mtime +365 | 
while read fname 
do
        if ! rm $fname
        then
                echo "fatal error $fname" | /bin/mail -s 'Log Error' zachs@domain.com
                exit 1
        fi
done
# good exit
echo "log maintenance completed" | /bin/mail -s 'Logs ok' zachs@domain.com
exit 0

You just want this in a zip, right. Nothing fancy?

correct in a zip nothing fancy at all.

'zip' is kind of alien in the UNIX world, it has has some limitations and your system may not even have it by default. tar and gzip are more ordinary.