The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 10-16-2008
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Dynamic Log Deletion/Rotatoin Script

I've written a small static script for my log deletion, but I was wondering if there was a way to make it a dynamic script. here is how my script currently works.

Code:
#!/bin/sh
###########################################
#Script to zip logs older than 1 week old
#and to delete logs older than 30 days old
###########################################
#
##TEST
#
cd /opt/apache/test/logs
find . -name "access_*" -type f -mtime +8 -exec gzip {} \;
find . -name "access_*.gz" -type f -mtime +31 -exec rm {} \;
find . -name "error_*" -type f -mtime +8 -exec gzip {} \;
find . -name "error_*.gz" -type f -mtime +31 -exec rm {} \;
cd /opt/apache/test1/logs
find . -name "access_*" -type f -mtime +8 -exec gzip {} \;
find . -name "access_*.gz" -type f -mtime +31 -exec rm {} \;
find . -name "error_*" -type f -mtime +8 -exec gzip {} \;
find . -name "error_*.gz" -type f -mtime +31 -exec rm {} \;
cd /opt/apache/test2/logs
find . -name "access_*" -type f -mtime +8 -exec gzip {} \;
find . -name "access_*.gz" -type f -mtime +31 -exec rm {} \;
find . -name "error_*" -type f -mtime +8 -exec gzip {} \;
find . -name "error_*.gz" -type f -mtime +31 -exec rm {} \;
echo "Script complete on `date +%D`."
Now for this particular script, i have to simply copy and paste a new block when I create a new apache server, but I figured since the directory structure and log file names are always the same, there would be some way I could add in some actual logic on this.

Possibly cd to /opt/apache/, do a listing, then somehow use an array and a loop to capture the name of each directory in to a variable, and then use that variable to run the find command, do those 4 lines, then if another directory exists repeat.