zoukankan      html  css  js  c++  java
  • logrotate日志转储

    简单点的方法:

    写个定时任务:

    cd /root/online
    zip log.out.$date.zip log.out
    > log.out
    #find ./ -name *.zip -ctime +7 | xargs rm -f

     

    1 工具目录

    ***系统开启selinux,logrotate会不生效***

    linux默认会安装logrotate工具,自身的boot.log就是通过它分割转储的。

    [root@webmaster log]# ll| grep boot 
    -rw-------. 1 root   root         0 Jun 25 03:33 boot.log
    -rw-------. 1 root   root     81499 Jun 23 03:20 boot.log-20180623
    -rw-------. 1 root   root     28340 Jun 24 03:38 boot.log-20180624
    -rw-------. 1 root   root         0 Jun 25 03:33 boot.log-20180625

    主配置文件为/etc/logrotate.conf,还有个目录/etc/logrotate.d/。系统会按应用,在这个目录中创建转储配置文件。

    -rw-r--r--  1 root root 160 Jan 31  2017 chrony
    -rw-r--r--  1 root root 194 Oct 20  2017 httpd
    -rw-r--r--  1 root root 868 Feb  4 10:33 mysql
    -rw-r--r--  1 root root 351 Oct 17  2017 nginx
    -rw-r--r--  1 root root 203 Mar  7 21:37 php-fpm
    -rw-r--r--. 1 root root 136 Jun 10  2014 ppp
    -rw-r--r--  1 root root 224 May 10  2017 syslog
    -rw-r--r--  1 root root 100 Oct 18  2017 wpa_supplicant
    -rw-r--r--  1 root root 100 Jan 30 21:51 yum
    

      

    2 转储配置

    2.1 nginx

    [root@webmaster logrotate.d]# cat nginx 
    /app/nginx/logs/*.log{
            daily
            missingok
            rotate 30
            compress
            delaycompress
            notifempty
            dateext
            sharedscripts
            postrotate
                    if [ -f /app/nginx/logs/nginx.pid ]; then
                            kill -USR1 `cat /app/nginx/logs/nginx.pid`
                    fi
            endscript
    }
    

      

    /app/nginx/logs/*.log
    #文件匹配
    daliy    按天分割

    missingok  日志为空则不转储
    rotate 30  转储周期30天,过期日志自动删除
    compress  通过gz格式压缩, 文件名如:jab.com.log-20180625.gz
    dateext  文件名按日期命令,如jab.com.log-20180625.gz
    postrotate  转储后执行命令,这个是给nginx发送reopen log file信号。

    3 logrotate命令

    [root@webmaster logs]# logrotate -?
    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')
      -s, --state=statefile     Path of state file
      -v, --verbose             Display messages during rotation
      -l, --log=STRING          Log file
      --version                 Display version information
    
    Help options:
      -?, --help                Show this help message
      --usage                   Display brief usage message
    

      

    在写完一个配置文件后,要用debug检查一下,然后用verbose或者force切割试一下。

    [root@webmaster logrotate.d]# logrotate -d /etc/logrotate.d/nginx  
    reading config file /etc/logrotate.d/nginx
    Allocating hash table for state file, size 15360 B
    
    Handling 1 logs
    
    rotating pattern: /app/nginx/logs/*.log after 1 days (30 rotations)
    empty log files are not rotated, old logs are removed
    considering log /app/nginx/logs/error.log
      log does not need rotating (log has been already rotated)considering log /app/nginx/logs/jab.com.log
      log does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated
    
    [root@webmaster logrotate.d]# logrotate -f /etc/logrotate.d/nginx
    

      

     --end:所有参数参考--


    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

  • 相关阅读:
    nyoj163 Phone List
    hdu1251统计难题
    hdu1754 I Hate It
    nyoj123 士兵杀敌(四)
    poj3468 A Simple Problem with Integers
    zoj1610 Count the Colors
    nyoj144 小珂的苦恼
    nyoj93 汉诺塔(三)
    poj2182 Lost Cows
    ASP.NET2.0中的Callback机制
  • 原文地址:https://www.cnblogs.com/jabbok/p/9223538.html
Copyright © 2011-2022 走看看