Logrotate files with date in the file name

后端 未结 11 867
孤城傲影
孤城傲影 2020-12-13 05:08

I am trying to configure logrotate in RHEL for tomcat6 logs. Currently, logrotate works fine for catalina.out log, it is rotated and compressed properly.

The problem

11条回答
  •  醉话见心
    2020-12-13 05:23

    (First post ever so if it looks like a drunk spider has formatted it then sorry)

    After using our friend Google, here and I can't remember where else I managed to achieve something using logrotate (rather than cron or some other equivalent).

    I have a the following in /var/log/rsync/:

    -rw-r--r-- 1 root root 1.1M Apr  9 08:13 2014-04-09 07:48:18.log
    -rw-r--r-- 1 root root 1.4M Apr 11 15:20 2014-04-11 15:02:52.log
    -rw-r--r-- 1 root root 1.6M Apr 11 15:42 2014-04-11 15:22:04.log
    -rw-r--r-- 1 root root 1.8M Apr 12 08:01 2014-04-12 07:45:31.log
    -rw-r--r-- 1 root root 2.0M Apr 13 08:10 2014-04-13 07:53:38.log
    -rw-r--r-- 1 root root 2.2M Apr 14 08:19 2014-04-14 07:51:09.log
    -rw-r--r-- 1 root root 2.5M Apr 15 08:05 2014-04-15 07:37:38.log
    -rw-r--r-- 1 root root 2.7M Apr 16 08:11 2014-04-16 07:43:14.log
    

    and the following logrotate file:

    /var/log/rsync/*.log {
           daily
           rotate 7
           compress
           delaycompress
           notifempty
           missingok
    }
    

    which I thought was perfectly reasonable. But after it refused to work and on finding out that it would never work (courtesy of this post) I wondered if it could be fudged to make it work.

    After much testing and tweaking I managed to fudge it the following way:

    /var/log/rsync/dummy {
            daily
            rotate 0
            create
            ifempty
            lastaction
                    /usr/bin/find /var/log/rsync/ -mtime +7 -delete
                    /usr/bin/find /var/log/rsync/ -mtime +1 -exec gzip -q {} \;
            endscript
    }
    

    into a logrotate config file called /etc/logrotate.d/local-rsync. Then create the dummy log file:

    touch /var/log/rsync/dummy
    

    then force a logrotate with:

    logrotate -fv /etc/logrotate.d/local-rsync
    

    which gives:

    -rw-r--r-- 1 root root  71K Apr  9 08:13 2014-04-09 07:48:18.log.gz
    -rw-r--r-- 1 root root  88K Apr 11 15:20 2014-04-11 15:02:52.log.gz
    -rw-r--r-- 1 root root  82K Apr 11 15:42 2014-04-11 15:22:04.log.gz
    -rw-r--r-- 1 root root  84K Apr 12 08:01 2014-04-12 07:45:31.log.gz
    -rw-r--r-- 1 root root  87K Apr 13 08:10 2014-04-13 07:53:38.log.gz
    -rw-r--r-- 1 root root  92K Apr 14 08:19 2014-04-14 07:51:09.log.gz
    -rw-r--r-- 1 root root 2.5M Apr 15 08:05 2014-04-15 07:37:38.log
    -rw-r--r-- 1 root root 2.7M Apr 16 08:11 2014-04-16 07:43:14.log
    -rw-r--r-- 1 root root    0 Apr 16 12:11 dummy
    

    Now just wait for tomorrow morning...

    I realise that cron would be tidier however I have another element in the logrotate config file and wanted to keep the two together.

    Bonus with the dummy file is that it doesn't take up any space!

    You may find that it does not appear to have rotated anything one day. It took me while to work out why but then it twigged. find -mtime +1 is whole days (i.e. 24*60 minutes) and if the daily logrotate kicked in less than 24 hours since the last time/time the logs were created then it sometimes appears not to have worked. If it bothers you then using 23 hours with find -mmin +1380 might be more appropriate.

提交回复
热议问题