Linux工具:日志切割【logrotate】

12/31/2023 Linux

目录


参考:


# Linux工具:日志切割【logrotate】


# 一、logrotate的配置


# 1. logrotate定义

logrotate是一个日志文件管理工具,用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到转储作用,可以节省磁盘空间;

logrotate是linux里内置的日志分割工具,若没有可如下安装:

# Fedora、CentOS、RHEL安装方式
yum install logrotate crontabs
1
2
# Debian、Ubuntu 安装方式
apt-get install logrotate cron
1
2

# 2. 配置文件介绍

主要的配置文件:/etc/logrotate.conf

局部的配置文件:/etc/logrotate.d/

 # 查看主配置文件
 cat /etc/logrotate.conf
1
2
# see "man logrotate" for details
# rotate log files weekly //轮转执行频率,此处默认每周执行一次轮转
 weekly                                
# keep 4 weeks worth of backlogs //保留多少个日志文件(轮转几次),此处默认保留4个.就是指定日志文件删除之前轮转的次数;0 指没有备份
 rotate 4                               
# create new (empty) log files after rotating old ones //自动创建新的日志文件,新的日志文件具有和原来的文件相同的权限
 create                                 
# use date as a suffix of the rotated file  //切割后的日志文件以当前日期为结尾,如xxx.log-20131216;如果注释则是按数字递增
 dateext                               
# uncomment this if you want your log files compressed  //是否通过gzip压缩转储后的日志文件,如xxx.log-20131216.gz
 compress                        
# RPM packages drop log rotation information into this directory //将 /etc/logrotate.d/ 目录中的所有文件都加载进来
 include /etc/logrotate.d         
# no packages own wtmp and btmp -- we'll rotate them here //log指定位置;仅针对此目录下所设定的参数
# //这个 wtmp 可记录用户登录系统及系统重启的时间;登录成功的日志
 /var/log/wtmp {  
 	#//每月一次切割
    monthly
    #//指定新建的日志文件权限以及所属用户和组
    create 0664 root utmp 
    #//文件大小超过 1M 后才会切割,因为有 minsize 的参数,因此不见得每个月一定会执行一次喔.要看文件大小
    minsize 1M
    #//只保留一个日志
    rotate 1                        
 }
# //btmp就是记录所有尝试登录,但是登录失败的日志 
 /var/log/btmp {
 	#//在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
    missingok         
    monthly
    create 0600 root utmp
    rotate 1
 }
# system-specific logs may be also be configured here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

/etc/logrotate.d是由/etc/logrotate.conf 所规划出来的目录,建议每个要切割日志的服务, 就可以独自成为一个文件,放置到 /etc/logrotate.d/

# 2.1 其他配置参数详解

compress   通过gzip 压缩转储以后的日志
nocompress不做gzip压缩处理
copytruncate  用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate备份日志文件不过不截断
create mode owner group轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate  不建立新的日志文件
delaycompress   和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress覆盖 delaycompress 选项,转储同时压缩
missingok如果日志丢失,不报错继续滚动下一个日志
errors address专储时的错误信息发送到指定的Email 地址
ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项
notifempty当日志文件为空时,不进行轮转
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail转储时不发送日志文件
olddir directory  转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
sharedscripts运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
daily指定转储周期为每天
weekly 指定转储周期为每周
monthly指定转储周期为每月
rotate count指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
dateext使用当期日期作为命名格式
dateformat .%s配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).
当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过)
size = 5 或 size 5 (>= 5 个字节就转储)
size = 100k 或 size 100k
size = 100M 或 size 100M

# 二、logrotate的运行过程


# 2.1 轮转(rotate)的运行方式

以系统日志/var/log/message做切割来简单说明下:

  1. 第一次执行完rotate(轮转)之后,原本的messages会变成messages.1,而且会制造一个空的messages给系统来储存日志;

  2. 第二次执行之后,messages.1会变成messages.2,而messages会变成messages.1,又造成一个空的messages来储存日志!

  3. 如果仅设定保留2个日志(rotate 2)的话,那么执行第三次时,则messages.3这个档案就会被删除,并由后面的较新的保存日志所取代!

日志究竟轮换几次,这个是根据配置文件中的rotate参数来判定的。


# 2.1.1 轮转方式:系统自动执行

logrotate基于cron来运行,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。

# 查看logrotate自动运行脚本
cat /etc/cron.daily/logrotate   
1
2
#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
1
2
3
4
5
6
7
8

# 2.1.2 轮转方式:手动命令执行

手动强制执行命令如下:

