-
Notifications
You must be signed in to change notification settings - Fork 1
/
mr.wolf.sh
executable file
·248 lines (221 loc) · 6.58 KB
/
mr.wolf.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
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
#!/bin/bash -e
########################################################################
#** Version: 1.0
#* This script helps keeping a target directory clean. E.g. by cron:
#* `41 3 * * * [ -x /usr/local/bin/mr.wolf.sh ] && /usr/local/bin/mr.wolf.sh -a "last week" -f data.json -e /tmp"
#
# note: the frame for this script was auto-created with
# *https://github.com/inofix/admin-toolbox/blob/master/makebashscript.sh*
########################################################################
# author/copyright: <[email protected]>
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
########################################################################
[ "$1" == "debug" ] && shift && set -x
## variables ##
### you may copy the following variables into this file for having your own
### local config ...
#conffile=.mr.wolf.sh
### {{{
dryrun=1
needsroot=1
target_dir=""
file_pattern=""
file_type=""
older_than=""
older_than_date=""
remove_empty=1
timestamp=".timestamp"
### }}}
# Unsetting this helper variable
_pre=""
# The system tools we gladly use. Thank you!
declare -A sys_tools
sys_tools=( ["_cat"]="/bin/cat"
["_date"]="/bin/date"
["_find"]="/usr/bin/find"
["_grep"]="/bin/grep"
["_id"]="/usr/bin/id"
["_touch"]="/usr/bin/touch" )
# this tools get disabled in dry-run and sudo-ed for needsroot
danger_tools=( "_cat" )
# special case sudo (not mandatory)
_sudo="/usr/bin/sudo"
## functions ##
print_usage()
{
echo "usage: $0"
}
print_help()
{
print_usage
$_grep "^#\* " $0 | $_sed_forced 's;^#\*;;'
}
print_version()
{
$_grep "^#\*\* " $0 | $_sed 's;^#\*\*;;'
}
die()
{
echo "$@"
exit 1
}
error()
{
print_usage
echo ""
die "Error: $@"
}
## logic ##
## first set the system tools
for t in ${!sys_tools[@]} ; do
if [ -x "${sys_tools[$t]##* }" ] ; then
export ${t}="${sys_tools[$t]}"
else
error "Missing system tool: ${sys_tools[$t]##* } must be installed."
fi
done
[ ! -f "/etc/$conffile" ] || . "/etc/$conffile"
[ ! -f "/usr/etc/$conffile" ] || . "/usr/etc/$conffile"
[ ! -f "/usr/local/etc/$conffile" ] || . "/usr/local/etc/$conffile"
[ ! -f ~/"$conffile" ] || . ~/"$conffile"
[ ! -f "$conffile" ] || . "$conffile"
#* options:
while true ; do
case "$1" in
#* -c |--config conffile alternative config file
-c|--config)
shift
if [ -r "$1" ] ; then
. $1
else
die " config file $1 does not exist."
fi
;;
#* -e |--toggle-remove-empty remove/leave empty parent directories,
#* do just the contrary of what the config
#* sais (default w/o config: 'leave')
-e|--toggle-remove-empty)
if [ $remove_empty -eq 0 ] ; then
remove_empty=1
else
remove_empty=0
fi
;;
#* -a |--find-file-age pattern only consider files older than this
#* (see `date -d ..`)
-a|--find-file-age)
shift
if $_date -d "$1" 2>&1 > /dev/null ; then
older_than_date="$1"
else
error "Could not create the timestamp $1"
fi
;;
#* -o |--find-older timestampfile only consider files older than this
-o|--find-older)
shift
if [ -f "$1" ] ; then
older_than="! -newer $1 ! -name ${1##*/}"
else
error "Could not find regular timestamp file $1"
fi
;;
#* -f |--find-file-pattern pattern only consider file names similar to
#* (see `find <dir> -name ..`)
-f|--find-file-pattern)
shift
file_pattern="-name $1"
;;
#* -t |--find-file-type only consider files of this type
#* (see `find <dir> -type ..`)
-t|--find-file-type)
file_type=""
;;
#* -h |--help print this help
-h|--help)
print_help
exit 0
;;
#* -n |--dry-run do not change anything
-n|--dry-run)
dryrun=0
;;
#* -v |--version
-v|--version)
print_version
exit
;;
#* -- use target_dir from the config
--)
if [ -d "$target_dir" ] ; then
break
else
error "Directory '$target_dir' not found."
fi
;;
-*|--*)
error "option '$1' not supported"
;;
#* targetdirectory the directory to clean up (mandatory)
*)
if [ -d "$1" ] ; then
target_dir="$1"
break
else
error "Directory '$1' not found.."
fi
;;
esac
shift
done
if [ $dryrun -eq 0 ] ; then
_pre="echo "
fi
if [ $needsroot -eq 0 ] ; then
iam=$($_id -u)
if [ $iam -ne 0 ] ; then
if [ -x "$_sudo" ] ; then
_pre="$_pre $_sudo"
else
error "Priviledges missing: use ${_sudo}."
fi
fi
fi
for t in ${danger_tools[@]} ; do
export ${t}="$_pre ${sys_tools[$t]}"
done
[ ! -d "$target_dir" ] && error "No target directory provided.."
if [ -n "$older_than_date" ] ; then
if [ -z "${timestamp%%*/}" ] ; then
error "The temp file '$timestamp' must be at a valid location"
fi
if [ -n "${timestamp##/*}" ] ; then
timestamp="$target_dir/$timestamp"
fi
if $_touch -d "$older_than_date" "$timestamp" ; then
older_than="! -newer $timestamp ! -name ${timestamp##*/}"
else
error "Failed to create the temp file '$timestamp'"
fi
fi
if [ $dryrun -eq 0 ] ; then
echo "Not removing this files - try run"
# always consider the pattern as find would
set -f
$_find $target_dir $file_type $older_than $file_pattern
set +f
if [ $remove_empty -eq 0 ] ; then
echo "Not removing this directories - try run"
$_find $target_dir -type d -empty $older_than
fi
else
# always consider the pattern as find would
set -f
$_find $target_dir $file_type $older_than $file_pattern -delete
set +f
if [ $remove_empty -eq 0 ] ; then
$_find $target_dir -type d -empty $older_than -delete
fi
fi