forked from zfsnap/zfsnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfSnap.sh
executable file
·390 lines (335 loc) · 10.8 KB
/
zfSnap.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/sh
# "THE BEER-WARE LICENSE":
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Aldis Berjoza
# wiki: https://github.com/graudeejs/zfSnap/wiki
# repository: https://github.com/graudeejs/zfSnap
# Bug tracking: https://github.com/graudeejs/zfSnap/issues
# feedback email: [email protected]
readonly VERSION=1.10.3
readonly zfs_cmd=/sbin/zfs
readonly zpool_cmd=/sbin/zpool
OS=`uname`
case $OS in
'FreeBSD')
ESED='sed -E'
;;
'SunOS' | 'Linux')
ESED='sed -r'
;;
*)
echo "ERR: Your OS isn't supported" > /dev/stderr
exit 1
;;
esac
s2time() {
# convert seconds to human readable time
xtime=$1
years=$(($xtime / 31536000))
xtime=$(($xtime % 31536000))
[ ${years:-0} -gt 0 ] && years="${years}y" || years=""
months=$(($xtime / 2592000))
xtime=$(($xtime % 2592000))
[ ${months:-0} -gt 0 ] && months="${months}m" || months=""
days=$(($xtime / 86400))
xtime=$(($xtime % 86400))
[ ${days:-0} -gt 0 ] && days="${days}d" || days=""
hours=$(($xtime / 3600))
xtime=$(($xtime % 3600))
[ ${hours:-0} -gt 0 ] && hours="${hours}h" || hours=""
minutes=$(($xtime / 60))
[ ${minutes:-0} -gt 0 ] && minutes="${minutes}M" || minutes=""
seconds=$(($xtime % 60))
[ ${seconds:-0} -gt 0 ] && seconds="${seconds}s" || seconds=""
echo "${years}${months}${days}${hours}${minutes}${seconds}"
}
time2s() {
# convert human readable time to seconds
echo "$1" | sed -e 's/y/*31536000+/g; s/m/*2592000+/g; s/w/*604800+/g; s/d/*86400+/g; s/h/*3600+/g; s/M/*60+/g; s/s//g; s/\+$//' | bc -l
}
date2timestamp() {
date_normal="`echo $1 | $ESED -e 's/\./:/g; s/(20[0-9][0-9]-[01][0-9]-[0-3][0-9])_([0-2][0-9]:[0-5][0-9]:[0-5][0-9])/\1 \2/'`"
case $OS in
'FreeBSD')
date -j -f '%Y-%m-%d %H:%M:%S' "$date_normal" '+%s'
;;
*)
date --date "$date_normal" '+%s'
;;
esac
}
help() {
cat << EOF
${0##*/} v${VERSION} by Aldis Berjoza
Syntax:
${0##*/} [ generic options ] [ options ] zpool/filesystem ...
GENERIC OPTIONS:
-d = Delete old snapshots
-e = Return number of failed actions as exit code.
-F age = Force delete all snapshots exceeding age
-n = Only show actions that would be performed
-s = Don't do anything on pools running resilver
-S = Don't do anything on pools running scrub
-v = Verbose output
-z = Force new snapshots to have 00 seconds!
-zpool28fix = Workaround for zpool v28 zfs destroy -r bug
OPTIONS:
-a ttl = Set how long snapshot should be kept
-D pool/fs = Delete all zfSnap snapshots of specific pool/fs (ignore ttl)
-p prefix = Use prefix for snapshots after this switch
-P = Don't use prefix for snapshots after this switch
-r = Create recursive snapshots for all zfs file systems that
fallow this switch
-R = Create non-recursive snapshots for all zfs file systems that
fallow this switch
LINKS:
wiki: https://github.com/graudeejs/zfSnap/wiki
repository: https://github.com/graudeejs/zfSnap
Bug tracking: https://github.com/graudeejs/zfSnap/issues
feedback email: [email protected]
EOF
exit 0
}
rm_zfs_snapshot() {
if [ $zpool28fix -eq 1 -a "$1" = '-r' ]; then
# get rid of '-r' parameter
rm_zfs_snapshot $2
return
fi
if [ "$1" = '-r' ]; then
skip_pool $2 || return 1
else
skip_pool $1 || return 1
fi
zfs_destroy="$zfs_cmd destroy $*"
# hardening: make really, really sure we are deleting snapshot
if echo $i | grep -q -e '@'; then
if [ $dry_run -eq 0 ]; then
if $zfs_destroy > /dev/stderr; then
[ $verbose -ne 0 ] && echo "$zfs_destroy ... DONE"
else
[ $verbose -ne 0 ] && echo "$zfs_destroy ... FAIL"
[ $count_failures -ne 0 ] && failures=$(($failures + 1))
fi
else
echo "$zfs_destroy"
fi
else
echo "ERR: trying to delete zfs pool or filesystem? WTF?" > /dev/stderr
echo " This is bug, we definitely don't want that." > /dev/stderr
echo " Please report it to [email protected]" > /dev/stderr
echo " Don't panic, nothing was deleted :)" > /dev/stderr
[ $count_failures -ne 0 -a $failures > 0 ] && exit $failures
exit 1
fi
}
skip_pool() {
# more like skip pool???
if [ $scrub_skip -ne 0 ]; then
for i in $scrub_pools; do
if [ `echo $1 | sed -e 's#/.*$##; s/@.*//'` = $i ]; then
[ $verbose -ne 0 ] && echo "NOTE: No action will be performed on '$1'. Scrub is running on pool." > /dev/stderr
return 1
fi
done
fi
if [ $resilver_skip -ne 0 ]; then
for i in $resilver_pools; do
if [ `echo $1 | sed -e 's#/.*$##; s/@.*//'` = $i ]; then
[ $verbose -ne 0 ] && echo "NOTE: No action will be performed on '$1'. Resilver is running on pool." > /dev/stderr
return 1
fi
done
fi
return 0
}
[ $# = 0 ] && help
[ "$1" = '-h' -o $1 = "--help" ] && help
ttl='1m' # default snapshot ttl
force_delete_snapshots_age=-1 # Delete snapshots older than x seconds. -1 means NO
delete_snapshots=0 # Delete old snapshots? 0 = NO
delete_specific_snapshots=0 # Delete specific snapshots? 0 = NO
verbose=0 # Verbose output? 0 = NO
dry_run=0 # Dry run? 0 = NO
prefx="" # Default prefix
prefxes="" # List of prefixes
delete_specific_fs_snapshots="" # List of specific snapshots to delete
delete_specific_fs_snapshots_recursively="" # List of specific snapshots to delete recursively
zero_seconds=0 # Should new snapshots always have 00 seconds? 0 = NO
scrub_pools="" # List of pools that are in precess of scrubing
resilver_pools="" # List of pools that are in process of resilvering
pools="" # List of pools
get_pools=0 # Should I get list of pools? 0 = NO.
resilver_skip=0 # Should I skip processing pools in process of resilvering. 0 = NO
scrub_skip=0 # Should I skip processing pools in process of scrubing. 0 = NO
failures=0 # Number of failed actions.
count_failures=0 # Should I coundt failed actions? 0 = NO
zpool28fix=0 # Workaround for zpool v28 zfs destroy -r bug
while [ "$1" = '-d' -o "$1" = '-v' -o "$1" = '-n' -o "$1" = '-F' -o "$1" = '-z' -o "$1" = '-s' -o "$1" = '-S' -o "$1" = '-e' -o "$1" = '-zpool28fix' ]; do
case "$1" in
'-d')
delete_snapshots=1
shift
;;
'-v')
verbose=1
shift
;;
'-n')
dry_run=1
shift
;;
'-F')
force_delete_snapshots_age=`time2s $2`
shift 2
;;
'-z')
zero_seconds=1
shift
;;
'-s')
get_pools=1
resilver_skip=1
shift
;;
'-S')
get_pools=1
scrub_skip=1
shift
;;
'-e')
count_failures=1
shift
;;
'-zpool28fix')
zpool28fix=1
shift
;;
esac
done
if [ $get_pools -ne 0 ]; then
pools=`$zpool_cmd list -H -o name`
for i in $pools; do
if [ $resilver_skip -ne 0 ]; then
$zpool_cmd status $i | grep -q -e 'resilver in progress' && resilber_pools="$resilver_pools $i"
fi
if [ $scrub_skip -ne 0 ]; then
$zpool_cmd status $i | grep -q -e 'scrub in progress' && scrub_pools="$scrub_pools $i"
fi
done
fi
readonly date_pattern='20[0-9][0-9]-[01][0-9]-[0-3][0-9]_[0-2][0-9]\.[0-5][0-9]\.[0-5][0-9]'
if [ $zero_seconds -eq 0 ]; then
readonly tfrmt='%Y-%m-%d_%H.%M.%S'
else
readonly tfrmt='%Y-%m-%d_%H.%M.00'
fi
readonly htime_pattern='([0-9]+y)?([0-9]+m)?([0-9]+w)?([0-9]+d)?([0-9]+h)?([0-9]+M)?([0-9]+[s]?)?'
[ $dry_run -ne 0 ] && zfs_list=`$zfs_cmd list -H -o name`
ntime=`date "+$tfrmt"`
while [ "$1" ]; do
while [ "$1" = '-r' -o "$1" = '-R' -o "$1" = '-a' -o "$1" = '-p' -o "$1" = '-P' -o "$1" = '-D' ]; do
case "$1" in
'-r')
zopt='-r'
shift
;;
'-R')
zopt=''
shift
;;
'-a')
ttl="$2"
echo "$ttl" | grep -q -E -e "^[0-9]+$" && ttl=`s2time $ttl`
shift 2
;;
'-p')
prefx="$2"
prefxes="$prefxes|$prefx"
shift 2
;;
'-P')
prefx=""
shift
;;
'-D')
if [ "$zopt" != '-r' ]; then
delete_specific_fs_snapshots="$delete_specific_fs_snapshots $2"
else
delete_specific_fs_snapshots_recursively="$delete_specific_fs_snapshots_recursively $2"
fi
shift 2
;;
esac
done
# create snapshots
if [ $1 ]; then
if skip_pool $1; then
if [ $1 = `echo $1 | $ESED -e 's/^-//'` ]; then
zfs_snapshot="$zfs_cmd snapshot $zopt $1@${prefx}${ntime}--${ttl}${postfx}"
if [ $dry_run -eq 0 ]; then
if $zfs_snapshot > /dev/stderr; then
[ $verbose -ne 0 ] && echo "$zfs_snapshot ... DONE"
else
[ $verbose -ne 0 ] && echo "$zfs_snapshot ... FAIL"
[ $count_failures -ne 0 ] && failures=$(($failures + 1))
fi
else
printf "%s\n" $zfs_list | grep -m 1 -q -E -e "^$1$" \
&& echo "$zfs_snapshot" \
|| echo "ERR: Looks like zfs filesystem '$1' doesn't exist" > /dev/stderr
fi
else
echo "WARN: '$1' doesn't look like valid argument. Ignoring" > /dev/stderr
fi
fi
shift
fi
done
prefxes=`echo "$prefxes" | sed -e 's/^\|//'`
# delete snapshots
if [ $delete_snapshots -ne 0 -o $force_delete_snapshots_age -ne -1 ]; then
if [ $zpool28fix -eq 0 ]; then
zfs_snapshots=`$zfs_cmd list -H -o name -t snapshot | grep -E -e "^.*@(${prefxes})?${date_pattern}--${htime_pattern}$" | sed -e 's#/.*@#@#'`
else
zfs_snapshots=`$zfs_cmd list -H -o name -t snapshot | grep -E -e "^.*@(${prefxes})?${date_pattern}--${htime_pattern}$"`
fi
current_time=`date +%s`
for i in `echo $zfs_snapshots | xargs printf "%s\n" | $ESED -e "s/^.*@//" | sort -u`; do
create_time=$(date2timestamp `echo "$i" | $ESED -e "s/--${htime_pattern}$//; s/^(${prefxes})?//"`)
if [ $delete_snapshots -ne 0 ]; then
stay_time=$(time2s `echo $i | $ESED -e "s/^(${prefxes})?${date_pattern}--//"`)
[ $current_time -gt $(($create_time + $stay_time)) ] \
&& rm_snapshot_pattern="$rm_snapshot_pattern $i"
fi
if [ "$force_delete_snapshots_age" -ne -1 ]; then
[ $current_time -gt $(($create_time + $force_delete_snapshots_age)) ] \
&& rm_snapshot_pattern="$rm_snapshot_pattern $i"
fi
done
if [ "$rm_snapshot_pattern" != '' ]; then
rm_snapshots=$(echo $zfs_snapshots | xargs printf '%s\n' | grep -E -e "@`echo $rm_snapshot_pattern | sed -e 's/ /|/g'`" | sort -u)
for i in $rm_snapshots; do
rm_zfs_snapshot -r $i
done
fi
fi
# delete all snap
if [ "$delete_specific_snapshots" != '' ]; then
if [ "$delete_specific_fs_snapshots" != '' ]; then
rm_snapshots=`$zfs_cmd list -H -o name -t snapshot | grep -E -e "^($(echo "$delete_specific_fs_snapshots" | tr ' ' '|'))@(${prefxes})?${date_pattern}--${htime_pattern}$"`
for i in $rm_snapshots; do
rm_zfs_snapshot $i
done
fi
if [ "$delete_specific_fs_snapshots_recursively" != '' ]; then
rm_snapshots=`$zfs_cmd list -H -o name -t snapshot | grep -E -e "^($(echo "$delete_specific_fs_snapshots_recursively" | tr ' ' '|'))@(${prefxes})?${date_pattern}--${htime_pattern}$"`
for i in $rm_snapshots; do
rm_zfs_snapshot -r $i
done
fi
fi
[ $count_failures -ne 0 ] && exit $failures
exit 0
# vim: set ts=4 sw=4: