logrotate multiple files via prerotate script

171 Views Asked by At

I have "ansible jobs log DIR" /var/log/ansible where are stored all ansible logs.

e.g.

# ll
total 35688
-rw-r--r--. 1 root root     5874 May 16 07:28 ansible_20230516_072619.log
-rw-r--r--. 1 root root     2937 May 16 07:35 ansible_20230516_073516.log
-rw-r--r--. 1 root root     2937 May 16 07:37 ansible_20230516_073654.log
-rw-r--r--. 1 root root     2937 May 16 07:41 ansible_20230516_074104.log
...
-rw-r--r--. 1 root root    11476 Jun 27 11:14 ansible_20230627_111401.log
-rw-r--r--. 1 root root    33871 Jun 27 11:26 ansible_20230627_111442.log
-rw-r--r--. 1 root root    61888 Jun 27 11:57 ansible_20230627_115531.log

I would like to do housekeeping via logrotate : Because there are too many "small" log files, I would like to compress all files older then 1 month. So I used prerotate script to move files before rotate to folder /var/log/ansible/archive. But logrotate not works. Goal is to have one compressed file for each month. This file will contain all logs for particular month.

Here is my conf

/var/log/ansible/archive/*.log {
    missingok
    notifempty
    compress
    delaycompress
    monthly
    rotate 30
    maxage 30
    prerotate
        find /var/log/ansible/ -name "*.log" -type f -mtime +30 -exec mv {} /var/log/ansible/archive/ \;
    endscript
}

Output:

logrotate -df /etc/logrotate.d/ansible
Creating new state

Handling 1 logs

rotating pattern: /var/log/ansible/archive/*.log  forced from command line (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ansible/archive/*.log
  log /var/log/ansible/archive/*.log does not exist -- skipping
Creating new state

//UPDATE//

OK, I removed prerotate script, looks it started works something, but I cant see any rotated files. Also as I mentioned earlier, my goal was to rotate / zip all 1 month old files in one file, not each file separately, therefore I used this prerotate script before.

Creating new state

Handling 1 logs

rotating pattern: /var/log/ansible/*.log  forced from command line (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ansible/ansible_20230516_072619.log
Creating new state
  Now: 2023-07-24 13:16
  Last rotated at 2023-07-24 13:00
  log needs rotating
...
renaming /var/log/ansible/ansible_20230627_115531.log.1.gz to /var/log/ansible/ansible_20230627_115531.log.2.gz (rotatecount 30, logstart 1, i 1),
renaming /var/log/ansible/ansible_20230627_115531.log.0.gz to /var/log/ansible/ansible_20230627_115531.log.1.gz (rotatecount 30, logstart 1, i 0),
log /var/log/ansible/ansible_20230627_115531.log.31.gz doesn't exist -- won't try to dispose of it
set default create context to unconfined_u:object_r:var_log_t:s0
renaming /var/log/ansible/ansible_20230627_115531.log to /var/log/ansible/ansible_20230627_115531.log.1
compressing log with: /bin/gzip
# logrotate.d]#

...

ll /var/log/ansible/ansible_20230627_115531.log.1.gz
ls: cannot access '/var/log/ansible/ansible_20230627_115531.log.1.gz': No such file or directory

1

There are 1 best solutions below

0
eniel On

It looks like you just have to remove your prerotate script and the delaycompress keyword;
logrotate already compresses your files because of the compress keyword

If you look in /etc/logrotate.d you will find a number of scripts for logrotate.
In /var/log you see their result.
Looking at these you can easily adapt your config file