Linux使用logrotate进行日志文件切分备份
配置参数 | 说明 |
---|---|
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 | 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 |
daily | 指定转储周期为每天 |
weekly | 指定转储周期为每周 |
monthly | 指定转储周期为每月 |
rotate count | 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 |
tabooext [+] list让logrotate | 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ |
size size | 当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem) |
missingok | 在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。 |
##常用配置样例
/opt/apache-tomcat-7.0.26/logs/catalina.out{
su root root
daily
rotate 365
copytruncate
dateext
compress
notifempty
missingok
olddir /opt/apache-tomcat-7.0.26/logs/logbak
}
##配置步骤
1、将配置文件放入 /etc/logrotate.d 文件夹
2、测试脚本是否正确,手动执行脚本 /usr/sbin/logrotate -v -f /etc/logrotate.conf
3、修改crontab配置 crontab -e
4、增加执行定时配置,例如:1 0 * * * /usr/sbin/logrotate -f /etc/logrotate.conf
5、启动crontab命令:/etc/init.d/crond restart 或者 service crond restart
6、查看crontab状态命令:/etc/init.d/crond status 或者 service crond status
##权限异常处理
because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation.
增加配置 su root xx 到配置文件中,其中xx代表log文件所属用户