用logrotate旋转日志

我想按照以下规则安排日志轮换:

  1. 日志文件最后2天保持不变
  2. 超过2天的日志文件被归档并移动到按date命名的文件夹,它们被创build。 (例如,2012年12月1日的所有日志都将移至20121201并存档。)
  3. 超过14天的日志文件将被删除。

我想为此使用logrotate,但是我不确定它是否适合我的需求。

我想要这样的东西,但它不起作用。

/mylog/* { prerotate DIR=$(date +%y%m%d); // actually it's current date mkdir $DIR endscript daily rotate 2 olddir /mylog/$DIR missingok compress postrotate find /mylog -type d -mtime +180 | xargs rm -f endscript } 

Logrotate不理解$ DIR是可变的。

任何build议,欢迎!

根据超级用户的@ koniu, 这是不可能的 。 据我检查,我没有find任何其他的方式。

对于日志的年龄,你有一个maxage指令,根据手册:

  maxage count Remove rotated logs older than <count> days. The age is only checked if the logfile is to be rotated. The files are mailed to the configured address if maillast and mail are configured. 

看起来它不适用于在olddir中移动的旧日志。