Logrotate清理带日期戳的文件 [英] Logrotate to clean up date stamped files

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

问题描述

我目前正在尝试整理由Cron创建的Oracle Recover日志文件的方法...

I'm currently trying to work out a method of tidying up Oracle Recover log files that are created by Cron...

当前,Cron使用以下命令每15分钟调用一次Oracle备用恢复过程:

Currently, our Oracle standby recover process is invoked by Cron every 15mins using the following command:

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

这将创建如下文件:

$ 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

我基本上想删除早于x天的文件,我认为logrotate非常适合...

I basically want to delete off files older than x days old, which I thought logrotate would be perfect for...

我已经使用以下配置文件配置了logrotate:

I've configured logrotate with the following config file:

/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?

我想我可以从Crontab日志文件中删除日期,然后使logrotate旋转该文件,但是日志文件中的日期将不能反映生成日志的日期...即010313上的恢复将在由于020313上的logrotate触发并旋转了文件,因此日期为020313的文件...

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

还有其他想法吗? 在此先感谢您的任何回应.

Any other ideas? And thank-you in advance for any responses.

致谢

Gavin

推荐答案

Logrotate会根据按顺序对旋转日志文件名称进行按词法排序的列表中的顺序,并按文件使用期限(使用文件的最后修改时间)来删除文件

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)

    您可能会发现
  • 旋转是已旋转文件的最大数量.如果旋转日志文件的数量更多,则将按词法对它们的名称进行排序,并删除按词法最小的名称.

  • rotate is 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.

最大容量定义了另一个删除轮换日志文件的条件.早于给定天数的所有轮换日志文件都将被删除.请注意,日期是根据文件的上次修改时间而不是文件名来检测的.

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

dateformat 允许对旋转文件中的日期进行特定的格式设置.手册页指出,格式应能按词法正确排序.

dateformat allows specific formatting for date in rotated files. Man page notes, that the format shall result in lexically correct sorting.

日期昨天允许一天前在日志文件名中使用日期.

dateyesterday allows using dates in log file names one day back.

要在每日轮换文件中保留给定的天数(例如7),必须将rotate的值设置为7,并且如果实际上每天都创建和轮换文件,则可以忽略maxage.

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.

如果几天未创建日志,例如在14天之内,轮换的日志文件数将保持不变(7).

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

maxage将通过始终删除太旧的文件来改善未生成日志"方案中的情况. 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.

您不能如OP所示使用dateformat,因为它不能按词法排序.弄乱dateformat可能会导致删除您真正想要的其他轮转日志文件.

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.

提示:使用-d选项从命令行运行logrotate以执行空运行:您将看到logrotate可以做什么,但实际上不做任何事情.然后使用-v(详细)执行手动运行,以便您可以确认完成的操作是否是您想要的.

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.

概念是:

让cron创建和更新日志文件,但是使用默认的dateext

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

/data/tier2/scripts/logs/recover_standby_SID.log-`date +\%Y\%m\%d`.log

仅将logrotate用于删除太旧的日志文件

Use logrotate only for removing too old log files

  • 针对不存在的日志文件/data/tier2/scripts/logs/recover_standby_SID.log
  • 使用missingok进行logrotate清理
  • rotate设置得足够高,以覆盖要保留的日志文件数(如果每天有一个旋转的"日志文件,则至少为7,但是可以安全地将其设置为9999,例如9999)
  • >
  • maxage设置为7.这将删除上次修改时间超过7天的文件.
  • dateext仅用于确保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.

Logrotate配置文件如下:

Logrotate configuration file would look like:

data/tier2/scripts/logs/recover_standby_SID.log {
    daily
    missingok
    rotate 9999
    maxage 7
    dateext
}

解决方案:每天通过logrotate直接旋转一次

我不确定如何创建源恢复备用文件,但是我认为Oracle或您的某些脚本会定期或连续地添加到文件/data/tier2/scripts/logs/recover_standby_SID.log

概念是:

  • 每天通过logrotate
  • 旋转文件一次
  • 直接使用包含恢复数据/data/tier2/scripts/logs/recover_standby_SID.log
  • 的日志文件
  • daily每天会导致轮换一次(就crondaily的理解而言)
  • rotate必须设置为7(或更高的数字).
  • maxage设置为7天(天)
  • dateext使用默认的logrotate日期后缀
  • dateyesterday过去通常使旋转文件中的日期后缀返回一天.
  • missingok可以清除较旧的文件,即使没有新的内容可以旋转.
  • 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 how cron understands daily)
  • rotate must be set to 7 (or any higher number).
  • maxage set to 7 (days)
  • dateext to use default logrotate date suffix
  • dateyesterday 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配置如下:

Logrotate config would look like:

data/tier2/scripts/logs/recover_standby_SID.log {
    daily
    missingok
    rotate 7
    maxage 7
    dateext
    dateyesterday
}

请注意,您可能需要使用copytruncate和其他类似的选项,这些选项与外部进程如何创建源日志文件以及其对旋转动作的反应有关.

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.

这篇关于Logrotate清理带日期戳的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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