2

I have a folder /home/usr/logs/ which contain log files which are older than 1 day. I wish to compress all the log files older than one day to separate compressed archives (e.g. zip or tar.gz) and move them to the folder /home/usr/logs/archive.

The log files are on naming formats, such as valid.app5s.log.1019, app5s.gf3sts.1019, valid.app5s.gf3log.1019, app5s.gf3log.1019, app5s.gf1sts.1019, valid.app5s.gf1log.1019, app5s.sts.1019.

I tried like this:

find .  -mtime +1 -exec zip filename.zip '{}' + && mv filename.zip archive/ \;

But those files are not zipped properly. Will anyone please help me in figuring out the mistake my code?

phk
  • 5,953
  • 7
  • 42
  • 71
Satya
  • 21

1 Answers1

4

logrotate can do this, the pattern match can be for *log, then you add a section called "postrotate", consider postrotate a small bash script that runs after rotation.

example;

/home/usr/logs/*.log {
    daily
    missingok
    compress
    #delaycompress (this will prevent compressing of one day)
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
                mv /home/usr/logs/*.gz /home/usr/logs/archive/;
    endscript
}

UPDATE1:

if you want to use zip instead of gzip, add;

compresscmd /usr/bin/zip
compressoptions -r
compressext .zip
mikejonesey
  • 2,030
  • Thanks mike. But my problem is how to zip each log files with different xtension in name. – Satya Oct 26 '16 at 10:24
  • if you mean zip instead of gzip i've added an update, if you mean your src files have a different extension, just change the pattern match from .log to whatever your files have. for example /home/usr/logs/.log.gz, it also supports using multiple patterns. – mikejonesey Oct 26 '16 at 10:28