用文件名中的DateTime旋转文件

发布于 2025-01-30 10:40:41 字数 3515 浏览 2 评论 0原文

我对Logrotate的使用很新,但我还没有找到任何有助于解决我当前问题的东西。

我的公司有一台服务器(Debian),我们经常创建CRON任务来处理通过FTP上传的文件。为了遵循这些提取物所带来的任何问题,CRON任务始终配置为编写日志(来自Echos和var_dump),以便能够轻松调试我们的脚本。

这些日志文件是在文件名中使用时间戳生成的,因此一个日志文件=执行一天。为了防止日志使用过多的磁盘空间,而不是手动删除旧日志,我想使用loogrotate来完成此作业。

例如,目的是,这是日志文件夹之一:

-rw-r----- 1 root root 0 May  1 21:55 index-no-guzzle-2022-05-01.log
-rw-r----- 1 root root 0 May  2 21:55 index-no-guzzle-2022-05-02.log
-rw-r----- 1 root root 0 May  3 21:55 index-no-guzzle-2022-05-03.log
-rw-r----- 1 root root 0 May  4 21:55 index-no-guzzle-2022-05-04.log
-rw-r----- 1 root root 0 May  5 21:55 index-no-guzzle-2022-05-05.log
-rw-r----- 1 root root 0 May  6 21:55 index-no-guzzle-2022-05-06.log
-rw-r----- 1 root root 0 May  7 21:55 index-no-guzzle-2022-05-07.log
-rw-r----- 1 root root 0 May  8 21:55 index-no-guzzle-2022-05-08.log
-rw-r----- 1 root root 0 May  9 21:55 index-no-guzzle-2022-05-09.log
-rw-r----- 1 root root 0 May 10 21:55 index-no-guzzle-2022-05-10.log
-rw-r----- 1 root root 0 May 11 21:55 index-no-guzzle-2022-05-11.log
-rw-r----- 1 root root 0 May 12 21:55 index-no-guzzle-2022-05-12.log
-rw-r----- 1 root root 0 May 13 21:55 index-no-guzzle-2022-05-13.log
-rw-r----- 1 root root 0 May 14 21:55 index-no-guzzle-2022-05-14.log
-rw-r----- 1 root root 0 May 15 21:55 index-no-guzzle-2022-05-15.log
-rw-r----- 1 root root 0 May 16 21:55 index-no-guzzle-2022-05-16.log
-rw-r----- 1 root root 0 May 17 21:55 index-no-guzzle-2022-05-17.log
-rw-r----- 1 root root 0 May 18 21:55 index-no-guzzle-2022-05-18.log
-rw-r----- 1 root root 0 May 19 10:14 index-no-guzzle-2022-05-19.log

这是我当前的logrotate配置文件:

