3

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?

drewyupdrew
  • 1,549
  • 1
  • 11
  • 16

3 Answers3

4

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
Ben Grimm
  • 4,316
  • 2
  • 15
  • 24
0

Use the lastaction. Is executed once, rotating or not

/opt/backups/backup_data.tar.gz {
    nocompress
    daily
    rotate 7
    extension .tar.gz
    missingok
    postrotate
        /opt/backups/backup.sh
    endscript
}
Shoo Limberger
  • 277
  • 2
  • 11
0

Shoo you are wrong, read the man page below.

you have to use a cron job, like ben said.

   lastaction/endscript
          The lines between lastaction and endscript (both of which must appear on lines by themselves) are executed (using /bin/sh)

once after all log files that match the wildcarded pattern are rotated, after postrotate script is run and only if at least one log is rotated. These directives may only appear inside a log file definition. Whole pattern is passed to the script as first argument. If the script exits with error, just an error message is shown (as this is the last action). See also firstaction.