I have logs in the following format: YYYYMMDD
I want to compress old logs (older then current day) and maybe move them to a different directory afterwards.
Can I do this in logrotate, or do I have to use a custom script in cron?
logrotate can do it with olddir if your log file name is the same every time it runs and you can add dates. If your log file name changes i.e. YYYYMMDD then logrotate won't do it for you.
# sample logrotate conf file copytruncate compress dateformat %Y%m%d. dateext extension log olddir ./logarchive /logs/sys.log { rotate 7 daily } Copies and gzips /logs/sys.log to /logs/logarchive/sys.20120101.log.gz, keeps one week worth of logs.
Here's a quickie script which will do what you need:
#!/bin/bash LOGDIR=/var/log/somedir OLDLOGS=/var/log/keep-old-logs-here PATH=/bin:$PATH TODAY=$(date +'%Y%m%d') [ -d $OLDLOGS ] || mkdir -p $OLDLOGS cd $LOGDIR for LOG in $(ls | egrep '^[[:digit:]]{8}$'); do [ $LOG -lt $TODAY ] && gzip $LOG && mv $LOG.gz done Make the script executable:
$ chmod +x /where/you/put/this/script The crontab entry will look like:
30 0 * * * /where/you/put/this/script Just adjust LOGDIR and OLDLOGDIR. At 12:30am it will move all logs in the format of YYYYMMDD for the previous (and earlier, if any) days.
nodateext, olddir, compress, and daily options. logrotate itself does not do this. I'd recommend writing a supplementary script and invoking it from logrotate using the postrotate option in the configuration.