/root/gun-logs/some-cron-logs-folder/*.log {
    daily
    missingok
    rotate 14
    maxage 14
    create 640 root root
}

但是,输出不是我所期望的:

root@myserver:/etc/logrotate.d# ls /root/gun-logs/some-cron-logs-folder/
index-no-guzzle-2022-05-01.log    index-no-guzzle-2022-05-07.log.1  index-no-guzzle-2022-05-14.log
index-no-guzzle-2022-05-01.log.1  index-no-guzzle-2022-05-08.log    index-no-guzzle-2022-05-14.log.1
index-no-guzzle-2022-05-02.log    index-no-guzzle-2022-05-08.log.1  index-no-guzzle-2022-05-15.log
index-no-guzzle-2022-05-02.log.1  index-no-guzzle-2022-05-09.log    index-no-guzzle-2022-05-15.log.1
index-no-guzzle-2022-05-03.log    index-no-guzzle-2022-05-09.log.1  index-no-guzzle-2022-05-16.log
index-no-guzzle-2022-05-03.log.1  index-no-guzzle-2022-05-10.log    index-no-guzzle-2022-05-16.log.1
index-no-guzzle-2022-05-04.log    index-no-guzzle-2022-05-10.log.1  index-no-guzzle-2022-05-17.log
index-no-guzzle-2022-05-04.log.1  index-no-guzzle-2022-05-11.log    index-no-guzzle-2022-05-17.log.1
index-no-guzzle-2022-05-05.log    index-no-guzzle-2022-05-11.log.1  index-no-guzzle-2022-05-18.log
index-no-guzzle-2022-05-05.log.1  index-no-guzzle-2022-05-12.log    index-no-guzzle-2022-05-18.log.1
index-no-guzzle-2022-05-06.log    index-no-guzzle-2022-05-12.log.1  index-no-guzzle-2022-05-19.log
index-no-guzzle-2022-05-06.log.1  index-no-guzzle-2022-05-13.log    index-no-guzzle-2022-05-19.log.1
index-no-guzzle-2022-05-07.log    index-no-guzzle-2022-05-13.log.1

唯一需要的结果是,我需要的结果是比N天更早的文件(这是14 )自动删除。

我可以从文件名中删除时间戳,但我将使用“每日”的时间打算,这意味着logrotate将在6或7 am左右执行,这是一个问题,因为我们的cron任务可以在夜间触发,这意味着日志将附加到附加错误的文件。

所以,我可以为此使用logrotate,如果是,如何?如果没有,这是实现这一目标的最安全方法并创建logrotate配置文件)。

非常感谢您或您的帮助!

I'm pretty new with the use of logrotate and I haven't find anything yet that helps with my current issue.

My company has a server (Debian) in which we often create cron tasks to handle files uploaded via FTP. In order to follow any issue that can come with those extractions, the cron task is always configured to write logs (from echos and var_dump) to be able to easily debug our scripts.

Those log files are generated with a timestamp in the filename, so one log file = one day of execution. To prevent the logs from using too much disk space, and not delete manually the old logs, I'd like to use logrotate to do this job.

For example purpose, here is that one of the logs folder looks like :

-rw-r----- 1 root root 0 May  1 21:55 index-no-guzzle-2022-05-01.log
-rw-r----- 1 root root 0 May  2 21:55 index-no-guzzle-2022-05-02.log
-rw-r----- 1 root root 0 May  3 21:55 index-no-guzzle-2022-05-03.log
-rw-r----- 1 root root 0 May  4 21:55 index-no-guzzle-2022-05-04.log
-rw-r----- 1 root root 0 May  5 21:55 index-no-guzzle-2022-05-05.log
-rw-r----- 1 root root 0 May  6 21:55 index-no-guzzle-2022-05-06.log
-rw-r----- 1 root root 0 May  7 21:55 index-no-guzzle-2022-05-07.log
-rw-r----- 1 root root 0 May  8 21:55 index-no-guzzle-2022-05-08.log
-rw-r----- 1 root root 0 May  9 21:55 index-no-guzzle-2022-05-09.log
-rw-r----- 1 root root 0 May 10 21:55 index-no-guzzle-2022-05-10.log
-rw-r----- 1 root root 0 May 11 21:55 index-no-guzzle-2022-05-11.log
-rw-r----- 1 root root 0 May 12 21:55 index-no-guzzle-2022-05-12.log
-rw-r----- 1 root root 0 May 13 21:55 index-no-guzzle-2022-05-13.log
-rw-r----- 1 root root 0 May 14 21:55 index-no-guzzle-2022-05-14.log
-rw-r----- 1 root root 0 May 15 21:55 index-no-guzzle-2022-05-15.log
-rw-r----- 1 root root 0 May 16 21:55 index-no-guzzle-2022-05-16.log
-rw-r----- 1 root root 0 May 17 21:55 index-no-guzzle-2022-05-17.log
-rw-r----- 1 root root 0 May 18 21:55 index-no-guzzle-2022-05-18.log
-rw-r----- 1 root root 0 May 19 10:14 index-no-guzzle-2022-05-19.log

And here is my current logrotate configuration file:

/root/gun-logs/some-cron-logs-folder/*.log {
    daily
    missingok
    rotate 14
    maxage 14
    create 640 root root
}

However, the output is not what I expect:

root@myserver:/etc/logrotate.d# ls /root/gun-logs/some-cron-logs-folder/
index-no-guzzle-2022-05-01.log    index-no-guzzle-2022-05-07.log.1  index-no-guzzle-2022-05-14.log
index-no-guzzle-2022-05-01.log.1  index-no-guzzle-2022-05-08.log    index-no-guzzle-2022-05-14.log.1
index-no-guzzle-2022-05-02.log    index-no-guzzle-2022-05-08.log.1  index-no-guzzle-2022-05-15.log
index-no-guzzle-2022-05-02.log.1  index-no-guzzle-2022-05-09.log    index-no-guzzle-2022-05-15.log.1
index-no-guzzle-2022-05-03.log    index-no-guzzle-2022-05-09.log.1  index-no-guzzle-2022-05-16.log
index-no-guzzle-2022-05-03.log.1  index-no-guzzle-2022-05-10.log    index-no-guzzle-2022-05-16.log.1
index-no-guzzle-2022-05-04.log    index-no-guzzle-2022-05-10.log.1  index-no-guzzle-2022-05-17.log
index-no-guzzle-2022-05-04.log.1  index-no-guzzle-2022-05-11.log    index-no-guzzle-2022-05-17.log.1
index-no-guzzle-2022-05-05.log    index-no-guzzle-2022-05-11.log.1  index-no-guzzle-2022-05-18.log
index-no-guzzle-2022-05-05.log.1  index-no-guzzle-2022-05-12.log    index-no-guzzle-2022-05-18.log.1
index-no-guzzle-2022-05-06.log    index-no-guzzle-2022-05-12.log.1  index-no-guzzle-2022-05-19.log
index-no-guzzle-2022-05-06.log.1  index-no-guzzle-2022-05-13.log    index-no-guzzle-2022-05-19.log.1
index-no-guzzle-2022-05-07.log    index-no-guzzle-2022-05-13.log.1

The only result I need is that the files older than N days (here 14) are automatically deleted.

I could remove the timestamp from the filename but I red that using "daily" would mean that logrotate would execute around 6 or 7 AM, which is a problem since our cron tasks can be triggered during the night, meaning the logs would be appended to the wrong file.

So, can I use logrotate for this and if yes, how? And if not, that's the safest way to achieve that, considering I'd like to automate the creation of those config files (a script would create the cron folder and files needed, create the logs folder, create the cron task and create the logrotate config file).

Thank you very much or your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2025-02-06 10:40:41

好吧,在进行了更多的挖掘之后,看起来这样的方法是不使用logrotate,而是使用cron任务来删除旧文件。

使用这个答案对我来说是一个很好的解决方案,我每天都可以使用另一个CRON任务来运行该命令。

所以我的命令是:

/usr/bin/find /root/gun-logs/some-cron-logs-folder/ -mtime +14 -name "*.log" -print -delete

Well, looks like that after some more digging, the best way to handle this is to not use logrotate but a cron task to delete old files.

Using this answer was a good solution for me, I can just run the command every day using another cron task.

So my command would be :

/usr/bin/find /root/gun-logs/some-cron-logs-folder/ -mtime +14 -name "*.log" -print -delete
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文