2

I'm trying to setup a logrotation to rotate the logs daily, for 14 days. In the posst rotate, it will cal a separate script to tar any log older than 10 days. Files older than 14 days will be deleted.

So far, the rotation seems to work fine, but I'm having some trouble testing given the time scale required for this. Is there any way test this setup in way that doesn't involve waiting 14 days, as that is not very practical.

Also, here is my configuration and the tar script. Does anything about it seem wrong, given my requirements?

logrotate.conf

/home/user1/services/logs/*/*.log{
# Rotate logs daily
daily
copytruncate
missingok
notifempty
# Keep 14 days worth of backlogs
rotate 14
maxage 14

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

# use date as a suffix of the rotated file
dateext
dateformat .%Y-%m-%d

# compress your logs
nocompress

#postrotate scripts to tar logs that are too old.
postrotate
    /home/user1/service/logrotate/archive.sh
endscript

}

archive.sh:

#!/bin/bash

rotateDate=[0-9]{4}-[0-9]{2}-[0-9]{2}

shopt -s nullglob

for file in $HOME/services/logs/*/*.log.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
    log=$(basename "$file")
    find . -type f -mtime +10 | tar -zcvf "$file.tar" "$log"
done

Edit: Having run my script overnight, I noticed that archive.sh is not working correctly. Instead of finding older files and taring them, I see that the log file that was rotated last night has been tared.

I should have:

test.log
test.log.2020-04-09
test.log.2020-04-08

and then not see any tars until a file is 10 days old.

Instead I have:

test.log
test.log.2020-04-09
test.log.2020-04-09.tar
test.log.2020-04-08
test.log.2020-04-08.tar
SVill
  • 331
  • 5
  • 22
  • 55

1 Answers1

0

I don't know much about logrotate.conf.


However for your second issue: Please have a look into those zipped-log-files whether they are even the correct ones and do not contain the same content in all of them.

What you currently do is:

For every $file that matches your glob expression:

  • Search for all files that are older than 10 days (not only $file)
  • Discard the result and call tar -zcvf "$file.tar" "$log"

So you should include the filename in your find command and use the output of find as arguments to tar (for example using xargs):

for file in $HOME/services/logs/*/*.log.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
    log=$(basename "$file")
    find . -iname "$file" -type f -mtime +10 | xargs tar -zcvf "$file.tar"
done

If you have spaces in your file names consider the options -print0 for find and -0 for xargs

Or without xargs use the parameters -T - for tar:

find . -iname "$file" -type f -mtime +10 -print0 | tar -zcvf "$file.tar" --null -T  -

However the concrete parameters may depends on your version of tar: How can I build a tar from stdin?

D. Schmidt
  • 147
  • 7