Compress old log file into single zip-linux

38,038

Solution 1

Create tar.gz files older than one day logs

find /home/testuser/log/ -mtime +1 | xargs  tar -czvPf  /opt/older_log_$(date +%F).tar.gz

Delete older files [ Note:- if below find output is Correct then remove echo , after that it will delete those files]

find /home/testuser/ -mtime +1 | xargs  -n1 echo rm

Solution 2

Extending sr_'s comment, I'm using logrotate to housekeep a nightly SQL dump. I have this configuration in /etc/logrotate.d/mydbdump:

/var/backups/dump.sql {
        daily
        rotate 7
        missingok
        create 640 root root
        compress
}

It basically takes the /var/backups/dump.sql file (you would specify the name of your logfile instead), compresses it and renames it to dump.sql.1.gz. Before that, it rotates the old dump.sql.1.gz to dump.sql.2.gz and so on, and deletes the oldest one.

logrotate itself is usually called by cron, so have a look at your /etc/cron* directories to control when it's run.

Share:
38,038

Related videos on Youtube

Ajo Mathew
Author by

Ajo Mathew

Updated on September 18, 2022

Comments

  • Ajo Mathew
    Ajo Mathew over 1 year

    I have a folder /home/testuser/log which contain log files of one day old *.log. I wish to compress all the log files older than one day to a single zip(gzip or tar.gz) and delete the older files.

    I tried to pipeline find and tar commands but didn't work

    • sr_
      sr_ over 11 years
      If you need to do this more than once, logrotate is your friend
    • Bernhard
      Bernhard over 11 years
      @sr_ Maybe you can extend it in an answer?
    • Ajo Mathew
      Ajo Mathew over 11 years
      I am planning to put a job in cron tab to do this. I compress-->Move to another location. This is what I am planning to do.. but not able to compress all files into one zip/gz
    • Rahul Patil
      Rahul Patil over 11 years
      what do you mean by " delete the older files." , you want compress then delete those same files which are compressed ?
  • gertvdijk
    gertvdijk over 11 years
    Great suggestion, yet it doesn't meet the "to a single zip" requirement of the OP yet.
  • Ajo Mathew
    Ajo Mathew over 11 years
    I have few log files log1.log log2.log my idea was to take created date of files if(created date=current date-1) compress all, move to new location, Delete older files
  • domsom
    domsom over 11 years
    @b00tbu9 logrotate does this for you. If you look at your /var/log dir and notice the *.#.gz files you'll see its results.
  • domsom
    domsom over 11 years
    @gertvdijk if the single zip file is a requirement, I'd suggest using the prerotate/postrotate options to manually merge the input/output files.
  • Paul Calabro
    Paul Calabro almost 10 years
    For the second part, you can simply type: find /home/testuser/ -mtime +1 -delete