Search code examples
dateloggingcrondebianlogrotate

Rotating files with datetime already in the filename


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!


Solution

  • 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