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