1

Let me start by saying I'm very new to linux and bash scripting, only been at this for 2 weeks so assume I know nothing!

For development reasons, I need to copy a log file to another location and name, here's my logrotate file:

/pub/share/logs/results.txt {
    daily
    rotate 30
    dateext
    compress
    delaycompress
    missingok
    notifempty
    postrotate
        sh /pub/share/scripts/result_rotate.sh $1 > /dev/null
    endscript
}

And the bash script is simply:

#!/bin/bash

date=$(date +%Y%m%d)
cp $1-$date /pub/share/reports/data.log

I don't know if I have to put this in a script file or if I can just type that inside the postrotate block, but for now this works.

It passes the name of the file to be rotated but does so after it's already renamed it with the date extension, hence the date code above. Is this a safe way to do this? Could there ever be an instance where it might fail to match up properly?

0

Yes, why not, you can directly write in postrotate block:

cp $1-$(date +%Y%m%d) /pub/share/reports/data.log >/dev/null

in a worse situation where logrotation happened for example with today's date and postrotate posted to run in next day's date, so our cp command will not get that file to copy. for that you can get one day older file always instead of today's date rotated file:

cp $1-$(date -d'-1day' +%Y%m%d) /pub/share/reports/data.log >/dev/null

or of course you can copy those both files, meaning today's date and yesterday's date files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.