File tree Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Expand file tree Collapse file tree 1 file changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bash
2+
3+ # Usage: bash rotate.sh /mnt/logs/curvefs/client 86400
4+
5+ # ########################### GLOBAL VARIABLES
6+ g_log_dir=" $1 "
7+ g_color_yellow=$( printf ' \033[33m' )
8+ g_color_red=$( printf ' \033[31m' )
9+ g_color_normal=$( printf ' \033[0m' )
10+
11+ # ########################### BASIC FUNCTIONS
12+ msg () {
13+ printf ' %b' " ${1} " >&2
14+ }
15+
16+ success () {
17+ msg " ${g_color_yellow} [✔]${g_color_normal} ${1}${2} "
18+ }
19+
20+ die () {
21+ msg " ${g_color_red} [✘]${g_color_normal} ${1}${2} "
22+ exit 1
23+ }
24+
25+ # ########################### FUNCTIONS
26+ precheck () {
27+ local log_dir=" $1 "
28+ if [ ! -d " ${log_dir} " ]; then
29+ die " Log directory ${log_dir} does not exist.\n"
30+ fi
31+
32+ if [ ! -w " ${log_dir} " ]; then
33+ die " Log directory ${log_dir} is not writable.\n"
34+ fi
35+ }
36+
37+ expired () {
38+ local path=" $1 "
39+ local timeout=" $2 "
40+ local mtime=$( stat -c %Y " ${path} " )
41+ local now=$( date +%s)
42+ if (( now - mtime > timeout )) ; then
43+ return 0
44+ fi
45+ return 1
46+ }
47+
48+ delete () {
49+ local path=" $1 "
50+ rm " ${path} "
51+ success " Delete ${path} \n"
52+ }
53+
54+ rotate () {
55+ local log_dir=" $1 "
56+ local timeout=" $2 "
57+ for file in $( ls " ${log_dir} " | grep -E ' (access|curve-fuse|aws)(.+)(\.log)?' ) ; do
58+ local path=" ${log_dir} /${file} "
59+ if expired " ${path} " " ${timeout} " ; then
60+ delete " ${path} "
61+ fi
62+ done
63+ }
64+
65+ main () {
66+ local log_dir=" $1 "
67+ local timeout=" $2 " # seconds
68+ precheck " ${log_dir} "
69+ rotate " ${log_dir} " " ${timeout} "
70+ }
71+
72+ # ########################### MAIN()
73+ main " $@ "
You can’t perform that action at this time.
0 commit comments