/usr/sbin/logrotate -dvf /etc/logrotate.d/nginx
1

参数详解:

  • -v或--verbose:详细的输出数据。
  • -f或--force:强制转储轮循日志文件。
  • -d或--debug:debug模式,测试配置文件是否有错误。
  • -m或--mail=command:压缩日志后,发送日志到指定邮箱。
  • -s或--state=statefile:使用指定的状态文件。

# 2.1.3 无法自动轮询日志怎么办?

  • 先查看自动执行的脚本有没有问题:cat /etc/cron.daily/logrotate
  • 再查看日志切割的配置文件是否正常:cat /etc/logrotate.d/nginx (以nginx为例)
  • 最后查看预定的cron(若无预定则省略此步)有没有执行,查看cron的日志:cat /var/log/cron

# 2.2 系统自动执行的时机


# 2.2.1 logrotate执行流程

以nginx为例说明:

---> crond服务加载 /etc/cron.d/0hourly

---> 在每小时的01分执行/etc/cront.hourly/0anacron

--->执行anacron

--->根据/etc/anacrontab的配置执行/etc/cron.daily/etc/cron.weekly/etc/cron.monthly

--->执行/etc/cron.daily/下的logrotate脚本

--->执行logrotate

--->根据/etc/logrotate.conf配置执行脚本/etc/logrotate.d/nginx

--->分割nginx日志成功


# 2.2.2 logrotate执行时机

根据logrotate执行流程可以知道:要想知道logrotate什么时候执行日志分割操作,需要关注/etc/anacrontab配置文件。

/etc/anacrontab是使logrotate按时执行脚本的主要配置文件:

# 查看使logrotate按时执行脚本的主要配置文件
cat /etc/anacrontab   
1
2
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 默认的logrotate 一般会在每天的3点05分到3点50分之间执行,真实的延迟时间是RANDOM_DELAY + delay in minutes

  • 此外:如果在3-22这个时间段内服务器处于关机状态,则logrotate会在机器开机5分钟后执行分割日志的操作。

**注意!!!**不建议直接修改此文件,如果想修改logrotate默认的执行时间,可通过crontab执行自定义的logrotate配置文件,但这时间不能将自定义的配置文件放置在/etc/logrotate.d下,应该放置在/etc/logrotate.d下层目录或另外的路径,否则就会执行两遍日志分割的操作,执行时间分别是你在crontab中定义的时间以及anacrontab默认的执行时间即3:05-3:50。


# 2.3 自定义logrotate执行时间

正常的日志分割都是按天进行的,即每个日志只记录0-24点之间的内容,如果是按默认的crontab设定的执行时间那肯定是不行的,具体实现如下:

  1. /etc/logrotate.d创建下层目录,mkdir -p/etc/logrotate.d/nginx 当然也可在非/etc/logrotate.d/下创建此目录,例如我是在 /etc/logrotate.daily.0下创建了nginxLogrotate文件
  2. 移除之前自定义的配置文件,sudo mv /etc/logrotate.d/nginxLogrotate ~/bak/,这样做的作用就是防止执行两次日志分割脚本的操作.
  3. 3: 添加crontab计划任务, sudo echo "59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.daily.0/nginxLogrotate >/dev/null 2>&1" > /etc/crontab 当然也可直接在root用户下执行crontab -e然后添加上面的内容. 如果用非root用户则会报错error: error creating output file /var/lib/logrotate/logrotate.status.tmp: 权限不够
  4. 4: 重启crontab,service crond restart,其实可以不用重启.

**注意!!!**如果crontab中定义的执行时间是0点,则需要在/etc/logrotate.d/nginx/nginxLogrotate里添加上dateyesterday这个选项,否则日志内容记录的是昨天的,而 日志名称格式却是今天的,但crontab中定义的执行时间是23:59这样的则不用添加dateyesterday选项.


# 2.4 crontab和anacron和logrotate的关系详解

三个文件的作用

  • /var/spool/cron/USER_NAME:该文件保存了crontab -e编辑的任务内容,比如:执行 crontab -u root -e,编辑保存后,就会有/var/spool/cron/root 这个文件。
  • /var/spool/anacron/{cron.daily,cron.monthly,cron.weekly}:这三个文件记录了anacron上一次执行的时间(上一天,上一周或上一月),anacron任务执行时,对照这里的时间,决定是否执行anacron任务。
  • /var/lib/logrotate/logrotate.status:该文件记录logrotate执行情况,logrotate参考这个文件来决定是否需要rotate日志。
上次更新时间: 9/25/2024, 1:19:15 AM