shell脚本日志清理和压缩

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
我的博客已迁移到xdoujiang.com请去那边和我交流
1、日志结构
/opt/online/ {123com,abccom} /log/online [1-9]/[a-z]
 
2、变量说明
#是否开启压缩功能(0:关闭,1:开启;若关闭压缩功能,则compressdayn无效)
needcompress=1
删除14天以前的日志
deldayn=14
前1天
lastdayn=1
服务类型
allsvrtype= "online1 online2 online3"
压缩5天前的日志
compressdayn=5
 
3、计划任务使用方法:
将该脚本本身copy到 /opt/online 下;
crontab 中配置成:
#30 0 * * * cd /opt/online && chmod +x cleanlog.sh && ./cleanlog.sh >/dev/null 2>&1
 
cat  cleanlog.sh
#!/bin/bash
#--------------------------------------------------  
#Created:2015-05-18
#Author:jimmygong
#Mail:jimmygong@taomee.com
#Function:
#Version:1.0
#--------------------------------------------------
allsvrtype= "online1 online2 online3"
deldayn=14
needcompress=1
compressdayn=5
lastdayn=1
 
alldomain= "123com abccom"
allsubdir= "a b c d e f g h i j k l m n o p q r s t u v w x y z"
rootdir= "/opt/online"
dirtobedelname= "tobedel"
 
lastday=` date  -d  "$lastdayn days ago"  +%Y%m%d`
compressday=` date  -d  "$compressdayn days ago"  +%Y%m%d`
delday=` date  -d  "$deldayn days ago"  +%Y%m%d`
 
function  cleandir ()
{
     local  ldir= "$1"
     local  lfiles=
     local  lfile=
 
     lfiles=` ls  -1 $ldir |  xargs `
     for  lfile  in  $lfiles
     do
         rm  -f $ldir/$lfile
     done
}
 
function  compressdir ()
{
     local  ldir= "$1"
     local  lfiles=
     local  lfile=
     local  lcompressedtarget= "$ldir/$compressday.tar.gz"
     # to prevent duplicate-compress
     if  [[ -f $lcompressedtarget ]]
     then
         return
     fi
     tar  zcvf $ldir. tar .gz $ldir
     if  [[ $? - ne  0 ]] ;  then
         return
     fi
     lfiles=` ls  -1 $ldir |  xargs `
     for  lfile  in  $lfiles
     do
         rm  -f $ldir/$lfile
     done
     if  [[ -f $ldir. tar .gz ]] ;  then
         mv  $ldir. tar .gz $lcompressedtarget
     fi
}
 
function  handleonelogdir ()
{
     local  ldir= "$1"
     local  lscandir= "$ldir/$dirtobedelname"
     local  lmvdir= "$lscandir/$lastday"
     local  lones=
     local  lone=
     local  lonefull=
     mkdir  -p $lscandir
     # remove
     lones=` ls  -1 $lscandir |  grep  $delday |  xargs `
     for  lone  in  $lones
     do
         lonefull= "$lscandir/$lone"
         if  [[ -d $lonefull ]] ;  then
             cleandir $lonefull
             rm  -rf $lonefull
         elif  [[ -f $lonefull ]] ;  then
             rm  -f $lonefull
         fi
     done
     # compress
     if  [[ $needcompress - eq  1 ]]
     then
         lones=` ls  -1 $lscandir |  grep  $compressday |  xargs `
         for  lone  in  $lones
         do
             lonefull= "$lscandir/$lone"
             if  [[ -d $lonefull ]]
             then
                 compressdir $lonefull
             elif  [[ -f $lonefull ]]
             then
                 tar  zcvf $lonefull. tar .gz $lonefull
             fi
         done
     fi
     # move last day's data
     mkdir  -p $lmvdir
     lones=` ls  -1 $ldir |  grep  $lastday |  xargs `
     for  lone  in  $lones
     do
         mv  $ldir/$lone $lmvdir
     done
}
 
for  domain  in  $alldomain
do
     domaindir= "$rootdir/$domain"
     if  [[ ! -d $domaindir ]]
     then
         continue
     fi
     for  svrtp  in  $allsvrtype
     do
         svrtplogdir= "$domaindir/log/$svrtp"
         if  [[ ! -d $svrtplogdir ]]
         then
             continue
         fi
         for  sub  in  $allsubdir
         do
             svrtplogsubdir= "$svrtplogdir/$sub"
             if  [[ ! -d $svrtplogsubdir ]]
             then
                 continue
             fi
             handleonelogdir  "$svrtplogsubdir"
         done
     done
done
 
exit  0









本文转自 xdoujiang 51CTO博客,原文链接:http://blog.51cto.com/7938217/1652898,如需转载请自行联系原作者
相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
目录
相关文章
|
7月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
627 9
|
7月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
646 2
|
10月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
355 60
|
7月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
9月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1074 1
|
11月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
477 17
|
11月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
578 3
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
528 9
|
11月前
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
1077 54