linux日志切割

在linux下的日志会定期进行滚动增加,我们可以在线对正在进行回滚的日志进行指定大小的切割(动态),如果这个日志是静态的。比如没有应用向里面写内容。那么我们也可以用split工具进行切割;其中Logrotate支持按时间和大小来自动切分,以防止日志文件太大。

logrotate配置文件主要有:
/etc/logrotate.conf 以及 /etc/logrotate.d/ 这个子目录下的明细配置文件。

logrotate的执行由crond服务调用的。
logrotate程序每天由cron在指定的时间(/etc/crontab)启动

日志是很大的,如果让日志无限制的记录下去 是一件很可怕的事情,日积月累就有几百兆占用磁盘的空间,如果你要找出某一条可用信息:海底捞针

日志切割:
当日志达到某个特定的大小,我们将日志分类,之前的日志保留一个备份,再产生的日志创建一个同名的文件保存新的日志.

配置文件说明

[root@wangxuejin-test195 ~]# vim /etc/logrotate.conf

说明:(全局参数)

  • weekly : 每周执行回滚,或者说每周执行一次日志回滚
  • rotate: 表示日志切分后历史文件最多保存离现在最近的多少份 [rəʊˈteɪt] 旋转
  • create : 指定新创建的文件的权限与所属主与群组
  • dateext : 使用日期为后缀的回滚文件 #可以去/var/log目录下看看
  • 单独配置信息
/var/log/btmp {                       指定的日志文件的名字和路径
missingok                          如果文件丢失,将不报错
monthly                       每月轮换一次
create 0664 root utmp           设置btmp这个日志文件的权限,属主,属组
minsize 1M        文件超过1M进行回滚,所以大家要知道它不一定每个月都会进行分割,要看这个文件大小来定
rotate 1      日志切分后历史文件最多保存1份,不含当前使用的日志

其它参数说明:

  • monthly: 日志文件将按月轮循。其它可用值为‘daily’,‘weekly’或者‘yearly’。
  • rotate 5: 一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。
  • compress: 在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
  • delaycompress: 总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
  • missingok: 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
  • notifempty: 如果日志文件为空,轮循不会进行。
  • create 644 root root: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件。
  • postrotate/endscript: 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd 进程将立即再次读取其配置并继续运行。
  • /var/lib/logrotate/status中默认记录logrotate上次轮换日志文件的时间。

实战一

使用logrotate进行ssh日志切割

日志存储在/var/log/sshd.log

[root@wangxuejin-test195 ~]# vim /etc/logrotate.d/sshd 
/var/log/sshd.log {
    missingok
    weekly
    create 0600 root root
    minsize 1M
    rotate 3
}

重启rsyslog服务

[root@wangxuejin-test195 ~]# systemctl restart rsyslog

预演,不实际轮询

[[root@wangxuejin-test195 ~]# logrotate -d /etc/logrotate.d/sshd 
reading config file /etc/logrotate.d/sshd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/sshd.log  weekly (3 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/sshd.log
  log does not need rotating (log has been rotated at 2020-6-12 8:0, that is not week ago yet)
  log does not need rotating ('misinze' directive is used and the log size is smaller than the minsize value[

强制轮询,即使轮询条件没有满足也可以通过-f强制轮询日志文件

  • -v 显示指令执行过程
  • -f 强制执行
root@wangxuejin-test195 ~]# llogrotate -vf /etc/logrotate.d/sshd 
reading config file /etc/logrotate.d/sshd
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/sshd.log  forced from command line (3 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/sshd.log
  log needs rotating
rotating log /var/log/sshd.log, log->rotateCount is 3
dateext suffix '-20200612'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/sshd.log.3 to /var/log/sshd.log.4 (rotatecount 3, logstart 1, i 3), 
old log /var/log/sshd.log.3 does not exist
renaming /var/log/sshd.log.2 to /var/log/sshd.log.3 (rotatecount 3, logstart 1, i 2), 
old log /var/log/sshd.log.2 does not exist
renaming /var/log/sshd.log.1 to /var/log/sshd.log.2 (rotatecount 3, logstart 1, i 1), 
old log /var/log/sshd.log.1 does not exist
renaming /var/log/sshd.log.0 to /var/log/sshd.log.1 (rotatecount 3, logstart 1, i 0), 
old log /var/log/sshd.log.0 does not exist
log /var/log/sshd.log.4 doesn't exist -- won't try to dispose of it
fscreate context set to system_u:object_r:var_log_t:s0
renaming /var/log/sshd.log to /var/log/sshd.log.1
creating new /var/log/sshd.log mode = 0600 uid = 0 gid = 0
set default create context

查看日志文件

再次查看原log日志文件大小

sshd日志分割完成

发表评论

后才能评论