Dynamic Log Deletion/Rotatoin Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamic Log Deletion/Rotatoin Script
# 1  
Old 10-16-2008
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.
# 2  
Old 10-16-2008
Hi, use:
Code:
find /opt/apache/test*/logs \( -name "access_*.gz" -o -name "error_*.gz" \) -type f -mtime +31 -exec rm {} \;
find /opt/apache/test*/logs \( -name "access_*" -o -name "error_*" \) -type f -mtime +8 -exec gzip {} \;

Regards
# 3  
Old 10-16-2008
Quote:
Originally Posted by Klashxx
Hi, use:
Code:
find /opt/apache/test*/logs \( -name "access_*.gz" -o -name "error_*.gz" \) -type f -mtime +31 -exec rm {} \;
find /opt/apache/test*/logs \( -name "access_*" -o -name "error_*" \) -type f -mtime +8 -exec gzip {} \;

Regards
The test* directory isn't actually all named test, it's the name of our webservers, and they don't have a normal pattern like that. Can I just substitute an * for test*? To make it

Code:
find /opt/apache/*/logs \( -name "access_*.gz" -o -name "error_*.gz" \) -type f -mtime +31 -exec rm {} \;
find /opt/apache/*/logs \( -name "access_*" -o -name "error_*" \) -type f -mtime +8 -exec gzip {} \;

# 4  
Old 10-16-2008
Of course!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deletion and recreation within a script

All printers are created on the PROD server and my script must pick up the differences in a diffs.txt file delete the printer if exist on the DEV server recreate it as it currently stands on the PROD server from the diffs.txt The following script I have, should allow me in synching the local... (0 Replies)
Discussion started by: ggoliath
0 Replies

2. Shell Programming and Scripting

file and directory deletion script

I saw many post about this but could not find a specific answer to this question. i have the following code find . -depth -name "$FILEEXT" -ctime -30 -exec rm {} \; when i type ./deletefiles.sh *.txt for example, it will find all the txt files less than 30 days old and delete them. my... (9 Replies)
Discussion started by: new2learn09
9 Replies

3. Shell Programming and Scripting

Unique Directory and Folder Deletion Script

Ok, so I just got charged with the task of deleting some 300 user folders in a FTP server to free up some space. I managed to grep and cut the list of user folders to delete into a list of one user folder per line. Example: bob00 jane01 sue03 In the home folder, there are folders a-z, and... (5 Replies)
Discussion started by: b4sher
5 Replies

4. Solaris

Script for automatic deletion of old folder

Hi, I have a folder with limited space. So i have to delete folder which are more than 5 days old automatically. So my script should be like delete the folder more than 5 days old. Can someone help me to generate a script for this. Thank you... Cheer Summer (5 Replies)
Discussion started by: summerpeh
5 Replies

5. UNIX for Dummies Questions & Answers

script for deletion using wildcards

find ./ -name t\* | sed "s@^./@@" > .filestobedeleted j=$(wc -l < .filestobedeleted) typeset -i cnt=0 typeset -i i=0 while read line do myarray=$line ((cnt = cnt + 1)) done < .filestobedeleted while (1 Reply)
Discussion started by: aishu
1 Replies

6. UNIX for Dummies Questions & Answers

conditional deletion of log files

i am a newbie and learning the ropes.........want to know how can i include a piece of code in a script (which redirects log files to a company standard out file) to delete the log files which are empty but should retain only those which has some process information in it......this should happen... (3 Replies)
Discussion started by: sonali007
3 Replies

7. Solaris

Script for automatic deletion of trash file of mail server

Hi, I have a mail server with limited space and operating system is sun solaris 8 (sparc). I do not have provisions to increase the space for home directory. So i have to delete files from /home/username/mail/trash which are more than 10 days old automatically. So my script should be like... (1 Reply)
Discussion started by: crown2100bd
1 Replies

8. Shell Programming and Scripting

Script for automatic deletion of old files

Hi, I have a folder with limited space. I do not have provisions to increase the space for this folder. So i have to delete files which are more than 1 month old automatically. But, i need to maintain the files created by 4 users and delete all the other files automatically which is more than 1... (4 Replies)
Discussion started by: vivek_scv
4 Replies

9. UNIX for Advanced & Expert Users

Dynamic time to append for every log info

Hi All, What i am looking for: i want to generate a dynamic system time for each and every data(line) which goes on generating data continously.... now i am using this script to append a time for dynamic data generation log file, tail -f filename_error.log|awk -v var="$( date... (2 Replies)
Discussion started by: gsprasanna
2 Replies

10. UNIX for Dummies Questions & Answers

Deletion of log files.

We have log files dating back to 2004. I need to write an interative script that will request the user for how many months he needs to keep the log files and remove all the remaing log files after that month. Supposing we are now in June 2006 , if teh user request to keep log file for the last 3... (1 Reply)
Discussion started by: Geeta
1 Replies
Login or Register to Ask a Question