How can I rotate many log files into a different subdirectory per rotation?

I have a directory with many log files, all of which I would like to rotate daily. For organizational purposes I would like to be able to move the rotated logs into a different directory (or subdirectory) named by date, keeping the last week of logs.

I can use logrotate to achieve most of this by roatating the files in-place or even move them to a single different directory using the olddir directive but I'm having trouble finding a solution for making individual sub-directories per rotation. How can I achieve this?:

Logs to rotate: /var/log/example/*

Desired target directories (keeping a week):
    /var/log/example/20121006/*
    [ ... ]
    /var/log/example/20121012/*

You should be able to call an external script in the postrotate directive:

postrotate
  /path/to/your.sh
endscript

and have that script do the moving, e.g.:

#!/bin/bash

newdir=/var/log/example/`date +%Y%m%d`

mkdir $newdir
mv /var/log/example.1.gz $newdir

find /var/log/example -mindepth 1 -maxdepth 1 -mtime +7 \
  -type d -print0 | xargs -0 rm -rf

However, it might be easier to just use the dateext directive. With that the rotated files will be appended with a timestamp (although not moved to a different directory).

See logrotate(8) for details about both directives.


You have olddir directive you can use in config file.

From the documentation:

olddir
Logs are moved into directory for rotation. The directory must be on the same physical device as the log file being rotated, and is assumed to be relative to the directory holding the log file unless an absolute path name is specified. When this option is used all old versions of the log end up in directory. This option may be overridden by the noolddir option.

Reference: https://manpages.debian.org/jessie/logrotate/logrotate.8.en.html