-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcheck_and_archive_log_files.sh
executable file
·72 lines (61 loc) · 1.64 KB
/
check_and_archive_log_files.sh
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
#!/bin/bash
#
# .============.
# // M A K E / \
# // C++ DEV / \
# // E A S Y / \/ \
# ++ ----------. \/\ .
# \\ \ \ /\ /
# \\ \ \ /
# \\ \ \ /
# -============'
#
# Copyright (c) 2025 Hevake and contributors, all rights reserved.
#
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
# Use of this source code is governed by MIT license that can be found
# in the LICENSE file in the root of the source tree. All contributing
# project authors may be found in the CONTRIBUTORS.md file in the root
# of the source tree.
#
# 打包日志文件脚本
if [ $# -lt 2 ]; then
echo "Usage: $0 <log_path> <log_prefix> [log_file_num] [zip_file_num]"
echo "Exp : $0 /var/log/ sample"
echo "Exp : $0 /var/log/ sample 5 10"
exit
fi
log_path=$1
log_prefix=$2
log_file_num=5
zip_file_num=10
[ $# -ge 3 ] && log_file_num=$3
[ $# -ge 4 ] && zip_file_num=$4
if [ ! -d $log_path ]; then
echo "Error: path '$log_path' not exist"
exit
fi
cd $log_path
log_files=(`ls -t ${log_prefix}.*.log*`)
echo ${log_files[*]}
if [ ${#log_files[*]} -gt ${log_file_num} ]; then
unset log_files[0]
zip_filename=${log_prefix}.log.1.zip
if [ -f ${zip_filename} ]; then
zip_files=(`ls ${log_prefix}.log.*.zip`)
index=${#zip_files[*]}
while [ ${index} -ge 1 ]; do
if [ ${index} -ge ${zip_file_num} ]; then
rm ${log_prefix}.log.${index}.zip
else
mv ${log_prefix}.log.${index}.zip ${log_prefix}.log.$((index+1)).zip
fi
index=$((index-1))
done
fi
zip ${zip_filename} ${log_files[*]}
rm ${log_files[*]}
echo "Info: done"
else
echo "Info: skip"
fi