在文件名中记录日期的文件 [英] Logrotate files with date in the file name

查看:135
本文介绍了在文件名中记录日期的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在RHEL中为tomcat6日志配置logrotate。目前,logrotate对于catalina.out日志工作正常,它被正确旋转和压缩。

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 is with the files with date in them like:

catalina.2012-01-20.log
catalina.2012-01-21.log
catalina.2012-01-22.log

这些文件没有旋转。我明白,我必须在/etc/logrotate.d/tomcat6文件中配置这些文件,其中配置了catalina.out的旋转。但是我无法配置它。

These files are not being rotated. I understand that I have to configure these in /etc/logrotate.d/tomcat6 file where rotation for catalina.out is configured. But I am not able to configure it.

我想要的是每天要压缩的旧文件,除了当前日期日志文件。

All I want is these older files to be compressed daily, except the current date log file.

请问有人可以帮我吗?

感谢
Noman A。

Thanks Noman A.

推荐答案

(第一次发布,如果看起来像一个醉了的蜘蛛已经格式化了,那么抱歉)

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

使用我们的朋友Google之后,我不记得我在哪里设法使用logrotate(而不是cron或其他一些等效的)来实现某些功能。

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).

我有一个以下在/ var / log / rsync /:

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

和以下logrotate文件:

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
}

转换成名为/ etc / logrotate的logrotate配置文件。 d / local-rsync。然后创建虚拟日志文件:

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

touch /var/log/rsync/dummy

然后强制使用logrotate:

then force a logrotate with:

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

其中:

-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...

我意识到cron会更整洁,但是我在logrotate配置文件中有另一个元素,并希望将它们保持在一起。

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!

你可能会发现它似乎没有旋转任何一天。为了解决这个问题,我花了一段时间才开始琢磨。找到-mtime +1是整天(即24 * 60分钟),如果每天的logrotate从上次创建日期开始的时间不到24小时踢,那么它有时似乎没有工作。如果它打扰了你,那么使用23小时,找到-mmin +1380可能更合适。

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.

这篇关于在文件名中记录日期的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