您的位置:首页 > 运维架构 > Apache

Apache自带日志切割工具Logrotate和maxage

2017-08-30 23:21 716 查看
作者清水大王(/u/4yjkua) 关注
2016.02.20 16:50* 字数1086 阅读155 评论0 喜欢0
星期五的早上,公司应用服务提示不可访问。最后发现是硬盘内存不够。
某应用写日志太频繁,导致内存撑爆了。
于是决定做日志切割,备份。
在决定做日志切割和备份时,本来自己想写个shell 脚本。
脚本需要执行
1. copy 原来的日志文件。
2. 给文件名添加日期等特殊的标识符。
3. 压缩拷贝过的文件。
4. 清空原来日志文件。
5. 添加定时任务,指定某一时间执行。
6. 只保留固定个数的切割文档。
最后寻么着,Linux这么强大,有没有已经写好的工具,Google下,还真有。
那就是 Logrotate
一、 Logrotate 是啥
logrotate is designed to ease administration of systems that generate large numb
ers of log files. It allows automatic rotation, compression, removal, and mailing
of log files. Each log file may be handled daily, weekly, monthly, or when it gr
ows too large.
Normally, logrotate is run as a daily cron job. It will not modify a log multiple time
s in one day unless the criterion for that log is based on the log's size
and logrotate is being run multiple times each day, or unless the -f or --force opti
on is used.
Any number of config files may be given on the command line. Later config files
may override the options given in earlier files, so the order in which the
logrotate config files are listed is important. Normally, a single config file which in
cludes any other config files which are needed should be used. See
below for more information on how to use the include directive to accomplish thi
s. If a directory is given on the command line, every file in that directory is
used as a config file.
If no command line arguments are given, logrotate will print version and copyrigh
t information, along with a short usage summary. If any errors occur while
rotating logs, logrotate will exit with non-zero status.
logrotate 可以很方便的管理日志。可以对日志文件进行轮替,压缩,切割,发布邮件等
功能。
Logrotate 配置
文件所在位置: /etc/logrotate.d/rails_production

/home/deploy/apps/production.log {
missingok
copytruncate
rotate 10
compress
notifempty
sharedscripts
dateext
dateformat -%Y-%m-%d-%s
olddir /data1/log/rails
size=10M
postrotate
endscript
}
参数解释
missingok: 如果找不到log档也没有关系
copytruncate: 先复制log档的内容后,再清空的做法
rotate: 表示要保留的份数
compress: 表示压缩起来,预设用gzip
notifempty: 表示如果log档是空的,就不rotate
dateext: 表示添加YYYYMMDD形式字符串作为文件名一部分
dateformat: 表示格式化文件名日期显示部分
olddir: 表示要备份的文件目录
size: 表示超过这个值,就进行rotate,可以为100M或10G
sharedscripts: 表示在所有的日志文件轮转完毕后统一执行一次脚本,如果没有这条指
令,那么每个日志文件轮转完毕后,都会执行一次脚本
rotate 和 maxage 区别
rotate 在于rotate是以个数为单位的,而maxage
是以天数为单位的。如果我们是以按天来轮转的日志,那么二者的差别就不大
Logrotate 是以CRON 允许的,所以这个时间由CRON控制的
查看/etc/crontab 或/etc/anacrontab
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
可以自定义修改,这些配置文件,如果需要的话。
二、定时任务配置
为了更加自由,设定定时任务执行,自定义rotate 的执行。
* 0 * * * /bin/bash -l -c 'logrotate -vf /etc/logrotate.d/rails_production 1>/dev/null 2>&1'
三、线下备份切割日志
由于线上只有 10个备份文件
永久备份文件,需要线下启动定时任务,定时同步线上切割日志
* 1 * * 6 /bin/bash -l -c 'rsync -ravz -e "ssh -p 10080" deploy@xxx:xxx:xxx:xxx:/data1/log/rails /data1/online-log-backup —log-file=/data1/online-四、logrotate 源文件和切割文件不再同一挂载点
logrotate olddir are on different devices

举报文章 著作权归作者所有
(/u/4yjkua)
关注

(http://cwb.assets.jianshu.io/notes/images/3051261/weibo/image_d9ef4727673b.更多分享
解决办法
1、去掉了 compress
2、去掉了 olddir 配置
3、在 postrotate 里面添加如下语句
mv /home/deploy/apps/production.log-* /data1/log/rails
gzip /data1/log/rails/production.log-*
最终结果如下:
/home/deploy/apps/production.log {
missingok
copytruncate
rotate 10
notifempty
sharedscripts
dateext
dateformat -%Y-%m-%d-%s
size=10M
postrotate
mv /home/deploy/apps/production.log-* /data1/log/rails
gzip /data1/log/rails/production.log-*
endscript
}
参考
10 Practical Examples of Rsync Command in Linux (http://www.tecmint.com/rsync-local
-remote-file-synchronization-commands/)
rsync 同步的艺术(http://roclinux.cn/?p=2643)
被遗忘的Logrotate (http://huoding.com/2013/04/21/246)
logrotate 详细介绍(http://linux.die.net/man/8/logrotate)
认识与分析日志(http://linux.vbird.org/linux_basic/0570syslog.php#rotate)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Apache