1

I want to rotate a file and place the rotated file inside a new directory called 'backup'. The rotation of the log is working fine where the rotated file is not getting moved inside the backup directory. Please find below the script I have used.

Note: only the recently rotated file which is compressed to .gz format is not getting moved inside the backup folder, where as if you keep any other zip file with .gz format it is moved.

Script --

compress    

/data_gpfs/engageone/active-drive/E-Archive/SystemData/logrotation_test/test.log {
        daily
        copytruncate
        create 775 root wsdepl
        dateext
     dateformat %Y-%m-%d
        rotate 30
     maxage 30
        missingok
        #notifempty 
     sharedscripts
     postrotate
        mv /data_gpfs/engageone/active-drive/E-Archive/SystemData/logrotation_test/*.gz /data_gpfs/engageone/active-drive/E-Archive/SystemData/logrotation_test/backup
     endscript
}

1 Answer 1

2

From logrotate(8):

It allows automatic rotation, compression, removal, and mailing of log files. [...] The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated.

In other words: postrotate happens after rotation, but before compression. Your log's gzip archive doesn't exist yet when your mv call is made.

logrotate does not provide a postcompress hook, however you can:

  1. Use the lastaction hook instead.
  2. Handle the compression in your postrotate script/code. Create a shell script which zips the file, then moves it, and call it in your postrotate section. Don't forget to disable the compress option in that case.
  3. Do a similar thing using compresscmd and uncompresscmd, using custom compression/decompression scripts which take care of locating and moving the file when necessary.
  4. Take care of the moving in a CRON job independent from logrotate, or schedule it in the postrotate section using at (this isn't really clean, since you can't really predict the compression time).

For more information, have a look here. logrotate's workflow might be a little tricky to understand at first, but once you get a hold of the steps it takes, everything should go smoothly ;) You might also be interested in the following SE questions:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .