Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script for archiving log files

Tags:

linux

bash

unix

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.

like image 497
user1825465 Avatar asked Nov 15 '12 02:11

user1825465


1 Answers

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
}
like image 111
Gilles Quenot Avatar answered Sep 24 '22 18:09

Gilles Quenot