Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logrotate: can you run postrotate even if log isn't rotated?

I have a backup script (backup.sh) that ssh's into another machine, compresses a file, and then scp's this file (backup_data.tar.gz) back to my local machine. I also have a logrotate file on my local machine:

/opt/backups/backup_data.tar.gz {
    nocompress
    daily
    rotate 7
    extension .tar.gz
    missingok
    postrotate
        /opt/backups/backup.sh
    endscript
}

Therefore, the first time the logrotate is executed, the log file to rotate isn't there (since the backup.sh script hasn't been executed yet). I have missingok, so it moves on and doesn't throw an error, but it doesn't seem like backup.sh is executed since after the first run, the backup_data.tar.gz still isn't there. I believe postrotate only executes if the file is rotated, is there a way to get around this and have it execute regardless?

like image 335
drewyupdrew Avatar asked Dec 02 '25 08:12

drewyupdrew


1 Answers

logrotate will only run the scripts (prerotate, postrotate, firstaction, lastaction) if at least one log file has been rotated. Bootstrapping the process by running backup.sh once would work, but if it were to fail in the future you would have no new backups until you intervene.

I would create a separate logrotate config file, e.g.

/opt/backups/logrotate.conf:

/opt/backups/backup_data.tar.gz {
    nocompress
    daily
    rotate 7
    extension .tar.gz
    missingok
}

Next modify your backup.sh and insert a call to logrotate at the top of the script:

logrotate --state=/opt/backups/logrotate.state /opt/backups/logrotate.conf

And finally, set backup.sh to run daily e.g. crontab -e/sudo crontab -e -u backupuser:

15 2    * * *   /path/to/backup.sh
like image 73
Ben Grimm Avatar answered Dec 04 '25 00:12

Ben Grimm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!