-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteoldfiles.sh
More file actions
156 lines (131 loc) · 3.38 KB
/
Copy pathdeleteoldfiles.sh
File metadata and controls
156 lines (131 loc) · 3.38 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
#!/bin/bash
#
###############################################################################
# Author : Louwrentius
# Contact : louwrentius@gmail.com
# Initial release : August 2011
# Licence : Simplified BSD License
###############################################################################
VERSION=1.01
#
# Mounted volume to be monitored.
#
MOUNT="$1"
#
# Maximum threshold of volume used as an integer that represents a percentage:
# 95 = 95%.
#
MAX_USAGE="$2"
#
# Failsafe mechansim. Delete a maxium of MAX_CYCLES files, raise an error after
# that. Prevents possible runaway script. Disable by choosing a high value.
#
MAX_CYCLES=10
show_header () {
echo
echo DELETE OLD FILES $VERSION
echo
}
show_header
reset () {
CYCLES=0
OLDEST_FILE=""
OLDEST_DATE=0
ARCH=`uname`
}
reset
if [ -z "$MOUNT" ] || [ ! -e "$MOUNT" ] || [ ! -d "$MOUNT" ] || [ -z "$MAX_USAGE" ]
then
echo "Usage: $0 <mountpoint> <threshold>"
echo "Where threshold is a percentage."
echo
echo "Example: $0 /storage 90"
echo "If disk usage of /storage exceeds 90% the oldest"
echo "file(s) will be deleted until usage is below 90%."
echo
echo "Wrong command line arguments or another error:"
echo
echo "- Directory not provided as argument or"
echo "- Directory does not exist or"
echo "- Argument is not a directory or"
echo "- no/wrong percentage supplied as argument."
echo
exit 1
fi
check_capacity () {
USAGE=`df -h | grep "$MOUNT" | awk '{ print $5 }' | sed s/%//g`
if [ ! "$?" == "0" ]
then
echo "Error: mountpoint $MOUNT not found in df output."
exit 1
fi
if [ -z "$USAGE" ]
then
echo "Didn't get usage information of $MOUNT"
echo "Mountpoint does not exist or please remove trailing slash."
exit 1
fi
if [ "$USAGE" -gt "$MAX_USAGE" ]
then
echo "Usage of $USAGE% exceeded limit of $MAX_USAGE percent."
return 0
else
echo "Usage of $USAGE% is within limit of $MAX_USAGE percent."
return 1
fi
}
check_age () {
FILE="$1"
if [ "$ARCH" == "Linux" ]
then
FILE_DATE=`stat -c %Z "$FILE"`
elif [ "$ARCH" == "Darwin" ]
then
FILE_DATE=`stat -f %Sm -t %s "$FILE"`
else
echo "Error: unsupported architecture."
echo "Send a patch for the correct stat arguments for your architecture."
fi
NOW=`date +%s`
AGE=$((NOW-FILE_DATE))
if [ "$AGE" -gt "$OLDEST_DATE" ]
then
export OLDEST_DATE="$AGE"
export OLDEST_FILE="$FILE"
fi
}
process_file () {
FILE="$1"
#
# Replace the following commands with wathever you want to do with
# this file. You can delete files but also move files or do something else.
#
echo "Deleting oldest file $FILE"
rm -f "$FILE"
}
while check_capacity
do
if [ "$CYCLES" -gt "$MAX_CYCLES" ]
then
echo "Error: after $MAX_CYCLES deleted files still not enough free space."
exit 1
fi
reset
FILES=`find "$MOUNT" -type f`
IFS=$'\n'
for x in $FILES
do
check_age "$x"
done
if [ -e "$OLDEST_FILE" ]
then
#
# Do something with file.
#
process_file "$OLDEST_FILE"
else
echo "Error: somehow, item $OLDEST_FILE disappeared."
fi
((CYCLES++))
done
echo