Search code examples
linuxdatelogginglogrotate

How to use date and time as part of filename on Logrotate?


I need to use time and date as an extension of rotated logs. Right now I'm using dateext, but the problem is that I can't achieve rotation more than once on same date/day and I have to do rotation on hourly basis

Here is the logorotate configuration that I created:

/someDirectory/logs/*.log {
    nocompress
    notifempty
    copytruncate
    size 100M
    dateext
    olddir someDirectory/logs/archived
    rotate 10
}

What am I missing?


Solution

  • You can use postrotate script like:

       someDirectory/logs/*.log /someDirectory/logs/*.log {
       nocompress
       notifempty
       copytruncate
       size 100M
       dateext
       olddir someDirectory/logs/archived
       rotate 10
       sharedscripts
       postrotate
            day=$(date +%Y%m%d)
            daytime=$(date +%Y%m%d-%H:%M:%S)
            mv somelog-$day /var/log/somelog-$daytime
       endscript
      }