2

How can I rotate all the log files older 30 days in a unique file.tar.gz and remove the files inserted in the archive?

Before:

a.log // older than 30days
b.log // older than 30days
c.log // older than 20 days

After:

backup.tgz
c.log
0

You can do the following:

find . -name '*log' -mtime +30 -print0 | tar -czvf backup.tgz --remove-files --null -T -

With:

  • find options:
    • -name '*log' -mtime +30: selects log files older than 30 days
    • -print0: print the full file name on the standard output, followed by a null character
  • tar options:
    • --remove-files: remove files after adding them to the archive
    • --null: reads null-terminated names (sent by find -print0)
    • -T -: reads filenames from stdin (find's output)
1

If you are using the Linux logger and you can add a rotation config to the /etc/logrotate.d directory in which you can specify the size, and format, etc...

Example:

/var/log/test.log {
    rotate 30
    weekly
    size=30k
    compress 
}

Here is a cheat sheet for the scripts.

  • Hi, thanks for the answer, but is this method i don't have a unique file compress, but many single compress file for any log. I've need that every log is compress in unique tar for all. – Luca Jun 30 '17 at 7:08

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.