-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_expired_data.bash
executable file
·100 lines (91 loc) · 2.75 KB
/
check_expired_data.bash
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
#!/bin/bash
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- #
# check_expired_data.bash #
# #
# Use this script to find and remove reports older than the #
# specified number of days. Please use with caution. #
# #
# You can install this as a cron job by making a wrapper script #
# and placting the wrapper script in /etc/cron.daily #
# #
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- #
#set -veux
usage="
-d <directory> Find data within this directory (required)
-t <days> Find data this many days old or older (required)
-f Delete files (not a default option)
-v Print files
See the files you would delete:
$0 -d /home/lanforge/report-data -t 11 -v
Actually delete the files:
$0 -d /home/lanforge/report-data -t 11 -f
You may create a script in /etc/cron.daily like this:
----- ----- ----- ----- ----- ----- ----- ----- -----
#!/bin/bash
LF='/home/lanforge'
E='/home/lanforge/scripts/check_expired_data.bash'
$E -d \$LF/report-data -t 11 -f
$E -d \$LF/html-reports -t 11 -f
----- ----- ----- ----- ----- ----- ----- ----- -----
"
if (( $# < 2 )); then
echo "$usage"
exit 1
fi
actually_delete=0
data_dir=""
days_old=0 # zero will mean don't do anything
verbose=0
while getopts "d:ht:fv" arg; do
case $arg in
h)
echo "$usage"
exit 0
;;
d)
if [[ x$OPTARG = x ]]; then
echo "data dir required"
exit 1
fi
data_dir="$OPTARG"
;;
f)
actually_delete=1
;;
t)
if [[ x$OPTARG = x ]] || (( $OPTARG < 1 )); then
echo "time in days required, one or more days"
exit 1
fi
days_old="$OPTARG"
;;
v)
verbose=1
;;
*)
echo "Unkown option $arg"
exit 1
esac
done
if [[ ! -d $data_dir ]]; then
echo "Directory not found: $data_dir"
exit 1
fi
#set -veux
files_list=()
mapfile -d '' files_list < <(find -L "$data_dir" -type f -mtime +$days_old -print0)
if (( "${#files_list[@]}" < 1 )); then
echo "No files found"
exit 0
fi
echo "Found $((${#files_list[@]} + 1 )) files"
if (( $verbose == 1 )) ; then
printf "%s\n" "${files_list[@]}" | xargs -n 3 echo
fi
if (( $actually_delete == 1 )); then
echo -n "Deleting files..."
printf "%s\n" "${files_list[@]}" | xargs rm
echo "done"
else
echo "Not deleting files."
fi