Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is it possible to get logrotate to consider logfiles in a directory and all its subdirectories? (i.e. without explicitly listing the subdirectories.)

share|improve this question

2 Answers 2

up vote 33 down vote accepted

How deep do your subdirectories go?

/var/log/basedir/*.log /var/log/basedir/*/*.log {
    daily
    rotate 5
}

Will rotate all .log files in basedir/ as well as all .log files in any direct child of basedir. If you also need to go 1 level deeper just add another /var/log/basedir/*/*/*.log until you have each level covered.

This can be tested by using a separate logrotate config file which contains a constraint that will not be met (a high minsize) and then running log rotate yourself in verbose mode

logrotate -d testconfig.conf

the -d flag will list each log file it is considering to rotate.

share|improve this answer
3  
Thanks! Looks like -d puts logrotate in dry run mode (i.e. doesn't actually change anything) actually. –  ithinkihaveacat Dec 3 '10 at 15:36
    
Ah, that is even better! –  Dan R Dec 3 '10 at 16:35

In my case, the depth of subdirectories can change without warning, so I set up a bash script to find all subdirectories and create a config entry for each directory. It's also important for me to keep the structure of subdirectories after rotation, which wildcards (i.e. @DanR's answer) didn't seem to do. If you're doing daily logrotations, you could put this script in a daily cron-job.

basedir=/var/log/basedir/
#destdir=${basedir} # if you want rotated files in the same directories
destdir=/var/log/archivedir/ #if you want rotated files somewhere else
config_file=/wherever/you/keep/it
> ${config_file} #clear existing config_file contents

subfolders = $(find ${basedir} -type d)

for ii in ${subfolders}
do
    jj=${ii:${#basedir}} #strip off basedir, jj is the relative path

    #append new entry to config_file
    echo "${basedir}${jj}/* {
        olddir ${destdir}${jj}/
        daily
        rotate 5
    }" >> ${config_file}

    #add one line as spacing between entries
    echo "\n" >> ${config_file}

    #create destination folder, if it doesn't exist
    [ -d ${destdir}${jj} ] || mkdir ${destdir}${jj}
done

Like @DanR suggested, test with logrotate -d

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.