我是靠谱客的博主 喜悦面包,这篇文章主要介绍linux日志转储:logrotate,现在分享给大家,希望可以做个参考。

背景:

日志文件在排障过程中或者系统性能分析时经常被用到。对于忙碌的服务器,日志文件大小会增长极快,磁盘空间被消耗很快;操作单个过大日志文件也十分困难,可能会打不开等。因此需要管理日志文件,Linux系统中有一个自带的日志管理文件工具:logrotate.
logrotate工具对于防止因庞大的日志文件而耗尽存储空间是十分有用的。配置完毕后,进程是全自动的,可以长时间在不需要人为干预下运行。

Logrotate使用

  • 查看系统中是否存在logrotate工具

    复制代码
    1
    2
    3
    4
    5
    root@gds-VirtualBox:/home/gds/misc# logrotate logrotate 3.8.7 - Copyright (C) 1995-2001 Red Hat, Inc. This may be freely redistributed under the terms of the GNU Public License 用法: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command] [-s|--state=statefile] [-v|--verbose] [--version] [-?|--help] [--usage] [OPTION...] <configfile>
  • 配置文件参数说明
    参数 功能
    compress 通过gzip 压缩转储以后的日志
    nocompress 不需要压缩时,用这个参数
    copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
    nocopytruncate 备份日志文件但是不截断
    create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
    nocreate 不建立新的日志文件
    delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
    nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
    errors address 专储时的错误信息发送到指定的Email 地址
    ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
    notifempty 如果是空文件的话,不转储
    mail address 把转储的日志文件发送到指定的E-mail 地址
    nomail 转储时不发送日志文件
    olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
    noolddir 转储后的日志文件和当前日志文件放在同一个目录下
    prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
    postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
    daily 指定转储周期为每天
    weekly 指定转储周期为每周
    monthly 指定转储周期为每月
    rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
    tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
    size size 当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).

  • 为特定日志文件添加配置文件

    1、logrotate的配置文件可以在/etc/logrotate.conf(logrotate.conf会include logrotate.d中的配置文件)
    2、或/etc/logrotate.d/中添加,下面我们在/etc/logrotate.d/目录中为/home/gds/misc/log-file添加配置文件

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    root@gds-VirtualBox:/etc/logrotate.d# pwd /etc/logrotate.d root@gds-VirtualBox:/etc/logrotate.d# vim log-file home/gds/misc/log-file { su root list daily size = 10M rotate 5 dateext compress delaycompress missingok #notifempty create 644 root root postrotate /usr/bin/killall -HUP rsyslogd endscript }
  • 手动运行logrotate,查看是否转储

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    运行配置文件 root@gds-VirtualBox:/etc/logrotate.d# logrotate /etc/logrotate.d/log-file root@gds-VirtualBox:/etc/logrotate.d# logrotate -vf /etc/logrotate.d/log-file reading config file /etc/logrotate.d/log-file Handling 1 logs rotating pattern: /home/gds/misc/log-file forced from command line (5 rotations) empty log files are rotated, old logs are removed switching euid to 0 and egid to 38 considering log /home/gds/misc/log-file log needs rotating rotating log /home/gds/misc/log-file, log->rotateCount is 5 dateext suffix '-20170721' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' glob finding logs to compress failed destination /home/gds/misc/log-file-20170721 already exists, skipping rotation switching euid to 0 and egid to 0 查看是否转储 root@gds-VirtualBox:/home/gds/misc# ls log-file log-file-20170721

    通过查看我们自己的log-file文件已经转储。

最后

以上就是喜悦面包最近收集整理的关于linux日志转储:logrotate的全部内容,更多相关linux日志转储内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部