I am trying to write a script which will rename and archive log files but I just can't figure it out. Here is an example of how it should work: If you have a file named error_log and you run your script for a first time it should rename the file error_log to error_log.1 and then archive error_log.1 with gzip. The second time you run your script you will have two files: error_log and error_log.1.gz, now you should rename error_log.1.gz to error_log.2.gz; error_log to error_log.1 and once again archive error_log.1 to error_log.1.gz with gzip.
What you are looking for is named logrotate, it's a basic Unix administration command to rotate the logs like you'd like.
See  man 8 logrotate
Example of a simple configuration file :
/var/log/apache/error_log {
        daily
        rotate 90       # keep only 90 logs
        copytruncate    # don't stop apps, but copy log
        compress        # gzip log in *.gz
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With