2

Everyday a log file is created in my directory. But with time, there tend to be a lot of log files. I want to keep the latest 4 log files in .log format, and compress the rest and copy the compressed files to backup folder. Normally log files would named as following.

application.log <- the latest log 
application.log.2015-04-16
application.log.2015-04-15
application.log.2015-04-14

another_app.log
another_app.log.2015-04-16
another_app.log.2015-04-15
another_app.log.2015-04-14
another_app.log.2015-04-13

etc.

Following is the script I wrote which I included inside logrotate.conf

But it does not work. Can someone help me with this? I don't want to delete any log files. Only compress old log files and copy those compressed files to a backup folder.

/root/tharidu/log/*.log{
    missingok
    compress
    dateext
    dateformat "YYYY-MM-DD"
    notifempty
    daily
    rotate 4
    postrotate
        cp /root/tharidu/log/*.gz /root/tharidu/backup
    endscript
}
  • I'm not seeing any compressed files in your list. Did you forget to type the .gz part? Esp. because you explicitly state "Normally zip files would named as following" (although .gz files aren't really "zip" files, they're gzipped files). – wurtel Apr 17 '15 at 12:53
  • Make sure your syntax is correct and there is no permission issue. – Baazigar Apr 17 '15 at 14:38
  • I have edited question so that it is more clear. – TV Nath Apr 21 '15 at 9:53

Your Answer

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

Browse other questions tagged or ask your own question.