用于归档日志文件的Bash脚本

时间:2021-03-06 22:08:36

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.

我正在尝试编写一个脚本,它将重命名和存档日志文件,但我无法弄明白。下面是一个如何工作的示例:如果您有一个名为error_log的文件,并且您第一次运行脚本,则应将文件error_log重命名为error_log.1,然后使用gzip将error_log.1归档。第二次运行脚本时,您将有两个文件:error_log和error_log.1.gz,现在您应该将error_log.1.gz重命名为error_log.2.gz; error_log到error_log.1并再次使用gzip将error_log.1归档到error_log.1.gz。

1 个解决方案

#1


7  

What you are looking for is named logrotate, it's a basic Unix administration command to rotate the logs like you'd like.

您正在寻找的是名为logrotate,它是一个基本的Unix管理命令,可以像您一样旋转日志。

See man 8 logrotate

见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
}

#1


7  

What you are looking for is named logrotate, it's a basic Unix administration command to rotate the logs like you'd like.

您正在寻找的是名为logrotate,它是一个基本的Unix管理命令,可以像您一样旋转日志。

See man 8 logrotate

见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
}