logrotate日志轮替

 

logrotate日志切割:
1.防止日志文件过大
2.定期删除旧日志文件

配置文件:rpm -qc logrotate 文件如下
/etc/cron.daily/logrotate
/etc/logrotate.conf
/etc/rwtab.d/logrotate
/var/lib/logrotate/logrotate.status

查轮滚策略帮助手册:man logrotate.conf
配置文件内容:cat /etc/logrotate.conf
:此配置文件主要定义日志文件切割(轮滚轮转滚动)的策略方案.
# see "man logrotate" for details
# rotate log files weekly
weekly 每周一轮滚

# keep 4 weeks worth of backlogs
rotate 4 保留4个备份

# create new (empty) log files after rotating old ones
create 创建新的空日志文件代替旧文件

# use date as a suffix of the rotated file
dateext 使用日志为文件名后缀,禁用此项时默认以数字为后缀.

# uncomment this if you want your log files compressed
#compress 是否压缩(后缀为.gz)

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d  包含指定的目录,此目录下保存日志策略

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp { 日志文件路径及其个性化轮转策略
 monthly 每月一轮滚
 create 0664 root utmp 创建的新日志文件权限属主 、属组
 minsize 1M 文件最小容量
 rotate 1 保留1个备份
}

/var/log/btmp {
 missingok 丢了也OK,即不会报错
 monthly
 create 0600 root utmp
 rotate 1
}

# system-specific logs may be also be configured here.

-----------------
强制测试轮滚: logrotate -fv /etc/logrotate.conf ; ls /var/log/
:1.-f是强制轮滚,-v显示过程
 2.日志文件名后缀的数字越大,文件越旧.日志清理时是清理旧文件.

练习:/var/log/my.log日志文件创建轮滚策略,每天一轮滚,文件丢了也不报错,保留2个备份,启用压缩功能,用数字作为文件名后缀.
vim /etc/logrotate.d/my  添加如下内容
/var/log/my.log {
daily 每天一轮滚
missingok 丢了也不报错
nodateext 不使用日期为后缀,即用数字为后缀
create 创建新文件
rotate 2 保留2个备份
compress 启用压缩(后缀为.gz)
}

测试轮滚:  logrotate -fv /etc/logrotate.d/my ;
查看日志文件列表: ls /var/log/my*