1.简介

rsyslogd 利用的是 daemon 的方式来启动的, 当有需求的时候立刻就会被执行的,但是logrotate 却是在规定 的时间到了之后才来进行登录文件的轮替, 所以这个 logrotate 程序是挂在 cron 下的
/etc/cron.daily/logrotate 就是记录了每天要进行的登录文件轮替的行为

2.配置文件

/etc/logrotate.conf
/etc/logrotate.d
那个 logrotate.conf 才是主要的参数文件,至于 logrotate.d 是一个目录, 该目录里 面的所有文件都会被主动的读入 /etc/logrotate.conf 当中来进行!另外,在 /etc/logrotate.d/ 里面的文件中,如果没有规定到的一些细部设置,则以 /etc/logrotate.conf 这个文件的规定 来指定为默认值!

/etc/logrotate.conf

  1. [root@study ~]# vim /etc/logrotate.conf
  2. # 下面的设置是 "logrotate 的默认设置值" ,如果个别的文件设置了其他的参数,
  3. # 则将以个别的文件设置为主,若该文件没有设置到的参数则以这个文件的内容为默认值!
  4. weekly <==默认每个礼拜对登录文件进行一次 rotate 的工作
  5. rotate 4 <==保留几个登录文件呢?默认是保留四个!
  6. create <==由于登录文件被更名,因此创建一个新的来继续储存之意!
  7. dateext <==就是这个设置值!可以让被轮替的文件名称加上日期作为文件名喔!
  8. #compress <==被更动的登录文件是否需要压缩?如果登录文件太大则可考虑此参数启动
  9. include /etc/logrotate.d
  10. # 将 /etc/logrotate.d/ 这个目录中的所有文件都读进来执行 rotate 的工作!
  11. /var/log/wtmp { <==仅针对 /var/log/wtmp 所设置的参数
  12. monthly <==每个月一次,取代每周!
  13. create 0664 root utmp <==指定新建文件的权限与所属帐号/群组
  14. minsize 1M <==文件大小一定要超过 1M 后才进行 rotate (略过时间参数)
  15. rotate 1 <==仅保留一个,亦即仅有 wtmp.1 保留而已。
  16. }
  17. # 这个 wtmp 可记录登陆者与系统重新开机时的时间与来源主机及登陆期间的时间。
  18. # 由于具有 minsize 的参数,因此不见得每个月一定会进行一次喔!要看文件大小。
  19. # 由于仅保留一个登录文件而已,不满意的话可以将他改成 rotate 5 吧!

语法格式
登录文件的绝对路径文件名 ``... {
个别的参数设置值,如 ``monthly, compress ``等等
}

/etc/logrotate.d

[root@study ~]# vim /etc/logrotate.d/syslog 
/var/log/cron 
/var/log/maillog 
/var/log/messages 
/var/log/secure 
/var/log/spooler 
{ 
sharedscripts 
postrotate 
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true 
endscript 
} 

各个命令的详细解释:
文件名:被处理的登录文件绝对路径文件名写在前面,可以使用空白字符分隔多个登 
录文件; 
参数:上述文件名进行轮替的参数使用 { } 包括起来; 
执行脚本:
可调用外部指令来进行额外的命令下达,这个设置需与 sharedscripts .... 
endscript 设置合用才行。至于可用的环境为: 
prerotate:在启动 logrotate 之前进行的指令,例如修改登录文件的属性等动作; 
postrotate:在做完 logrotate 之后启动的指令,例如重新启动 (
kill -HUP) 某个 服务! Prerotate 与 postrotate 
对于已加上特殊属性的文件处理上面,是相当重要的执行 程序! 
那么 /etc/logrotate.d/syslog 内设置的 5 个文件的轮替功能就变成了:

该设置只对 /var/log/ 内的 cron, maillog, messages, secure, spooler 有效; 
登录文件轮替每周一次、保留四个、且轮替下来的登录文件不进行压缩(未更改默认 
值);
轮替完毕后 (postrotate) 取得 syslog 的 PID 后,以 kill -HUP 重新启动 syslogd

3.实际测试logtate的动作

[root@study ~]# logrotate [-vf] logfile 
选项与参数: 
-v :启动显示模式,会显示 logrotate 运行的过程喔! 
-f :不论是否符合配置文件的数据,强制每个登录文件都进行 rotate 的动作! 
范例一:执行一次 logrotate 看看整个流程为何? 
[root@study ~]# logrotate -v /etc/logrotate.conf 
reading config file /etc/logrotate.conf <==读取主要配置文件 
including /etc/logrotate.d <==调用外部的设置 
reading config file chrony <==就是外部设置啊! 
....(中间省略).... 
Handling 18 logs <==共有 18 个登录文件被记录 
....(中间省略).... 
rotating pattern: /var/log/cron 
/var/log/maillog 
/var/log/messages 
/var/log/secure 
/var/log/spooler 
weekly (52 rotations) 
empty log files are not rotated, old logs are removed 
considering log /var/log/cron 
log does not need rotating 
considering log /var/log/maillog 
log does not need rotating 
considering log /var/log/messages <==开始处理 messages 
log does not need rotating <==因为时间未到,不需要更动! 
....(下面省略).... 
范例二:强制进行 logrotate 的动作 
[root@study ~]# logrotate -vf /etc/logrotate.conf 
....(前面省略).... 
rotating log /var/log/messages, log->rotateCount is 52 
dateext suffix '-20150820' 
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' 
compressing log with: /bin/gzip 
....(下面省略).... 
# 看到否?整个 rotate 的动作就是这样一步一步进行的~ 
[root@study ~]# ll /var/log/messages*; lsattr /var/log/messages 
-rw-------. 1 root root 143 Aug 20 01:45 /var/log/messages 
-rw-------. 1 root root 167125 Aug 20 01:40 /var/log/messages-20150820 
-----a---------- /var/log/messages <==主动加入 a 的隐藏属性啰!

