Bash – rotating logs with logrotate

bashlogrotate

I want to schedule logs rotate with these rules:

  1. log files for the last 2 day are kept untouched
  2. log files older than 2 days are archived and moved to folder named by date, they were created. (For ex. All logs for 01 Dec 2012 are moved to 20121201 and archived.)
  3. log files older than 14 days are removed.

I want to use logrotate for this, but I'm not sure it suits my needs or not.

I'd like something like this, but it doesn't work.

/mylog/* {   
prerotate
DIR=$(date +%y%m%d); // actually it's current date
mkdir $DIR
endscript
daily
rotate 2
olddir /mylog/$DIR
missingok
compress
postrotate
       find /mylog -type d -mtime +180 | xargs rm -f
endscript
}

Logrotate does not understand that $DIR is variable.

Any suggestions are welcome!

Best Answer

According to @koniu from superuser, it is not possible. As far as I checked, I didn't find any other ways for that.

For the age of the log, you have a maxage directive which according to the manpage:

   maxage count
          Remove  rotated  logs  older  than <count> days. The age is only
          checked if the logfile is to be rotated. The files are mailed to
          the configured address if maillast and mail are configured.

Looks like it doesn't work on old logs moved in olddir.