-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclearzcfiles.sh
More file actions
288 lines (266 loc) · 9.64 KB
/
clearzcfiles.sh
File metadata and controls
288 lines (266 loc) · 9.64 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
if [ ! -f clearzcfiles.conf ]; then
touch clearzcfiles.conf
fi
if [ ! -f clearzcfiles.log ]; then
touch clearzcfiles.log
fi
#默认保留天数
savedays=15
#最少保存天数
readonly minsavedays=2
#docker保留行数
readonly savelines=5000000
#脚本路径
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
function usage() {
echo "USAGE: 主动或定时清理自动装车产生的历史文件夹"
echo "parameter:"
echo "-z 按照设置的保留的天数或默认的天数主动删除历史文件夹"
echo "-d 主动清理docker日志,数据全清理"
echo "-o 定时或主动清理docker日志,数据保留最近500W行"
echo "-t 设置定时清理文件夹任务,默认每天的一点进行"
echo "-l 设置定时清理docker日志任务,每周天清理一次,且只保留最近500W行数据做备份"
echo "-s <days> 设置保留最近指定的天数的文件夹"
echo "-w 检测服务是否正常,不正常则重新启动"
echo "-h 参数列表"
echo "-v 版本号"
}
#清理docker日志
function cleardockerlogs() {
echo $(date) "开始清理整个docker日志" >>$DIR"/clearzcfiles.log"
rootp=$(docker info | grep "Docker Root Dir")
if [[ $rootp =~ "/var/lib/docker" ]]; then
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs; do
echo "clean logs : $log"
tail -n $savelines $log >$log.bak
cat /dev/null >$log
done
elif [[ $rootp =~ "/home/storage/docker" ]]; then
logs=$(find /home/storage/docker/containers/ -name *-json.log)
for log in $logs; do
echo "clean logs : $log"
tail -n $savelines $log >$log.bak
cat /dev/null >$log
done
fi
return 0
}
#清理docker日志
function cleardockerpartlogs() {
echo $(date) "开始清理最旧的$savelines行docker日志" >>$DIR"/clearzcfiles.log"
rootp=$(docker info | grep "Docker Root Dir")
if [[ $rootp =~ "/var/lib/docker" ]]; then
logs=$(find /var/lib/docker/containers/ -name *-json.log)
for log in $logs; do
lines=$(wc -l <$log)
echo "clean logs : $log,lines : $lines" >>$DIR"/clearzcfiles.log"
if [ $lines -gt $savelines ]; then
sl=$(expr $lines - $savelines)
sed -i "1,${sl}d" $log
fi
done
elif [[ $rootp =~ "/home/storage/docker" ]]; then
logs=$(find /home/storage/docker/containers/ -name *-json.log)
for log in $logs; do
lines=$(wc -l <$log)
echo "clean logs : $log,lines : $lines" >>$DIR"/clearzcfiles.log"
if [ $lines -gt $savelines ]; then
sl=$(expr $lines - $savelines)
sed -i "1,${sl}d" $log
fi
done
fi
return 0
}
#读取配置文件中保存的天数
function getsavedays() {
if [ -f $DIR"/clearzcfiles.conf" ]; then
local conf=$(cat $DIR"/clearzcfiles.conf" | grep -e '^[0-9]*$')
if [ ! -z "$conf" ] && [ $conf -ge $minsavedays ]; then
savedays=$conf
fi
fi
return 0
}
#清理文件夹
function clearfiles() {
local filepath=$1
local nums=$(ls -d $filepath | wc -l)
if [ $nums -gt $savedays ]; then
clearnums=$(expr $nums - $savedays)
echo "需要清理"$clearnums"个文件夹"
local files=$(ls -rtd $filepath | head -n $clearnums)
echo $files >>$DIR"/clearzcfiles.log"
rm -rf $files
fi
return 0
}
#设置保留天数
function setsavedays() {
local days=$1
if [ -z "days" ]; then
echo "设置失败,保留天数不可为空~"
return -1
fi
expr $days "+" 10 &>/dev/null
if [ $? -eq 0 ]; then
echo "设置保留"$days"天的文件~"
else
echo "设置失败,保留天数应该输入数字~"
return -1
fi
if [ $days -lt $minsavedays ]; then
echo "设置失败,保留天数不可少于"$minsavedays"天数"
return -1
fi
echo $days >$DIR"/clearzcfiles.conf"
savedays=$days
echo -e "设置\033[1m\033[32m成功\033[0m!"
return 0
}
#清理文件
function clearhistory() {
getsavedays
echo $(date) "开始清理文件" >>$DIR"/clearzcfiles.log"
echo "开始清理img~"
clearfiles "/home/storage/data/lidar/img/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理pcd~"
clearfiles "/home/storage/data/lidar/pcd/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理log~"
clearfiles "/home/storage/data/lidar/log/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理统一的日志~"
clearfiles "/home/storage/data/lidar/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理自助原图片~"
clearfiles "/home/storage/data/video/img/001/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
clearfiles "/home/storage/data/video/img/002/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理自助结果图像~"
clearfiles "/home/storage/data/video/img/result/001/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
clearfiles "/home/storage/data/video/img/result/002/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
echo "开始清理自助日志"
clearfiles "/home/storage/data/video/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
clearfiles "/home/storage/data/video/[2-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
return 0
}
#启动定时清理文件夹任务
function startclearfilstask() {
systemctl stop clearzcfiles.timer 2>/dev/null
echo -e "[Unit]\nDescription=Periodic cleaning zc files\n\n[Timer]\nOnCalendar=*-*-* 01:00:00\nPersistent=true\nRandomizedDelaySec=60\nUnit=clearzcfiles.service\n\n[Install]\nWantedBy=timers.target" >/etc/systemd/system/clearzcfiles.timer
echo -e "[Unit]\nDescription=Periodic cleaning zc files\n\n[Timer]\nOnCalendar=*-*-* 06:00:00\nPersistent=true\nRandomizedDelaySec=60\nUnit=watchclearzcfiles.service\n\n[Install]\nWantedBy=timers.target" >/etc/systemd/system/watchclearzcfiles.timer
echo -e "[Unit]\nDescription=Periodic cleaning zc files\n\n[Service]\nExecStart=/bin/bash "$(pwd)"/clearzcfiles.sh -z\nRestart=on-failure\nRestartSec=10s" >/etc/systemd/system/clearzcfiles.service
echo -e "[Unit]\nDescription=Periodic cleaning zc files\n\n[Service]\nExecStart=/bin/bash "$(pwd)"/clearzcfiles.sh -w\nRestart=on-failure\nRestartSec=10s" >/etc/systemd/system/watchclearzcfiles.service
echo "清理文件夹任务的清理脚本工作文件路径为$(pwd)"
systemctl start watchclearzcfiles.timer 2>/dev/null
systemctl enable watchclearzcfiles.timer 2>/dev/null
systemctl start clearzcfiles.timer 2>/dev/null
systemctl enable clearzcfiles.timer 2>/dev/null
local info=$(systemctl status clearzcfiles.timer | grep "Active: active")
if [[ "$info" != "" ]]; then
echo -e "启动定时清理文件夹任务\033[1m\033[32m成功\033[0m!"
else
echo "启动定时清理文件夹任务失败~"
fi
}
#启动定时清理docker日志任务
function startcleardockerlogtask() {
systemctl stop clearzcdockerlogs.timer 2>/dev/null
echo -e "[Unit]\nDescription=Periodic cleaning zc docker logs\n\n[Timer]\nOnCalendar=Sun *-*-* 02:00:00\nPersistent=true\nRandomizedDelaySec=60\nUnit=clearzcdockerlogs.service\n\n[Install]\nWantedBy=timers.target" >/etc/systemd/system/clearzcdockerlogs.timer
echo -e "[Unit]\nDescription=Periodic cleaning zc docker logs\n\n[Service]\nExecStart=/bin/bash "$(pwd)"/clearzcfiles.sh -o" >/etc/systemd/system/clearzcdockerlogs.service
systemctl start clearzcdockerlogs.timer 2>/dev/null
systemctl enable clearzcdockerlogs.timer 2>/dev/null
local info=$(systemctl status clearzcdockerlogs.timer | grep "Active: active")
echo "清理docker日志任务的清理脚本工作文件路径为$(pwd)"
if [[ "$info" != "" ]]; then
echo -e "启动定时清理docker日志任务\033[1m\033[32m成功\033[0m!"
else
echo "启动定时清理docker日志任务失败~"
fi
}
#清理观察记录历史时间是否错乱的日志
function clearTimeLog() {
local file="/home/storage/zc/TimeLog.txt" # The path to the file
local line_num=60000
# Check if the file has more than 50000 lines
line_count=$(wc -l <"$file")
if [ "$line_count" -gt $line_num ]; then
# The file has more than 50000 lines, truncate the file to the last 50000 lines
tail -n $line_num "$file" >"$file.tmp" && mv "$file.tmp" "$file"
fi
}
#检测服务是否正常
function checkservice() {
local state=$(systemctl is-failed clearzcdockerlogs.timer)
if [[ "$state" != "active" ]]; then
echo -e "检测到清理文件任务失败~!"
$(pwd)/clearzcfiles.sh -l
fi
state=$(systemctl is-failed clearzcdockerlogs.service | grep "active")
if [[ "$state" != "active" ]]; then
echo -e "检测到清理文件任务失败~!"
$(pwd)/clearzcfiles.sh -l
fi
state=$(systemctl is-failed clearzcfiles.timer)
if [[ "$state" != "active" ]]; then
echo -e "检测到清理文件任务失败~!clearzcfiles.timer"
$(pwd)/clearzcfiles.sh -t
fi
state=$(systemctl is-failed clearzcfiles.service)
if [[ "$state" != "active" ]]; then
echo -e "检测到清理文件任务失败~!clearzcfiles.service"
$(pwd)/clearzcfiles.sh -t
fi
}
function main() {
while getopts "dlzos:thvw" arg; do
case $arg in
z)
echo "开始主动清理历史文件夹~"
clearhistory
;;
s)
echo "开始设置保留天数~"
setsavedays $OPTARG
;;
t)
echo "开始设置定时清理文件夹~"
startclearfilstask
;;
d)
echo "开始清理docker日志~"
cleardockerlogs
;;
l)
echo "开始设置定时清理docker日志任务~"
startcleardockerlogtask
;;
o)
echo "开始清理docker日志~"
cleardockerpartlogs
;;
w)
echo "检测服务运行状态~"
checkservice
;;
h)
usage
exit 1
;;
v)
echo "3.6.31"
exit 1
;;
time)
clearTimeLog
exit 1
;;
?)
usage
exit -2
;;
esac
done
}
main "$@"
RESULT=$?
exit $RESULT