代码之家  ›  专栏  ›  技术社区  ›  Carmageddon

是否可以将logrotate配置为在目录中保留文件X天?

  •  0
  • Carmageddon  · 技术社区  · 7 年前

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

    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非常适合。。。

    /data/tier2/scripts/logs/recover_standby_*.log {
        daily
        dateext
        dateformat %d%m%Y
        maxage 7
        missingok
    }
    

    我是否缺少一些东西来获得期望的结果?

    我想我可以从Crontab日志文件中删除日期,然后让logrotate旋转该文件,但是日志文件中的日期不会反映日志生成的日期。。。i、 e.由于020313上的logrotate点火并旋转文件,010313上的恢复将以020313的日期存档。。。

    并提前感谢您的回复。

    当做

    加文

    0 回复  |  直到 13 年前
        1
  •  41
  •   Thomas BDX alecxe    11 年前

    Logrotate根据已旋转日志文件名的词汇排序列表中的顺序以及文件年龄(使用文件的上次修改时间)删除文件

    • 旋转

    • maxage 定义用于删除旋转日志文件的另一个条件。任何超过给定天数的已旋转日志文件都将被删除。请注意,日期是从文件上次修改时间检测到的,而不是从文件名检测到的。

    • 日期格式 允许对旋转文件中的日期进行特定格式设置。手册页指出

    • 昨天

    要在每日轮换的文件(例如7)中保留给定的天数,必须设置 rotate maxage ,如果您的文件确实每天都在创建和旋转。

    maxage 将通过始终删除太旧的文件来改善“未生成日志”情况。在7天无日志生成后,将不会出现任何轮换日志文件。

    dateformat 正如OP所示,因为它不是词汇上可排序的。搞砸 可能会导致删除您真正想要的其他旋转日志文件。

    -d 执行试运行的选项:您将看到logrotate可以做什么,但实际上什么都不做。然后使用以下命令执行手动运行: -v (详细)以便确认所做的是您想要的。

    解决方案:清除cron创建的日志

    dateext

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

    仅在删除太旧的日志文件时使用logrotate

    • /data/tier2/scripts/logs/recover_standby_SID.log
    • 使用 missingok 让logrotate清理发生
    • 设置 旋转
    • 设置 maxage
    • dateext 仅用于确保logrotate搜索看起来像已旋转的旧文件。

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

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

    我不确定源代码恢复备用文件是如何创建的,但我假设Oracle或您的某些脚本定期或持续地附加到文件中

    这一概念是:

    • 每天按顺序旋转文件一次 logrotate
    • 直接使用包含恢复数据的日志文件 /数据/第二层/脚本/日志/恢复\u备用\u SID。日志
    • daily 将导致每天轮换一次(如何轮换) cron )
    • 旋转
    • maxage
    • 使用默认的logrotate日期后缀
    • dateyesterday 用于使旋转文件中的日期后缀返回一天。
    • 米森戈克

    Logrotate配置如下所示:

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

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

        2
  •  29
  •   Jeremy L    10 年前

    你可以用 find 命令可以轻松完成该任务!它将删除所有 7 Days crontab

    $ cd /data/tier2/scripts/logs/    
    $ /usr/bin/find . -mtime +7 -name "*.log" -print -delete
    

    $ /usr/bin/find /data/tier2/scripts/logs/ -mtime +7 -name "*.log" -print -delete;
    
        3
  •  5
  •   Alexis Wilke    7 年前

    (更新) 你的选择是:

    • 正如Satish所回答的,放弃logrotate并在cron中放置一个find脚本

    起初,我认为更改日期格式以匹配日志可能有效,但正如Reid Nabinger指出的那样,日期格式无论如何都与logrotate不兼容。最近,我尝试配置相同的东西,但我希望logrotate删除Java旋转日志。我尝试了下面的配置,但它一直试图删除所有日志

    /opt/jboss/log/server.log.* {
        missingok
        rotate 0
        daily
        maxage 30
    }
    

    我最终实现了Satish所建议的——在cron中使用rm脚本进行简单的查找。

        4
  •  3
  •   Community Mohan Dere    9 年前

    (由于声誉不足,无法发表评论)

    如果其他条件都一样,我可能会同意 find 在cron工作中。

    https://stackoverflow.com/a/23108631

    本质上,这是一种将cron作业封装在logrotate文件中的方法。也许不是最漂亮或最有效率的,但正如我所说,我有我的理由。

        5
  •  2
  •   Ben Aveling    8 年前

    根据@Jan Vlcinsky,您可以让logrotate添加日期-只需使用 dateyesterday 为了得到正确的日期。

    但是,我发现,如果我没有日志文件,logrotate不会清理带有日期的文件。

    但是,如果您准备有一个空的日志文件,那么它可以正常工作。

    例如,清理/var/log/mylogfile。 touch /var/log/mylogfile.log

    /var/log/mylogfile.log
    {
            daily
            rotate 7
            maxage 7
            dateext
            dateformat .%Y%m%d
            extension .log
            ifempty
            create
    }
    

    此条目与mylogfile的存在相结合。log,触发logrotate来清理旧文件,就像它们是由logrotate创建的一样。

    daily , rotate maxage 导致7天后删除旧日志文件(或7个旧日志文件,以先到者为准)。

    dateext , dateformat extension 使logrotate与文件名匹配。

    ifempty create

    另一个测试提示是,准备编辑/var/lib/logrotate。重置“上次旋转”日期或logrotate的状态对您没有任何帮助。

        6
  •  1
  •   Reid Nabinger    11 年前

    仅供参考,我知道这是一个老问题,但它不适用于您的原因是因为您的日期格式在词汇上不可排序。从手册页:

       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.
    

    解决方案是更改为年月日,或者调用外部进程来执行清理。