Logrotate命令教程以及适用于Linux的示例

Logrotate main purpose is to easy administrator of Linux logs. It is very talented tool it can

Logrotate的主要目的是简化Linux日志的管理员。 这是非常有才华的工具

  • Automatic rotation

    自动旋转
  • Compression

    压缩
  • Removal

    清除
  • Mailing Logs

    邮寄日志

Logrotate can be configured and run accordingly with configuration files. We will look all of them in this tutorial in detail.

可以配置Logrotate并使用配置文件相应地运行它。 我们将在本教程中详细介绍所有这些内容。

配置文件 (Configuration File)

Logrotate generally works as a daemon. The logrotate binary is located/usr/sbin/logrotate . The configuration file is located at /etc/logrotate.conf . Here is the default logrotate configuration file content.

Logrotate通常用作守护程序。 logrotate二进制文件位于/usr/sbin/logrotate 。 配置文件位于/etc/logrotate.conf 。 这是默认的logrotate配置文件内容。

# see "man logrotate" for details 
# rotate log files weekly 
weekly 
 
# use the syslog group by default, since this is the owning group 
# of /var/log/syslog. 
su root syslog 
 
# keep 4 weeks worth of backlogs 
rotate 4 
 
# create new (empty) log files after rotating old ones 
create 
 
# uncomment this if you want your log files compressed 
#compress 
 
# packages drop log rotation information into this directory 
include /etc/logrotate.d 
 
# no packages own wtmp, or btmp -- we'll rotate them here 
/var/log/wtmp { 
    missingok 
    monthly 
    create 0664 root utmp 
    rotate 1 
} 
 
/var/log/btmp { 
    missingok 
    monthly 
    create 0660 root utmp 
    rotate 1 
} 
 
# system-specific logs may be configured here

This file is the main configuration. There is also a directory names /etc/logrotate.d which holds multiple logrotate configuration files for multiple and generally different jobs.

此文件是主要配置。 还有一个目录名称/etc/logrotate.d ,其中包含用于多个(通常是不同的)作业的多个logrotate配置文件。

If we list files under logrotate.d we can see there are some configurations about asterisk, dpkg, ufw etc.

如果我们在logrotate.d下列出文件,我们可以看到存在有关星号,dpkg,ufw等的一些配置。

$ ls /etc/logrotate.d/

设置日志文件大小 (Set Log File Size)

The most used configuration is setting when the logs will be rotated as size reach. We will set size parameter with size option and the specifier. In the following example we will set size 5k.

最常用的配置是设置将日志随着大小到达而轮换的时间。 我们将使用size选项和说明符设置size参数。 在下面的示例中,我们将大小设置为5k。

/var/log/auth.log {
    create 700 pof pof
    size 1k
}

We have also used the create expression which will set the archived file permission with user and group name.

我们还使用了create表达式,该表达式将使用用户名和组名设置归档文件的权限。

启用压缩 (Enable Compression)

Another useful feature of the logrotate is compression archived files. Which will make archive files less in size. The compression ratio may change according to compression algorithm like gz,bzip,zip etc.  Default compression algorithm is gzip. In the example we wimply define compression

logrotate的另一个有用功能是压缩存档文件。 这将使存档文件的大小减小。 压缩率可能会根据gz,bzip,zip等压缩算法而变化。默认压缩算法为gzip。 在示例中,我们仅定义了压缩

/var/log/auth.log {
    compress
}

将日期添加到日志文件名 (Add Date To Log File Name)

While looking a lot of log files in a single directory finding log files according date can be very hard. Logrotate provides date mechanism to add log file  date to the end of the log file name. For example if the log file name is auth.log the new file name with date will be auth-20170308.gz

在单个目录中查找大量日志文件时,很难按日期查找日志文件。 Logrotate提供了日期机制,可将日志文件日期添加到日志文件名的末尾。 例如,如果日志文件名是auth.log则带有日期的新文件名将是auth-20170308.gz

This date feature can be enabled like below.

可以按以下方式启用此日期功能。

/var/log/auth.log {
    dateext
}

指定每月/每天/每周的轮换周期 (Specify Rotate Period Monthly/Daily/Weekly)

Rotate period can be specified with different metrics. Specifying as calender date is one way. Logrotate can archive log files in monthly, daily, weekly periods.

可以使用不同的指标指定轮换周期。 指定为日历日期是一种方法。 Logrotate可以按月,每天,每周的时间归档日志文件。

LEARN MORE  How To Use Psexec Tools To Run Commands and Get Shell Remote Windows Systems?
了解更多信息如何使用Psexec工具运行命令并获取Shell远程Windows系统?

每月一次 (Monthly)

/var/log/auth.log {
    monthly
}

每周 (Weekly)

/var/log/auth.log {
    weekly
}

日常 (Daily)

/var/log/auth.log {
    daily
}

旋转后运行脚本 (Run Scripts After Rotate)

After rotating and archiving log files we can trigger some scripts for various jobs. For example I want to get email about log rotation. We can specify script file with postrotate option. The script file must be set as executable to run. In this example after rotation is completed the mailme.sh script will be run.

旋转并归档日志文件后,我们可以触发一些脚本来执行各种作业。 例如,我想获取有关日志轮换的电子邮件。 我们可以使用postrotate选项指定脚本文件。 脚本文件必须设置为可执行文件才能运行。 在此示例中,旋转完成后,将运行mailme.sh脚本。

/var/log/auth.log {
    postrotate /usr/bin/mailme.sh
}

删除旧日志文件 (Remove Old Log Files)

What will be the rotated log file archives? They will grow and grow in time. Then they will fill up whole disk and the server will be down. This was the bad scenario If we do not setup number of logs those will be stored. Logrotate have a mechanism which will remove log file archives other than spefied count. In this example we will only want to store last 5 log archives and remove others. We will use rotate option for this information.

轮换的日志文件档案将是什么? 他们会随着时间的增长而成长。 然后它们将填满整个磁盘,服务器将关闭。 如果我们不设置日志数量,那将是糟糕的情况。 Logrotate具有一种机制,该机制将除去指定计数以外的日志文件存档。 在此示例中,我们只想存储最后5个日志归档文件并删除其他归档文件。 我们将使用rotate选项获取此信息。

/var/log/auth.log {
    rotate 5
}

Another way to remove old archives is setting number of days the log archives will be stored. when a log archive reach specified date it will be deleted. We will use maxage options for this. In the example we will want to remove archive files older than 50 days.

删除旧档案的另一种方法是设置日志档案的存储天数。 当日志归档达到指定日期时,它将被删除。 我们将为此使用maxage选项。 在示例中,我们将要删除超过50天的存档文件。

/var/log/auth.log {
    maxage 50
}

指定压缩命令 (Specify Compression Command)

In previous examples we have set compression. But we have not defined any algorithm and tool to compress. We can specify the compression tool and related parameters different than default. We will use compresscmd and compressext to set compression tool and extension explicitly. In this example we want to use bzip2 for compression.

在前面的示例中,我们设置了压缩率。 但是我们还没有定义任何压缩算法和工具。 我们可以指定不同于默认值的压缩工具和相关参数。 我们将使用compresscmdcompressext显式设置压缩工具和扩展名。 在此示例中,我们要使用bzip2进行压缩。

/var/log/auth.log {
    compress
    compresscmd /bin/bzip2
    compressext .bz2
}
 

翻译自: https://www.poftut.com/logrotate-command-tutorial-with-examples-for-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值