3

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

2 Answers 2

3

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.

1
  • 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, 2017 at 7:08
1

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)
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .