linux日志转储:logrotate

来源:互联网 发布:时代网络 12岁 编辑:程序博客网 时间:2024/05/19 00:42

背景:

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

Logrotate使用

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

    root@gds-VirtualBox:/home/gds/misc# logrotatelogrotate 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添加配置文件

    root@gds-VirtualBox:/etc/logrotate.d# pwd/etc/logrotate.droot@gds-VirtualBox:/etc/logrotate.d# vim log-filehome/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,查看是否转储

    运行配置文件root@gds-VirtualBox:/etc/logrotate.d# logrotate /etc/logrotate.d/log-fileroot@gds-VirtualBox:/etc/logrotate.d# logrotate -vf /etc/logrotate.d/log-filereading config file /etc/logrotate.d/log-fileHandling 1 logsrotating pattern: /home/gds/misc/log-file  forced from command line (5 rotations)empty log files are rotated, old logs are removedswitching euid to 0 and egid to 38considering log /home/gds/misc/log-file  log needs rotatingrotating log /home/gds/misc/log-file, log->rotateCount is 5dateext 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 faileddestination /home/gds/misc/log-file-20170721 already exists, skipping rotationswitching euid to 0 and egid to 0查看是否转储root@gds-VirtualBox:/home/gds/misc# lslog-file  log-file-20170721

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

原创粉丝点击