New User? Register here - Existing Users: Username: Password: [Advanced Login]

 

 

Current Poll

Your preferred Interactive shell?









( 1026 votes ~ 11 comments )

 

Weblog entry #8 for lee

Logrotate with date-based extensions
Posted by lee on Tue 17 Jan 2006 at 14:42
Tags: none.
error: /etc/logrotate.d/local-apache-vhosts:8 unknown option 'dateext' -- ignoring line

Argh. I'd written a logrotate job to rotate example.com_access.log to example.com_access.log-20060115.gz. I need for the naming to be consistant since the directory gets rsynced onto another server, and the naming needs to be alphanumerically in order, so as not to confuse the web stats program. But stupidly, I now find the dateext option isn't supported by the logrotate in sarge (3.7-5) and the first version to support it (3.7.1-1) was uploaded two months later.

Attempting to replicate it using something like "extension `date --date='-1 day' +%Y%m%d`" doesn't work.

Does anyone know any sarge backport for logrotate before I build it myself? (noting that apt-build doesn't seem to work... sigh.)

Otherwise, one possible workaround is to use something like the following:

   postrotate
      EXT=`date --date='-1 day' +%Y%m%d`
      for f in $1; do
        mv $f.1 $f-$EXT; /bin/gzip -f $f-$EXT
      done
   endscript

 

Comments on this Entry

Posted by Steve (82.41.xx.xx) on Tue 17 Jan 2006 at 14:58
[ Send Message | View Steve's Scratchpad | View Weblogs ]

Try this local one I made:http://www.steve.org.uk/apt/local/

It isn't in my proper repository, but you should be able to rebuild it easily if you really wish.

Steve

[ Parent | Reply to this comment ]

Posted by lee (193.82.xx.xx) on Tue 17 Jan 2006 at 17:44
[ Send Message | View Weblogs ]

Thanks Steve, that seems to be working.

I've now begun the task of renaming the existing access_log.5.gz logs to match this new naming scheme.

The following is a quick perl script that renumbers based on the mtime minus 24 hours.

#!/usr/bin/perl
use POSIX qw(strftime);
use File::stat;
while(<>) {
        $file = $_;
        chomp($file);
        if ($file =~ /(.*).(\d+)\.gz/) {
                $date = stat($file)->mtime;
                $output = strftime "%Y%m%d", localtime($date - 86400) ;
                printf "mv \"%s\" \"%s-%s.gz\"\n", $file, $1, $output;
        } else {
                printf "# %s not matched\n", $file;
        }
}

[ Parent | Reply to this comment ]

Posted by lee (193.82.xx.xx) on Tue 17 Jan 2006 at 17:56
[ Send Message | View Weblogs ]

Can't edit comments, but there's a backslash missing in the regexp...

if ($file =~ /(.*)\.(\d+)\.gz/) {

[ Parent | Reply to this comment ]

Posted by Anonymous (84.56.xx.xx) on Tue 7 Mar 2006 at 14:53
This modified script reads the content of the current directory and perform the same action as the script above, maybe useful for someone...
#!/usr/bin/perl
use POSIX qw(strftime);
use File::stat;
use Cwd;

my $working_dir = cwd;

opendir( DIR, $working_dir ) || die "can't open dir $working_dir";
my @files =  readdir(DIR);
print @files;
closedir( DIR );

foreach ( @files ) {
        if( $_ =~ /(.*)\.(\d+)\.gz/ ) {
                $date = stat( $_)->mtime;
                $output = strftime "%Y%m%d", localtime($date - 86400) ;
                printf "mv \"%s\" \"%s-%s.gz\"\n", $_, $1, $output;
        } else {
                printf "# %s not matched\n", $_;
        }
}

[ Parent | Reply to this comment ]

 

 

Flattr