Compress old log files and move to new directory

5,864

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
Share:
5,864

Related videos on Youtube

Satya
Author by

Satya

Updated on September 18, 2022

Comments

  • Satya
    Satya over 1 year

    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?