
logrotate日志管理
1.参数和配置文件
logrotate [-dv] [-f|--force] [-s|--state statefile] config_file .. 选项 # logrotate --help Usage: logrotate [OPTION...] <configfile> -d, --debug Don't do anything, just test (implies -v) 不做实际处理,仅调试 -f, --force Force file rotation 强制执行,忽视参数要求 -m, --mail=command Command to send mail (instead of `/bin/mail') 发送mail -s, --state=statefile Path of state file 查看状态文件 -v, --verbose Display messages during rotation 轮替一次,并显示轮替过程信息 --version Display version information 显示logrotate版本 Help options: -?, --help Show this help message --usage Display brief usage message配置文件
/etc/logrotate.conf /etc/logrotate.d logrotate.conf是主要的参数文件,至于logrotate.d是一个目录,该目录里面的所有文件都会被logrotate.conf文件主动读入,若logrotate.d/里的文件没有指定详细设置,则以/etc/logrotate.conf这个文件里配置的默认值为准。
2.参数解释
daily 每天轮替一次 weekly 每周轮替一次 monthly 每月轮替一次 yearly 每年轮替一次 rotate 保留几个轮替日志文件 ifempty 不论日志是否空,都进行轮替 notifempty 若日志为空,则不进行轮替 create 旧日志文件轮替后创建新的日志文件 size 日志达到多少后进行rotate minsize 文件容量一定要超过多少后才进行rotate nocompress 轮替但不进行压缩 compress 压缩轮替文件 dateext 轮替旧日志文件时,文件名添加-%Y %m %d形式日期,可用dateformat选项扩展配置。 nodateext 旧日志文件不使用dateext扩展名,后面序数自增如"*.log.1" dateformat 只允许%Y %m %d和%s指定符。注意:系统时钟需要设置到2001-09-09之后,%s才可以正确工作 sharedscripts 作用域下文件存在至少有一个满足轮替条件的时候,执行一次prerotate脚本和postrotate脚本。 prerotate/endscript 在轮替之前执行之间的命令,prerotate与endscript成对出现。 postrotate/endscript 在轮替之后执行之间的命令,postrotate与endscript成对出现。 olddir 将轮替的文件移至指定目录下 missingok 如果日志文件不存在,继续进行下一个操作,不报错示例
举个自定义的例子 #/etc/logrotate.d/test /test/log/*.log{ daily rotate 2 size 1M create compress missingok dateext olddir /test/rotate } 每天对/test/log/目录下后缀为“.log”的且大小超过1MB的文件进行轮替,文件后缀为”-20171124“。压缩轮替后的文件,并将该文件移到”/test/rotate“目录下,一个原日志文件最多仅有2个对应轮替日志文件,若轮替日志大于设定的值则删除之前较早的轮替日志文件。创建新的日志文件。若/test/log/目录下文件名以”.log“结尾的文件不存在,不报错。
3.默认配置文件
# see "man logrotate" for details # rotate log files weekly 每周轮替一次 weekly # keep 4 weeks worth of backlogs 保留4个轮替日志 rotate 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 # 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 /var/log/wtmp { #轮替对象为/var/log/中的wtmp文件 monthly #每个月轮替一次 create 0664 root utmp #创建新的日志文件 权限 所属用户 所属组 minsize 1M #日志大小大于1M后才能参与轮替 rotate 1 #保留一个轮替日志文件 } /var/log/btmp { missingok #如果日志文件不存在,继续进行下一个操作,不报错 monthly create 0600 root utmp rotate 1 } /etc/logrotate.d /etc/logrotate.d如上面说到的,在logrotate配置中扮演一个目录的角色,通过logrotate.conf中的配置include /etc/logrotate.d将/etc/logrotate.d目录包含进来,即将该目录下的文件内容加入到轮替任务中。 这里先简单介绍一下该目录下的文件内容及其格式,后面再对具体案例进行分析。 格式 日志文件路径 ...{ #多个文件绝对路径路径可以用空格、换行分隔, 参数配置 } /etc/logrotate.d/syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler #作用域:/var/log/目录下的cron、maillog、messages、secure和spooler文件 { missingok sharedscripts #作用域下文件存在至少有一个满足轮替条件的时候,完成轮替后执行一次postrotate内的脚本。 postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript }
4.secure线上配置
cat >> /etc/logrotate.d/secure <<EOF /var/log/secure { missingok daily copytruncate rotate 7 minsize 50M create 644 root root dateext dateformat -%Y%m%d_%H:%m:%s notifempty } EOF
5.一天周期不能满足要求
logrotate提供的轮替周期参数只能精确到天,以一天为轮替周期在一些情况下并不能满足我们的要求,可以搭配crontab(例行性工作),通过定时任务来执行logrotate命令,从而在不同情况实现对logrotate任务的自定义频率调用。
例行性工作的简单配置使用
cron执行时会读取/etc/cron.d这个目录的所有文件,按照文件中的设置来定时执行任务。该目录下新增的文件后,无需再重启crond服务。这里只简单地介绍该种定时任务配置。格式
#格式 *(分钟) *(小时) *(天) *(月) *(周几) 用户 命令 # 若分钟位值为 *,表示0-59之间的任意有效值; # 若分钟位值为 1,表示每小时的第1分钟; # 若分钟位值为 */5,表示每5分钟 # 若分钟位值为10,20 表示每小时的第10分钟和第20分钟 # 若分钟位值为10-12 表示每小时的第10、11、12分钟 #/etc/cron.d/cron_test * */1 * * * root /usr/sbin/logrotate -v /etc/logrotate.d/test 每小时root用户会调用logrotate读取/etc/logrotate.d/test配置,执行一次。示例
# rotate log files monthly monthly # keep 4 weeks worth of backlogs rotate 12 # create new (empty) log files after rotating old ones create # use date as a suffix of the rotated file dateext # 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 rotate 12 } /var/log/btmp { monthly create 0600 root utmp rotate 12 } /var/log/messages { monthly create 0600 root utmp rotate 12 } /var/log/secure { monthly create 0600 root utmp rotate 12 } /var/log/maillog { monthly create 0600 root utmp rotate 12 } # system-specific logs may be also be configured here. 下面自己定义文件 在 crontab 定时每周一强制转存 #/etc/logrotate.d/log_cron (文件名log_cron) * * * * 1 /usr/sbin/logrotate -fv /etc/logrotate.d/log_cron /var/log/wtmp { monthly dateext create 0664 root utmp rotate 12 } /var/log/btmp { monthly dateext create 0600 root utmp rotate 12 } /var/log/messages { monthly dateext create 0600 root utmp rotate 12 } /var/log/secure { monthly dateext create 0600 root utmp rotate 12 } /var/log/maillog { monthly dateext create 0600 root utmp rotate 12 }