上面那个 -f 具有“强制执行”的意思,如果一切的设置都没有问题的话,那么理论
上,你的 /var/log 这个目录就会起变化啰!而且应该不会出现错误讯息才对!嘿嘿!这样
就 OK 了!很棒不是吗?!
由于 logrotate 的工作已经加入 crontab 里头了!所以现在每天系统都会自动的给他
查看 logrotate 啰!不用担心的啦!只是要注意一下那个 /var/log/messages 里头是否常常有
类似下面的字眼:Aug 20 01:45:34 study rsyslogd: [origin software=”rsyslogd” swVersion=”7.4.7” x-
pid=”2145” x-info=”http://www.rsyslog.com“] rsyslogd was HUPed
这说明的是 rsyslogd 重新启动的时间啦 (就是因为 /etc/logrotate.d/syslog 的设置之
缘故!) 下

4.自定义日志文件的轮替

要求:

  • 登录文件轮替一个月进行一次;
  • 该登录文件若大于 10MB 时,则主动进行轮替,不需要考虑一个月的期限;
  • 保存五个备份文件;
  • 备份文件需要压缩
    # 1. 先创建 +a 这个属性啊! 
    [root@study ~]# chattr +a /var/log/admin.log 
    [root@study ~]# lsattr /var/log/admin.log 
    -----a---------- /var/log/admin.log 
    [root@study ~]# mv /var/log/admin.log /var/log/admin.log.1 
    mv: cannot move `/var/log/admin.log' to `/var/log/admin.log.1': Operation not permitted 
    # 这里确定了加入 a 的隐藏属性!所以 root 无法移动此登录文件! 
    # 2. 开始创建 logrotate 的配置文件,增加一个文件在 /etc/logrotate.d 内就对了! 
    [root@study ~]# vim /etc/logrotate.d/admin 
    # This configuration is from VBird 2015/08/19 
    /var/log/admin.log { 
    monthly <==每个月进行一次 
    size=10M <==文件大小大于 10M 则开始处置 
    rotate 5 <==保留五个! 
    compress <==进行压缩工作! 
    sharedscripts 
    prerotate
    /usr/bin/chattr -a /var/log/admin.log 
    endscript 
    sharedscripts 
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true 
    /usr/bin/chattr +a /var/log/admin.log 
    endscript 
    }
    # 3. 测试一下 logrotate 相关功能的信息显示: 
    [root@study ~]# logrotate -v /etc/logrotate.conf 
    ....(前面省略).... 
    rotating pattern: /var/log/admin.log 10485760 Bytes (5 rotations) 
    empty log files are rotated, old logs are removed 
    considering log /var/log/admin.log 
    log does not need rotating 
    not running prerotate script, since no logs will be rotated 
    not running postrotate script, since no logs were rotated 
    ....(下面省略).... 
    # 因为还不足一个月,文件也没有大于 10M,所以不需进行轮替! 
    # 4. 测试一下强制 logrotate 与相关功能的信息显示: 
    [root@study ~]# logrotate -vf /etc/logrotate.d/admin 
    reading config file /etc/logrotate.d/admin 
    reading config file /etc/logrotate.d/admin 
    Handling 1 logs 
    rotating pattern: /var/log/admin.log forced from command line (5 rotations) 
    empty log files are rotated, old logs are removedconsidering log /var/log/admin.log 
    log needs rotating 
    rotating log /var/log/admin.log, log->rotateCount is 5 
    dateext suffix '-20150820' 
    glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' 
    renaming /var/log/admin.log.5.gz to /var/log/admin.log.6.gz 
    (rotatecount 5, logstart 1, i 5), 
    old log /var/log/admin.log.5.gz does not exist 
    renaming /var/log/admin.log.4.gz to /var/log/admin.log.5.gz 
    (rotatecount 5, logstart 1, i 4), 
    old log /var/log/admin.log.4.gz does not exist 
    renaming /var/log/admin.log.3.gz to /var/log/admin.log.4.gz 
    (rotatecount 5, logstart 1, i 3), 
    old log /var/log/admin.log.3.gz does not exist 
    renaming /var/log/admin.log.2.gz to /var/log/admin.log.3.gz 
    (rotatecount 5, logstart 1, i 2), 
    old log /var/log/admin.log.2.gz does not exist 
    renaming /var/log/admin.log.1.gz to /var/log/admin.log.2.gz 
    (rotatecount 5, logstart 1, i 1), 
    old log /var/log/admin.log.1.gz does not exist 
    renaming /var/log/admin.log.0.gz to /var/log/admin.log.1.gz 
    (rotatecount 5, logstart 1, i 0), 
    old log /var/log/admin.log.0.gz does not exist 
    log /var/log/admin.log.6.gz doesn't exist -- won't try to dispose of it 
    running prerotate script 
    fscreate context set to system_u:object_r:var_log_t:s0 
    renaming /var/log/admin.log to /var/log/admin.log.1 
    running postrotate script 
    compressing log with: /bin/gzip 
    [root@study ~]# lsattr /var/log/admin.log* 
    -----a---------- /var/log/admin.log 
    ---------------- /var/log/admin.log.1.gz <==有压缩过喔!