设为首页 收藏本站
查看: 675|回复: 0

[经验分享] Apache自带日志切割工具Logrotate和maxage-Sky

[复制链接]

尚未签到

发表于 2018-11-18 10:38:56 | 显示全部楼层 |阅读模式
  作者清水大王(/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>  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)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-636499-1-1.html 上篇帖子: apache+tomcat6+jk负载均衡 下篇帖子: WDCP V3 安装mod
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表