Koozali.org: home of the SME Server

logrotate Add date to rotated file name.

Offline fastxl

  • *
  • 32
  • +0/-0
logrotate Add date to rotated file name.
« on: September 09, 2016, 06:47:44 PM »
How can I append the date to the filename of a log that I rotate daily? Right now it is only keeping 4 logs and just compressing them. I see other logs are keeping them longer with the date added to the file name. Currently I have a custom service in the /etc/logrotate.d

Thanks,
Eric

Offline Stefano

  • *
  • 10,839
  • +2/-0
Re: logrotate Add date to rotated file name.
« Reply #1 on: September 09, 2016, 07:23:25 PM »
SME doesn't use etc/logrotate.d AFAIK

Offline fastxl

  • *
  • 32
  • +0/-0
Re: logrotate Add date to rotated file name.
« Reply #2 on: September 09, 2016, 07:48:31 PM »
How does it rotate the system logs? I am using it as a syslog server, send info to it from a device. I setup it up based on this
 https://wiki.contribs.org/Syslog
and I am rotating the logs based on this
https://linuxconfig.org/setting-up-logrotate-on-redhat-linux
Now I would just like to add the date to the rotated filename.

Offline mmccarn

  • *
  • 2,627
  • +10/-0
Re: logrotate Add date to rotated file name.
« Reply #3 on: September 10, 2016, 01:11:49 PM »
on SME9, /etc/logrotate.conf includes 'dateext'.  I don't see 'dateext' in /etc/logrotate.conf on SME8

Quote from: man logrotate
       dateext
              Archive  old versions of log files adding a daily extension like
              YYYYMMDD instead of simply adding a number.


I can't find any template for /etc/logrotate.conf -- so you should be able to add 'dateext' there immediately after the 'create' directive.

Quote from: /etc/logrotate.conf on SME 9
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext


# uncomment this if you want your log files compressed
#compress

...


[edit]
I suspect you can get this behavior for any single log by adding 'dateext' immediately after the 'create' command in the associated config file in /etc/logrotate.d

« Last Edit: September 10, 2016, 01:14:20 PM by mmccarn »

Offline fastxl

  • *
  • 32
  • +0/-0
Re: logrotate Add date to rotated file name.
« Reply #4 on: September 23, 2016, 09:05:23 PM »
Here is what I ended up doing.

  • Create a script that copies the file to a network share (or it could be another directory) and adds the date to the file name.
  • Add a "job" to the CRON Daily so that it rotates everyday.

Here is what my script looked like.
Code: [Select]
cp -p /var/log/mylog.1.gz /home/e-smith/files/ibays/test/html/SME-share/mylog.1.`date +%Y.%m.%d_%H.%M.%S`.gz
And for the CRON Jobs I just followed these steps. https://wiki.contribs.org/Cron_entry

Seems to be working for me.