加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl按日期归档日志

发布时间:2020-12-16 00:33:24 所属栏目:大数据 来源:网络整理
导读:帮一上海朋友工厂维护一台freebsd服务器,该服务器上运行着squid等服务。服务器上有一支perl程序会每天定时的切割当天的squid日志,以日期形式保存在某目录下,大体如下: -rw-r--r--??1?root??wheel???51827130?Aug?20?04:27?20110801.log? -rw-r--r--??1?r

帮一上海朋友工厂维护一台freebsd服务器,该服务器上运行着squid等服务。服务器上有一支perl程序会每天定时的切割当天的squid日志,以日期形式保存在某目录下,大体如下:

 
 
  1. -rw-r--r--??1?root??wheel???51827130?Aug?20?04:27?20110801.log?
  2. -rw-r--r--??1?root??wheel???56598624?Aug?20?04:27?20110802.log?
  3. -rw-r--r--??1?root??wheel???66925544?Aug?20?04:27?20110803.log?
  4. -rw-r--r--??1?root??wheel???47917440?Aug?20?04:27?20110804.log?
  5. -rw-r--r--??1?root??wheel???44134286?Aug?20?04:27?20110805.log?
  6. -rw-r--r--??1?root??wheel???43705704?Aug?20?04:27?20110808.log?
  7. -rw-r--r--??1?root??wheel???33358662?Aug?20?04:27?20110809.log?
  8. -rw-r--r--??1?root??wheel???35820354?Aug?20?04:27?20110810.log?
  9. -rw-r--r--??1?root??wheel???48065784?Aug?20?04:27?20110811.log?
  10. -rw-r--r--??1?root??wheel???38021175?Aug?20?04:27?20110812.log?
  11. -rw-r--r--??1?root??wheel???30382856?Aug?20?04:27?20110813.log?
  12. -rw-r--r--??1?root??wheel???11898255?Aug?20?04:27?20110815.log?
  13. -rw-r--r--??1?root??wheel???10068232?Aug?20?04:27?20110816.log?
  14. -rw-r--r--??1?root??wheel???12431556?Aug?20?04:27?20110817.log?
  15. -rw-r--r--??1?root??wheel????6405197?Aug?20?04:27?20110818.log?
  16. -rw-r--r--??1?root??wheel????5054207?Aug?20?04:27?20110819.log?
  17. -rw-r--r--??1?root??wheel????3455222?Aug?26?04:14?20110821.log?
  18. -rw-r--r--??1?root??wheel??124680376?Aug?26?04:14?20110822.log?
  19. -rw-r--r--??1?root??wheel??150105140?Aug?26?04:14?20110823.log?
  20. -rw-r--r--??1?root??wheel????5183330?Aug?26?04:14?20110824.log?
  21. -rw-r--r--??1?root??wheel????6486877?Aug?26?04:14?20110825.log?
  22. -rw-r--r--??1?root??wheel????8481735?Sep??2?04:03?20110828.log?
  23. -rw-r--r--??1?root??wheel???22789068?Sep??2?04:03?20110829.log?
  24. -rw-r--r--??1?root??wheel???68841800?Sep??2?04:03?20110830.log?

要求:编写一脚本程序,对该目录中日志以年月的形式进行归档,如201108.tar.gz

代码如下:

 
 
  1. #!/usr/bin/perl?-w?
  2. ?
  3. use?strict;?
  4. use?Shell?qw/mv/;?
  5. use?File::Path?qw/remove_tree/; //调用该模块的目的是为了删除归档前的非空目录
  6. ?
  7. my?%log;?
  8. ?
  9. if?(?$<?!=?0?)?{?
  10. ???printf?"%s","You?must?run?this?script?as?rootn";?
  11. }?
  12. ?
  13. opendir?LOG,'/data/squid_log'?or?die?"$!n";?
  14. ?
  15. while?(?my?$file?=?readdir?LOG?)?{?
  16. ????????chomp?$file;?
  17. ????????next?unless?$file?=~?/^(d{6})d+/;?
  18. ????????push?@{$log{$1}},$file;?
  19. }?
  20. ?
  21. closedir?LOG;?
  22. ?
  23. for?my?$str?(?sort?keys?%log?)?{?
  24. ????my?@array?=?@{$log{$str}};?
  25. ????mkdir?$str,0755;?
  26. ????my?$sh?=?Shell->new();?
  27. ????$sh->mv("@array?$str");?
  28. ????system("tar?czvf?${str}.tar.gz?$str");?
  29. ????remove_tree("$str",{?verbose?=>?1,});?
  30. ?
  31. }?

归档过程:

 
 
  1. a?201102?
  2. a?201102/20110213.log?
  3. a?201102/20110214.log?
  4. a?201102/20110218.log?
  5. a?201102/20110220.log?
  6. a?201102/20110221.log?
  7. a?201102/20110222.log?
  8. a?201102/20110223.log?
  9. a?201102/20110224.log?
  10. a?201102/20110225.log?
  11. a?201102/20110228.log?
  12. unlink?201102/20110213.log?
  13. unlink?201102/20110214.log?
  14. unlink?201102/20110218.log?
  15. unlink?201102/20110220.log?
  16. unlink?201102/20110221.log?
  17. unlink?201102/20110222.log?
  18. unlink?201102/20110223.log?
  19. unlink?201102/20110224.log?
  20. unlink?201102/20110225.log?
  21. unlink?201102/20110228.log?
  22. rmdir?201102?
  23. ……………………………………………………………………

最后归档后的效果:

 
 
  1. [/data/squid_log]#?ll?
  2. total?203762?
  3. -rw-r--r--??1?root??wheel??12817056?Sep?16?20:49?201012.tar.gz?
  4. -rw-r--r--??1?root??wheel???8170954?Sep?16?20:49?201101.tar.gz?
  5. -rw-r--r--??1?root??wheel???8819026?Sep?16?20:49?201102.tar.gz?
  6. -rw-r--r--??1?root??wheel??15008666?Sep?16?20:49?201104.tar.gz?
  7. -rw-r--r--??1?root??wheel??10950252?Sep?16?20:49?201105.tar.gz?
  8. -rw-r--r--??1?root??wheel??70723508?Sep?16?20:50?201106.tar.gz?
  9. -rw-r--r--??1?root??wheel??13035446?Sep?16?20:50?201107.tar.gz?
  10. -rw-r--r--??1?root??wheel??63533990?Sep?16?20:51?201108.tar.gz?
  11. -rw-r--r--??1?root??wheel???5290849?Sep?16?20:51?201109.tar.gz?
  12. -rwx------??1?root??wheel???????646?Sep?16?17:30?log.pl

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读