日志轮替工具logrotate归纳总结

本文详细介绍logrotate的功能、配置及使用方法,包括日志分割、压缩、备份等操作,并提供实际应用场景示例。

1 理论部分

1.1 logrotate的作用

logrotate通俗点讲主要实现日志分割功能,关于详细功能如下:

- 定义日志的转存规则(根据时间或大小转存,转存几份以及旧日志删除)

- 定义转存同时压缩日志

- 定义日志的邮寄备份

- 定义日志的转存权限

- 定义空日志的转存方式

1.2 logrotate的启动

1
cat  /etc/cron .daily /logrotate

详细如下:

1
2
3
4
5
6
7
8
#!/bin/sh
 
/usr/sbin/logrotate  /etc/logrotate .conf
EXITVALUE=$?
if  [ $EXITVALUE != 0 ];  then
     /usr/bin/logger  -t logrotate  "ALERT exited abnormally with [$EXITVALUE]"
fi
exit  0

注:以上可见logrotate是基于crontab触发执行(按天)

1.3 logrotate的配置文件

1
cat  /etc/logrotate .conf

详细如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
weekly
rotate 4
create
dateext
include  /etc/logrotate .d
/var/log/wtmp  {
     monthly
     create 0664 root utmp
         minsize 1M
     rotate 1
}
/var/log/btmp  {
     missingok
     monthly
     create 0600 root utmp
     rotate 1
}

注:留意include参数,故可在“/etc/logrotate.d”目录配置子配置文件

1.4 logrotate的配置文件的常用参数

1.4.1 时间参数

yearly - 按年转存日志

monthly - 按月转存日志

weekly - 按周转存日志

daily - 按日转存日志

1.4.2 日志保留的份数

rotate - 转存保留多少份日志

范例:

1
rotate 5

1.4.3 日志的压缩

compress - 转存的日志使用gzip压缩

delaycompress- 不压缩最近一次的日志

1.4.4 错误处理

missingok - 转存时忽略任何错误

1.4.5 空日志处理

notifempty - 不转存空日志

1.4.6 权限处理

create - 指定转存日志权限

范例:

1
create 644 root root

1.4.7 脚本调用

prerotate/endscript - 指定日志转存前调用脚本

postrotate/endscript - 指定日志转存后调用脚本

范例:

1
2
3
postrotate
/usr/bin/killall  -HUP rsyslogd
endscript

1.4.7 其他参数

请参阅:

http://www.linuxcommand.org/man_pages/logrotate8.html

2 实践部分

2.1 软件包的安装

1
yum  install  -y logrotate

2.2 定位日志配置文件

1
ls  -l  /var/log/wtmp

详细显示如下:

1
-rw-rw-r--. 1 root utmp 46464 Feb  9 08:48  /var/log/wtmp

2.3 编辑配置文件

1
vim  /etc/logrotate .d /wtmp

配置如下:

1
2
3
4
5
6
7
8
9
/var/log/wtmp  {
     yearly
     rotate 5
     compress
     delaycompress
     missingok
     notifempty
     create 644 root utmp
}

注:没有就创建,配置文件名称与日志文件名称一致(方便管理)

2.4 测试配置文件

1
logrotate -d  /etc/logrotate .d /wtmp

2.5 手动运行配置文件(可选)

1
logrotate  /etc/logrotate .conf

1
logrotate  /etc/logrotate .d /wtmp

==========================================================










本文转自 tanzhenchao 51CTO博客,原文链接:http://blog.51cto.com/cmdschool/1896025,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值