-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs_clean_snap
executable file
·288 lines (272 loc) · 8.39 KB
/
zfs_clean_snap
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/sh
#
# destroy snapshots not corresponding to the expression given in the volume given
#
usage() {
echo "usage: $0 [-r] [-g \"GREP_EXPR\"] [-d] [-h] KEEPEXPR ZFSVOLUME"
echo "eg: $0 5l10d5w3m2y ZLIP-ftp/sav"
echo " KEEPEXPR: [<nblast>l][<nbhours>h][<nbdays>d][<nbweeks>w][<nbmonths>m][<nbyears>y]"
echo " eg: 5h10d4w3m2y for 5 hours, 10 days, 4 weeks, 3 months and 2 years"
echo " eg: 4w3m2y for 4 weeks, 3 months and 2 years"
echo " eg: 8l3h3m2y for 8 last, 3 hours, 3 months and 2 years"
echo ""
echo " -r (env CLEANSNAP_RECURSE): will destroy recursively (zfs destroy -r)"
echo " -R (env CLEANSNAP_RECURSE_MANUALLY): will destroy recursively (zfs destroy -r) AND re-search in each subvolume"
echo " -g GREP_EXPR (env CLEANSNAP_GREP): will use it to select snapshots (by default match GMT-YYYY.MM.DD-HH.mm.ss)"
echo " -v N (env CLEANSNAP_VERBOSE): will display more information (if N > 0 for now)"
echo " -d (env CLEANSNAP_FAKE): no destroy command will be launched for real, but displayed"
echo " -h you are here :)"
exit ${1:-1}
}
CLEANSNAP_VERBOSE=${CLEANSNAP_VERBOSE:-0}
ORIGARGS=$@
while getopts rRg:dv:h option
do
case $option in
r)
CLEANSNAP_RECURSE=yes
;;
R)
CLEANSNAP_RECURSE_MANUALLY=yes
CLEANSNAP_RECURSE=yes
;;
g)
CLEANSNAP_GREP=$OPTARG
;;
d)
CLEANSNAP_FAKE=1
;;
v)
CLEANSNAP_VERBOSE=$OPTARG
;;
h)
usage 0
;;
esac
done
# shift getopt args from ARGV
shift $(expr $OPTIND - 1)
if [ $# -ne 2 ]; then
usage
fi
if ! ( echo $1 | egrep '^([[:digit:]]+l)?([[:digit:]]+h)?([[:digit:]]+d)?([[:digit:]]+w)?([[:digit:]]+m)?([[:digit:]]+y)?$' > /dev/null ) || \
[ -z "$1" ]; then
echo "expression \"$1\" doesn't match [<nblast>l][<nbhours>h][<nbdays>d][<nbweeks>w][<nbmonths>m][<nbyears>y]:("
usage
fi
getsnapdate() {
ZTIME=$(LC_TIME=C zfs list -H -o creation $1| cut -c 1-21 2>/dev/null)
[ -z "$ZTIME" ] && return 1
LC_TIME=C date -jnu -f '%a %b %e %R %Y' +%s "$(LC_TIME=C zfs list -H -o creation $1| cut -c 1-21)"
}
say() {
if [ -z "$CLEANSNAP_FAKE" ]; then
if [ -z "$TERM" ]; then
echo "$0[$$]" $@ >> /var/log/clean_zfs_snaps.log
else
echo "$0[$$]" $@ | tee -a /var/log/clean_zfs_snaps.log
fi
else
echo $@
fi
}
doit() {
say $@
if [ -z "$CLEANSNAP_FAKE" ]; then
eval $@
fi
}
debug() {
if [ $CLEANSNAP_VERBOSE -gt 0 ]; then
[ -z "$TERM" ] && say $@ || echo $@
fi
}
error_with_logs() {
say $@;
echo "zfs_clean_snap $EXPR $VOL: $*" | /usr/local/bin/mutt -s "Erreur de zfs_clean_snap sur $(hostname)" sysadm@ec-m.fr -a $(for log in $TODELLIST $CHECKLIST $FULLLIST $WORKLIST; do echo "$log "; done)
}
ID=$(echo $2 | sed 's@/@_@g')
export CLEANSNAP_RECURSE_MANUALLY CLEANSNAP_RECURSE CLEANSNAP_GREP CLEANSNAP_FAKE CLEANSNAP_VERBOSE ID
# lock (not more than one by pool)
if ! [ "$LOCKED" != "YAISSE" ]; then
exec lockf -s -t 1600 /tmp/zfs_clean_snap_${ID}.lock $0 $*
fi
TODELLIST=/tmp/zfs_clean_snap_${ID}.todel
CHECKLIST=/tmp/zfs_clean_snap_${ID}.tocheck
FULLLIST=/tmp/zfs_clean_snap_${ID}.fullist
WORKLIST=/tmp/zfs_clean_snap_${ID}.list
for tmpfile in $TODELLIST $CHECKLIST $FULLLIST $WORKLIST; do
if [ -f $tmpfile ]; then
echo "derniere execution non finie: $tmpfile existe" 1>&2
exit 1
fi
touch $tmpfile
done
say "$(date) $0 $*"
EXPR=$1
VOL=$2
NBLAST=${EXPR%%l*}
[ "$NBLAST" = "$EXPR" ] && NBLAST=0
EXPR=${EXPR##*l}
NBHOURS=${EXPR%%h*}
[ "$NBHOURS" = "$EXPR" ] && NBHOURS=0
EXPR=${EXPR##*h}
NBDAYS=${EXPR%%d*}
[ "$NBDAYS" = "$EXPR" ] && NBDAYS=0
EXPR=${EXPR##*d}
NBWEEKS=${EXPR%%w*}
[ "$NBWEEKS" = "$EXPR" ] && NBWEEKS=0
EXPR=${EXPR##*w}
NBMONTHS=${EXPR%%m*}
[ "$NBMONTHS" = "$EXPR" ] && NBMONTHS=0
EXPR=${EXPR##*m}
NBYEARS=${EXPR%%y*}
[ "$NBYEARS" = "$EXPR" ] && NBYEARS=0
#EXPR=${EXPR##*y}
debug "Will save $NBLAST last, $NBHOURS hours, $NBDAYS days, $NBWEEKS weeks, $NBMONTHS months and $NBYEARS years of snapshots"
GREPSNAP=${CLEANSNAP_GREP:-'GMT-2[0-9][0-9][0-9]\.[01][0-9]\.[0-3][0-9]-[0-2][0-9]\.[0-5][0-9]\.[0-5][0-9]$'}
OLDHOURLY=$(date -u -v -$(( NBHOURS +1 ))H +%s)
OLDDAILY=$(date -u -v -$(( NBDAYS +1 ))d +%s)
OLDWEEKLY=$(date -u -v -$(( NBWEEKS * 7 + 2 ))d +%s)
OLDMONTHLY=$(date -u -v -$(( NBMONTHS * 31 ))d +%s)
OLDYEARLY=$(date -u -v -$(( NBYEARS * 365 +30 ))d +%s)
# la fonction principale
# usage: cleanupvol volume list
cleanupvol() {
volume=$1
TMPLIST=$WORKLIST.tmp
: > $TMPLIST
local NBLAST NBHOURS NBDAYS NBWEEKS NBMONTHS NBYEARS
SNAPNUM=$(grep "^$volume@" $WORKLIST | wc -l)
grep "^$volume@" $WORKLIST | while read name; do
# skip if already processed
if grep -q "^$name" $CHECKLIST; then
continue
fi
SNAPNUM=$(( $SNAPNUM - 1 ))
ts=$(getsnapdate $name)
[ -z "$ts" ] && continue
KEEPIT=""
# skip if format doesn't match
if [ $? -ne 0 ] || [ -z "$ts" ]; then
continue
fi
if [ $NBLAST -gt $SNAPNUM ]; then
KEEPIT=$KEEPIT" last"
NBLAST=$(($NBLAST - 1))
fi
if [ $NBHOURS -gt 0 ]; then
if [ $ts -ge $OLDHOURLY ]; then
if [ -z "$LASTHOUR" ]; then
LASTHOUR=$ts
KEEPIT=$KEEPIT" hourly"
NBHOURS=$(($NBHOURS - 1))
elif [ $ts -ge $(date -ju -f %s -v +55M +%s $LASTHOUR) ]; then
LASTHOUR=$ts
KEEPIT=$KEEPIT" hourly"
NBHOURS=$(($NBHOURS - 1))
fi
fi
fi
# daily: 1 day after than $LASTDAY
if [ $NBDAYS -gt 0 ]; then
if [ $ts -ge $OLDDAILY ]; then
if [ -z "$LASTDAY" ]; then
LASTDAY=$ts
KEEPIT=$KEEPIT" daily"
NBDAYS=$(($NBDAYS - 1))
elif [ $ts -ge $(date -ju -f %s -v +23H +%s $LASTDAY) ]; then
LASTDAY=$ts
KEEPIT=$KEEPIT" daily"
NBDAYS=$(($NBDAYS - 1))
fi
fi
fi
# weekly: 1 week after than $LASTWEEK
if [ $NBWEEKS -gt 0 ]; then
if [ $ts -ge $OLDWEEKLY ]; then
if [ -z "$LASTWEEK" ]; then
LASTWEEK=$ts
KEEPIT=$KEEPIT" weekly"
NBWEEKS=$(($NBWEEKS - 1))
elif [ $ts -ge $(date -ju -f %s -v +166H +%s $LASTWEEK) ]; then
LASTWEEK=$ts
KEEPIT=$KEEPIT" weekly"
NBWEEKS=$(($NBWEEKS - 1))
fi
fi
fi
# monthly: 1 month after than $LASTMONTH
if [ $NBMONTHS -gt 0 ]; then
if [ $ts -ge $OLDMONTHLY ]; then
if [ -z "$LASTMONTH" ]; then
LASTMONTH=$ts
KEEPIT=$KEEPIT" monthly"
NBMONTHS=$(($NBMONTHS - 1))
elif [ $ts -ge $(date -ju -f %s -v +30d +%s $LASTMONTH) ]; then
LASTMONTH=$ts
KEEPIT=$KEEPIT" monthly"
NBMONTHS=$(($NBMONTHS - 1))
fi
fi
fi
# yearly: 1 year after than $LASTYEAR
if [ $NBYEARS -gt 0 ]; then
if [ $ts -ge $OLDYEARLY ]; then
if [ -z "$LASTYEAR" ]; then
LASTYEAR=$ts
KEEPIT=$KEEPIT" yearly"
NBYEARS=$(($NBYEARS - 1))
elif [ $ts -ge $(date -ju -f %s -v +364d +%s $LASTYEAR) ]; then
LASTYEAR=$ts
KEEPIT=$KEEPIT" yearly"
NBYEARS=$(($NBYEARS - 1))
fi
fi
fi
if [ -n "$KEEPIT" ]; then
debug "Keeping $name ($KEEPIT)"
else
debug "will destroy ${CLEANSNAP_RECURSE:+-r} $name ($ts)"
echo "$name" >> $TODELLIST
if [ -n "$CLEANSNAP_RECURSE" ]; then
grep -v "^${volume}.*@${name#*@}$" $WORKLIST > $TMPLIST
grep "^${volume}.*@${name#*@}$" $WORKLIST >> $CHECKLIST
else
grep -v "^${name}$" $WORKLIST > $TMPLIST
grep "^${name}$" $WORKLIST >> $CHECKLIST
fi
fi
done
test -s $TMPLIST && mv $TMPLIST $WORKLIST || rm $TMPLIST
}
# c'est parti
[ "${CLEANSNAP_RECURSE_MANUALLY}" = "yes" ] || LIMITOPT=" -d1"
zfs list -t snapshot -o name -r${LIMITOPT} $VOL | grep '^'$VOL'.*@'$GREPSNAP > $FULLLIST
cp $FULLLIST $WORKLIST
for vol in $(sed 's/@.*$//' $FULLLIST | uniq); do
debug "Cleaning $vol"
cleanupvol $vol
done
ERRS=""
if [ -s $TODELLIST ]; then
# A ce moment on efface pour de vrai
for zsnap in $(cat $TODELLIST); do
if ! doit "zfs destroy -d${CLEANSNAP_RECURSE:+r} $zsnap"; then
ERRS=$ERRS" "$zsnap
fi
done
if [ ! -z "$ERRS" ]; then
error_with_logs "Erreurs a la suppression de $ERRS :("
else
# check
debug "checking"
zfs list -t snapshot -o name -r $VOL | grep '^'$VOL'.*@'$GREPSNAP > $WORKLIST
CHECK=$(cat $CHECKLIST $WORKLIST | sort | uniq -d)
if [ -n "$CHECK" ]; then
debug "checks: snapshots en trop a la fin: $CHECK"
fi
say "done :)"
fi
fi
rm $FULLLIST $WORKLIST $TODELLIST $CHECKLIST