Linux Logrotate 清理带有日期标记的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14858752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logrotate to clean up date stamped files
提问by fatmcgav
I'm currently trying to work out a method of tidying up Oracle Recover log files that are created by Cron...
我目前正在尝试找出一种整理由 Cron 创建的 Oracle Recover 日志文件的方法......
Currently, our Oracle standby recover process is invoked by Cron every 15mins using the following command:
目前,我们的 Oracle 备用恢复过程由 Cron 使用以下命令每 15 分钟调用一次:
0,15,30,45 * * * * /data/tier2/scripts/recover_standby.sh SID >> /data/tier2/scripts/logs/recover_standby_SID_`date +\%d\%m\%y`.log 2>&1
This creates files that look like:
这将创建如下所示的文件:
$ ls -l /data/tier2/scripts/logs/
total 0
-rw-r--r-- 1 oracle oinstall 0 Feb 1 23:45 recover_standby_SID_010213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 2 23:45 recover_standby_SID_020213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 3 23:45 recover_standby_SID_030213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 4 23:45 recover_standby_SID_040213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 5 23:45 recover_standby_SID_050213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 6 23:45 recover_standby_SID_060213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 7 23:45 recover_standby_SID_070213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 8 23:45 recover_standby_SID_080213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 9 23:45 recover_standby_SID_090213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 10 23:45 recover_standby_SID_100213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 11 23:45 recover_standby_SID_110213.log
-rw-r--r-- 1 oracle oinstall 0 Feb 12 23:45 recover_standby_SID_120213.log
I basically want to delete off files older than x days old, which I thought logrotate would be perfect for...
我基本上想删除超过 x 天的文件,我认为 logrotate 非常适合......
I've configured logrotate with the following config file:
我已经使用以下配置文件配置了 logrotate:
/data/tier2/scripts/logs/recover_standby_*.log {
daily
dateext
dateformat %d%m%Y
maxage 7
missingok
}
Is there something I'm missing to get the desired outcome?
有什么我想得到的结果吗?
I guess I could remove the date from the Crontab log file, and then have logrotate rotate that file, however then the date in the log file wont reflect the day the logs were generated... i.e. Recoveries on 010313 would be in file with a date of 020313 due to logrotate firing on 020313 and rotating the file...
我想我可以从 Crontab 日志文件中删除日期,然后让 logrotate 旋转该文件,但是日志文件中的日期不会反映日志生成的日期......即 010313 上的恢复将在文件中由于 logrotate 在 020313 上触发并旋转文件,因此日期为 020313...
Any other ideas? And thank-you in advance for any responses.
还有其他想法吗?并提前感谢您的任何回复。
Regards
问候
Gavin
加文
回答by Satish
You can use find
command to do that task easily! It will delete all 7 Days
old files. Put it in crontab
and run nightly basis:
您可以使用find
命令轻松完成该任务!它将删除所有7 Days
旧文件。放入crontab
并每晚运行:
$ cd /data/tier2/scripts/logs/
$ /usr/bin/find . -mtime +7 -name "*.log" -print -delete
Or Better way
或者更好的方法
$ /usr/bin/find /data/tier2/scripts/logs/ -mtime +7 -name "*.log" -print -delete;
回答by Lars Nordin
(Updated) Your options are:
(更新)您的选择是:
- As Satish answered, abandon logrotate and put a find script in cron
- You could even, use logrotate and put a find script in the postrotate command
- 正如 Satish 回答的那样,放弃 logrotate 并在 cron 中放置一个查找脚本
- 您甚至可以使用 logrotate 并在 postrotate 命令中放置一个查找脚本
Initially, I thought that changing the dateformat to match your logs might work but as Reid Nabinger pointed out the date format was not compatible with logrotate anyway. Recently, I tried to configure the same thing but for Java rotated logs that I wanted logrotate to delete. I tried the configuration below but it kept trying to delete all logs
最初,我认为更改日期格式以匹配您的日志可能会起作用,但正如 Reid Nabinger 指出的那样,日期格式无论如何都与 logrotate 不兼容。最近,我尝试配置相同的东西,但对于 Java 旋转日志,我希望 logrotate 删除。我尝试了下面的配置,但它一直试图删除所有日志
/opt/jboss/log/server.log.* {
missingok
rotate 0
daily
maxage 30
}
I ended up just implementing what Satish suggested - a simple find with rm script in cron.
我最终只是实现了 Satish 的建议 - 在 cron 中使用 rm 脚本进行简单查找。
回答by northern-bradley
(Unable to comment as not enough reputation)
(由于声誉不够,无法发表评论)
I had a similar issue. By all accounts logrotate is useless for use on filenames with built in datestamps.
我有一个类似的问题。所有帐户 logrotate 用于内置日期戳的文件名是无用的。
If all else was equal I would probably go with find
in a cron job.
如果其他一切都相同,我可能会find
从事 cron 工作。
For my own reasons I wanted to use logrotate and eventually found a way: https://stackoverflow.com/a/23108631
由于我自己的原因,我想使用 logrotate 并最终找到了一种方法:https: //stackoverflow.com/a/23108631
In essence it was a way of encapsulating the cron job in a logrotate file. Maybe not the prettiest or most efficient but like I said, I had my reasons.
本质上,它是一种将 cron 作业封装在 logrotate 文件中的方法。也许不是最漂亮或最有效的,但就像我说的,我有我的理由。
回答by Jan Vlcinsky
Logrotate removes files according to order in lexically sorted list of rotated log file names, and also by file age (using last modification time of the file)
Logrotate 根据旋转日志文件名的词法排序列表中的顺序以及文件年龄(使用文件的最后修改时间)删除文件
rotateis maximal number of rotated files, you may find. If there is higher number of rotated log files, their names are lexically sorted and the lexically smallest ones are removed.
maxagedefines another criteria for removing rotated log files. Any rotated log file, being older than given number of days is removed. Note, that the date is detected from the file last modification time, not from file name.
dateformatallows specific formatting for date in rotated files. Man page notes, that the format shall result in lexically correct sorting.
dateyesterdayallows using dates in log file names one day back.
旋转是旋转文件的最大数量,您可能会发现。如果轮换日志文件的数量较多,则它们的名称将按词法排序并删除词法上最小的名称。
maxage定义了另一个用于删除轮换日志文件的标准。任何超过给定天数的轮换日志文件都将被删除。请注意,日期是从文件上次修改时间中检测到的,而不是从文件名中检测到的。
dateformat允许对旋转文件中的日期进行特定格式设置。手册页指出,该格式应导致词法正确排序。
dateyesterday允许在一天前的日志文件名中使用日期。
To keep given number of days in daily rotated files (e.g. 7), you must set rotate
to value of 7 and you may ignore maxage
, if your files are created and rotated really every day.
要在每日轮换的文件中保留给定的天数(例如 7),您必须将rotate
值设置为 7 并且您可以忽略maxage
,如果您的文件确实每天都被创建和轮换。
If log creation does not happen for couple of days, a.g. for 14 days, number of rotated log files will be still the same (7).
如果日志创建没有发生几天,例如 14 天,轮换日志文件的数量将仍然相同 (7)。
maxage
will improve the situation in "logs not produced" scenarios by always removing too old files. After 7 days of no log production there will be no rotated log files present.
maxage
将通过始终删除太旧的文件来改善“未生成日志”场景中的情况。7 天无日志生成后,将不存在轮换日志文件。
You cannot use dateformat
as OP shows, as it is not lexically sortable. Messing up with dateformat
would probably result in removing other rotated log files than you really wanted.
您不能dateformat
像 OP那样使用,因为它在词法上不可排序。弄乱dateformat
可能会导致删除其他您真正想要的旋转日志文件。
Tip: Run logrotate from command line with -d
option to perform a dry run: you will see what logrotate would do but doesn't actually do anything. Then perform a manual run using -v
(verbose) so that you can confirm that what is done is what you want.
提示:从命令行运行 logrotate 并带有-d
执行试运行的选项:您将看到 logrotate 会做什么,但实际上没有做任何事情。然后使用-v
(verbose)执行手动运行,以便您可以确认所做的是您想要的。
Solution: clean logs created by cron
解决办法:清理cron创建的日志
The concept is:
概念是:
Let cron to create and update the log files, but make small modification to create files, following logrotate standard file names when using default dateext
让 cron 创建和更新日志文件,但在使用默认时遵循 logrotate 标准文件名做一些小的修改来创建文件 dateext
/data/tier2/scripts/logs/recover_standby_SID.log-`date +\%Y\%m\%d`.log
Use logrotate only for removing too old log files
仅将 logrotate 用于删除太旧的日志文件
- aim at not existing log file
/data/tier2/scripts/logs/recover_standby_SID.log
- use
missingok
to let logrotate cleanup to happen - set
rotate
high enough, to cover number of log files to keep (at least 7, if there will be one "rotated" log file a day, but you can safely set it very high like 9999) - set
maxage
to 7. This will remove files which have last modification time higher than 7 days. dateext
is used just to ensure, logrotate searches for older files looking like rotated.
- 针对不存在的日志文件
/data/tier2/scripts/logs/recover_standby_SID.log
- 用于
missingok
让 logrotate 清理发生 - 设置
rotate
得足够高,以涵盖要保留的日志文件数(至少 7 个,如果每天有一个“轮换”日志文件,但您可以安全地将其设置得非常高,例如 9999) - 设置
maxage
为 7。这将删除上次修改时间超过 7 天的文件。 dateext
仅用于确保 logrotate 搜索看起来像旋转的旧文件。
Logrotate configuration file would look like:
Logrotate 配置文件如下所示:
data/tier2/scripts/logs/recover_standby_SID.log {
daily
missingok
rotate 9999
maxage 7
dateext
}
Solution: rotate directly by logrotate once a day
解决办法:直接通过logrotate每天轮换一次
I am not sure, how is source recovery standby file created, but I will assume, Oracle or some script of yours is regularly or continually appending to a file /data/tier2/scripts/logs/recover_standby_SID.log
我不确定源恢复备用文件是如何创建的,但我会假设,Oracle 或您的某些脚本会定期或不断地附加到文件中 /data/tier2/scripts/logs/recover_standby_SID.log
The concept is:
概念是:
- rotate the file once a day by
logrotate
- working directly with log file containing recovery data
/data/tier2/scripts/logs/recover_standby_SID.log
daily
will cause rotation once a day (in terms of howcron
understandsdaily
)rotate
must be set to 7 (or any higher number).maxage
set to 7 (days)dateext
to use default logrotate date suffixdateyesterday
used to cause date suffixes in rotated files being one day back.missingok
to clean older files even when no new content to rotate is present.
- 每天旋转一次文件
logrotate
- 直接使用包含恢复数据的日志文件
/data/tier2/scripts/logs/recover_standby_SID.log
daily
将导致每天轮换一次(就如何cron
理解而言daily
)rotate
必须设置为 7(或任何更高的数字)。maxage
设置为 7(天)dateext
使用默认的 logrotate 日期后缀dateyesterday
用于使旋转文件中的日期后缀返回一天。missingok
即使不存在要旋转的新内容,也可以清理旧文件。
Logrotate config would look like:
Logrotate 配置看起来像:
data/tier2/scripts/logs/recover_standby_SID.log {
daily
missingok
rotate 7
maxage 7
dateext
dateyesterday
}
Note, that you may need to play a bit with copytruncate
and other similar options which are related to how is the source log file created by external process and how it reacts to the act of rotation.
请注意,您可能需要使用copytruncate
其他类似选项,这些选项与外部进程如何创建源日志文件以及它如何对轮换行为做出反应有关。
回答by Reid Nabinger
FYI I know that this is an old question, but the reason why it does not work for you is because your dateformat is not lexically sortable. From the manpage:
仅供参考,我知道这是一个老问题,但它对您不起作用的原因是因为您的日期格式在词法上不可排序。从联机帮助页:
dateformat format_string
Specify the extension for dateext using the notation similar to strftime(3) function. Only
%Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the
character separating log name from the extension is part of the dateformat string. The sys-
tem clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps
generated by this format must be lexically sortable (i.e., first the year, then the month
then the day. e.g., 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort
lower while it is later). This is because when using the rotate option, logrotate sorts all
rotated filenames to find out which logfiles are older and should be removed.
The solution is to either change to one that goes year-month-date, or call an external process to perform the cleanup.
解决方案是更改为年-月-日,或者调用外部进程来执行清理。
回答by Ben Aveling
As per @Jan Vlcinsky, you can let logrotate add the date - just use dateyesterday
to get the right date.
根据@Jan Vlcinsky,您可以让 logrotate 添加日期 - 仅用于dateyesterday
获取正确的日期。
Or, if you want to put in the date yourself, you can 'aim' at the name without the date , and then the names with the date will be cleaned up.
或者,如果你想自己输入日期,你可以'瞄准'没有日期的名称,然后将清除带有日期的名称。
However, what I found is that if I don't have a log file there, logrotate doesn't do the cleanup of the files with dates.
但是,我发现如果那里没有日志文件,logrotate 不会清除带有日期的文件。
But if you're prepared to have an empty log file lying around, then it can be made to work.
但是,如果您准备好放置一个空的日志文件,那么它就可以工作了。
For example, to clean up /var/log/mylogfile.yyyymmdd.log after 7 days, touch /var/log/mylogfile.log
, then configure logrotate as follows:
例如,清理/var/log/mylogfile。7 天后yyyymmdd.log touch /var/log/mylogfile.log
,然后配置 logrotate 如下:
/var/log/mylogfile.log { daily rotate 7 maxage 7 dateext dateformat .%Y%m%d extension .log ifempty create }
This entry, combined with the existence of mylogfile.log, triggers logrotate to clean up old files, as if they had been created by logrotate.
这个条目,结合 mylogfile.log 的存在,触发 logrotate 清理旧文件,就好像它们是由 logrotate 创建的一样。
daily
, rotate
plus maxage
cause old log files to be deleted after 7 days (or 7 old log files, whichever comes first).
daily
,rotate
加上maxage
导致旧日志文件在 7 天后被删除(或 7 个旧日志文件,以先到者为准)。
dateext
, dateformat
plus extension
causes logrotate to match our filesnames.
dateext
, dateformat
plusextension
导致 logrotate 匹配我们的文件名。
And ifempty
plus create
ensure that there continues to be an empty file there, or the log rotation would stop.
而ifempty
加create
确保有仍然是一个空文件存在,或日志旋转将停止。
Another tip for testing, be prepared to edit /var/lib/logrotate.status to reset the 'last rotated' date or logrotate won't do anything for you.
另一个测试提示,准备编辑 /var/lib/logrotate.status 以重置“上次旋转”日期,否则 logrotate 不会为您做任何事情。