-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs_asm
434 lines (395 loc) · 12.3 KB
/
zfs_asm
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
## Joe Nyilas, crafted this.
## An unpublished work.
## Prototyped and conceputalized 16-DEC-2006
# $Id: zfs_asm,v 0.8 2021/12/10 19:33:22 jnyilas Exp $
## The ZFS Automatic Snapshot Generator
## (Not the Automatic Snapshot System)
# Synopsis: Automatically create rolling snaphots for any mounted ZFS filesystem
# which have a user specified maximum lifetime. This lifetime is expressed
# in a simple and natural way, thanks to Gnu date.
# http://www.gnu.org/software/coreutils/
# Time syntax is as easy as:
# 1hour, 1day, 7week, etc.
# After this lifetime is reached, the expired zfs snapshot is automatically
# destroyed after it's replacement created.
# This completes the rolling snapshot cycle.
# Command line options:
# -t Set the TAG used to identify snapshots
# -e Set the expiration lifetime of snapshots
# -l Set report mode. This trumps other parameters and no snapshots
# will be created nor destroyed.
# -L Filter report for expired snapshots
# -a Consider all mounted ZFS datasets
# -A Consider all ZFS datasets, not just mounted ones
# -d Just destroy expired snapshots. Do not create any new snapshots.
# -s Just create new snapshots. Do not destroy any existing expired.
# -D Use with caution. This will destroy all snapshots!
# -f This will forcibly delete snapshots which have not yet expired.
# -r Use recursion for snapshot creation and deletion
# -v Increase verbosity.
##
## Customization Parameters
##
#Which date binary to use? For Solaris10, change this to gdate
#and ensure the binary is in the searchpath
DATE="date"
#Set default tag and expiration
#These are used as the defaults to create new snapshots
#unless specified differently on the command line.
EXPIRATION=1day
TAG="Auto_D"
# examples
## weekly
#"_W_"
## daily
#"_D_"
## monthly
#"_M_"
##
## End Customization
##
PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/bin
usage()
{
echo -e "\t$(basename "$0") [OPTION]... [ZFS_NAME]..." 1>&2
echo -e "\t\t-h prints a man page" 1>&2
echo "" 1>&2
exit 1
}
manpg()
{
echo "NAME" 1>&2
echo -e "\t$(basename "$0") - ZFS Automatic Snapshot Manager" 1>&2
echo "" 1>&2
echo "SYNOPSIS" 1>&2
echo -e "\t$(basename "$0") [OPTION]... [ZFS_NAME]..." 1>&2
echo "" 1>&2
echo "DESCRIPTION" 1>&2
echo -e "\tAutomatically create rolling snaphots for all (or any) specified ZFS
\tfilesystem which have a specified maximum expiration lifetime.
\tThis lifetime is expressed in a simple and natural way; 1hour, 6day,
\tetc. The general form is N<unit>, where N is a positive integer and
\t<unit> is any one of {sec min hour day week month year}.
\tThe default operation mode is as follows: A new snapshot is created
\tfor the specified ZFS having an expiration date \"${EXPIRATION}\" in the
\tfuture, and tagged with the name \"${TAG}\". After the successful
\tcreation of the snapshot, any managed and expired snapshots matching
\tthe tag on the specified (or all) ZFS are automatically destroyed.
\tThe default operation can be modified using the following options:"
echo "" 1>&2
echo "OPTIONS" 1>&2
echo -e "\t-l"
echo -e "\t List mode: Just report on managed snapshots" 1>&2
echo "" 1>&2
echo -e "\t-L"
echo -e "\t Filtered List mode: Just report on expired managed snapshots" 1>&2
echo "" 1>&2
echo -e "\t-s"
echo -e "\t Snapshot mode: Just create new snapshots" 1>&2
echo "" 1>&2
echo -e "\t-d"
echo -e "\t Destroy mode: Just destroy expired snapshots" 1>&2
echo "" 1>&2
echo -e "\t-D"
echo -e "\t DESTROY mode: Use with caution. Destroy *ALL* snapshots" 1>&2
echo "" 1>&2
echo -e "\t-t"
echo -e "\t Specify custom descriptive tag for the snapshot" 1>&2
echo "" 1>&2
echo -e "\t-e"
echo -e "\t Specify custom expiration date for the snapshot" 1>&2
echo "" 1>&2
echo -e "\t-a"
echo -e "\t Consider only mounted ZFS datasets and not a specified ZFS" 1>&2
echo -e "\t When in list mode, report on all tags, too" 1>&2
echo "" 1>&2
echo -e "\t-A"
echo -e "\t Consider all ZFS datasets (including unmounted) and not a specified ZFS" 1>&2
echo -e "\t When in list mode, report on all tags, too" 1>&2
echo "" 1>&2
echo -e "\t-f"
echo -e "\t Forcibly deletes unexpired snapshots matching the tag" 1>&2
echo "" 1>&2
echo -e "\t-r"
echo -e "\t Use recursion for snapshot creation and destruction" 1>&2
echo "" 1>&2
echo -e "\t-v"
echo -e "\t Verbose" 1>&2
echo "" 1>&2
exit 1
}
status()
{
if [[ $? -ne 0 ]]; then
echo "$*"
echo ""
exit 1
fi
}
#How close does the expiration time have to match to declare a snapshot
#expired?
#Due to differences in cron runs, etc, we can use a fudge factor:
#If the expiration time is within 60s of the current time, call it good
#enough and proceed. And yes, it needs to be a negative value.
S_TIME="-60"
#Normalize argv
LIST=0
ALL_ZFS=0
JUST_DESTROY_ALL=0
SNAPSHOT_ONLY=0
DESTROY_ONLY=0
FORCE=0
VERBOSE=0
TAG_SET=0
ZFS_ARG=""
#Parse arguments
while getopts t:e:lLaAdDfvshr i; do
case $i in
t) #Set the TAG used to identify snapshots
TAG=${OPTARG}
TAG_SET=1
;;
e) #Set the maximum lifetime of snapshots
EXPIRATION=${OPTARG}
${DATE} -d "+${EXPIRATION}" > /dev/null 2>&1
status "Invalid time spec. Try: 1hour, 1day, 1week, etc."
;;
l) #Set report mode. This trumps other parameters and no snapshots
#will be created nor destroyed.
LIST=1
LIST_EXPIRED=0
;;
L) #Set report mode. This trumps other parameters and no snapshots
#will be created nor destroyed.
LIST=1
LIST_EXPIRED=1
;;
a) #Consider all mounted ZFS
ALL_ZFS=1
FILTER_ZFS_MOUNT=1
;;
A) #Consider all ZFS, not just mounted
ALL_ZFS=1
FILTER_ZFS_MOUNT=0
;;
d) #Just destroy. Do not create any new snapshots.
DESTROY_ONLY=1
;;
s) #Just snapshot. Do not destroy.
SNAPSHOT_ONLY=1
;;
D) #Use with caution. This will destroy all snapshots!
JUST_DESTROY_ALL=1
;;
f) #This will forcibly delete snapshots which have not yet expired.
FORCE=1
;;
r) #Recursion
ZFS_ARG="-r"
;;
v) #Increase verbosity.
VERBOSE=1
;;
h) #Help.
manpg
;;
*) #Unrecognized parameter.
usage
;;
esac
done
shift $(( OPTIND - 1))
if [[ ${JUST_DESTROY_ALL} -eq 1 ]]; then
zlist=$(zfs list -t snapshot -H -o name)
if [[ -z "${zlist}" ]]; then
exit 0
fi
echo "This will *destroy* the following snapshots:"
echo "${zlist}"
echo -n "Are you sure you wish to proceed? y/n "
read foo
if [[ "${foo}" = "y" ]]; then
for i in ${zlist}; do
zfs destroy "$i" && echo "$i destroyed"
done
fi
exit 0
fi
##
## Error Checking
##
if [[ ${ALL_ZFS} -ne 1 && $# -eq 0 ]]; then
echo "Specify a zfs file system."
usage
fi
if [[ ${SNAPSHOT_ONLY} -eq 1 && ${DESTROY_ONLY} -eq 1 ]]; then
echo "You specified destroy only and snapshot only modes."
echo "These options are mutually exclusive."
usage
fi
if [[ ${LIST} -eq 0 && ${ALL_ZFS} -eq 1 && $# -ne 0 ]]; then
#Not in list mode, but specified all_zfs and a specific zfs
echo "You specified all ZFS and also provided a specific ZFS to consider."
echo "These are mutually exclusive options."
usage
fi
if [[ ${LIST} -eq 1 ]]; then
if [[ ${DESTROY_ONLY} -eq 1 ]]; then
echo "List mode trumps destroy mode."
fi
if [[ ${SNAPSHOT_ONLY} -eq 1 ]]; then
echo "List mode trumps snapshot mode."
fi
fi
#When listing, if the all_zfs option is specified, don't limit the return
#to the default tag, unless a tag is specified as another option.
if [[ ${LIST} -eq 1 ]]; then
#print the header for all cases
if [[ ${VERBOSE} -eq 1 ]]; then
printf "%41s %-15s %-18s%s %s\n" "Snapshot " Created Expires RIP USED
else
printf "%41s %-15s %-18s%s\n" "Snapshot " Created Expires RIP
fi
if [[ ${ALL_ZFS} -eq 1 ]]; then
# For list all, we want to be less specific, and so list all snaps and all tags
# unless a custom tag is specified on the command line
if [[ ${TAG_SET} -eq 0 ]]; then
#set tag null
TAG=""
fi
if [[ $# -ne 0 ]]; then
#specified a ZFS on the command line
#list all tags or the specified tag for the specified ZFS
ALL_ZFS=0
fi
fi
fi
#Is Gnu date working and not Solaris date?
${DATE} -d now > /dev/null 2>&1
status "This program requires Gnu date."
#Argument processing and error checking done.
##
## main()
##
if [[ "${ALL_ZFS}" -eq 1 ]]; then
if [[ "${FILTER_ZFS_MOUNT}" -eq 1 ]]; then
#obtain all mounted zfs filesystems
ZFS_LIST=$(zfs list -t filesystem -H -o name,mountpoint | awk '$2!="none" && $2!="-" {print $1}')
status "Failed to get ZFS list"
else
#obtain all zfs datasets
ZFS_LIST=$(zfs list -t filesystem -H -o name)
status "Failed to get ZFS list"
fi
else
#obtain specified ZFS
ZFS_LIST=$(zfs list -t filesystem -H -o name "$*")
status "Failed to get ZFS list"
fi
# for each mounted ZFS filesystem,
# see if a snapshot exists longer than the specified time
# create the new snap
# if successful, also delete any expired snap matching the TAG
for fs in ${ZFS_LIST}; do
if [[ "${DESTROY_ONLY}" -ne 1 ]]; then
#calculate expiration snapshot time
etime=$(${DATE} -d "+${EXPIRATION}" '+%s')
zfs_snap_name="${TAG}:${etime}"
if [[ "${LIST}" -eq 0 ]]; then
#create the new snap
zfs snapshot ${ZFS_ARG} "${fs}@${zfs_snap_name}"
status "ZFS snapshot failed: ${fs}@${zfs_snap_name}"
if [[ "${VERBOSE}" -eq 1 ]]; then
echo "Snapshot successful: ${fs}@${zfs_snap_name}"
echo ""
fi
fi
#if snapshot only specified, we are done
if [[ ${SNAPSHOT_ONLY} -eq 1 ]]; then
continue
fi
fi
# For destroy, we want to be specific and limit the set with regexp for
# a TAG -- unless all_zfs and destroy_only is specified on the command line.
# In this case, we really do destroy all expired snaps.
# If all_zfs is set and destroy_only is set and a tag was specified, we still limit the set to matching tag.
if [[ ${ALL_ZFS} -eq 1 && ${DESTROY_ONLY} -eq 1 && ${TAG_SET} -eq 0 ]]; then
#reset tag null
TAG=""
fi
## This is a big performance gain -- limiting the search for snapshots
## to the relevant filesystem restricted by depth recursion limit of 1.
## search regex increased to purposely not include beadm snapshots
SNAPS=$(zfs list -t snapshot -H -o name -d 1 "${fs}" | grep "${TAG}:[0-9][0-9][0-9][0-9][0-9]*$")
#SNAPS=$(zfs list -t snapshot -H -o name | grep "^${fs}@" | grep "${TAG}:[0-9][0-9][0-9][0-9][0-9]*$")
# loop through all snapshots for $fs
# and see which snapshots have expired
for snapfs in ${SNAPS}; do
#Has the snapshot expired?
mytime=$(${DATE} +%s)
#check is more than 4 significant digits in the expiration date
snap_etime=$(echo "${snapfs}" | awk -F: '/:[0-9][0-9][0-9][0-9][0-9][0-9]*$/ {print $2}')
#get creation time
cr=$(zfs get -Ho value creation "${snapfs}")
created=$(${DATE} -d "${cr}" "+%d-%b-%y %H:%M")
if [[ -z "${snap_etime}" ]]; then
#Not a valid epoch date.
#Since this is not a managed snapshot, make sure it never expires
expiry="Unmanaged"
delta=$(( S_TIME - 1 ))
else
expiry=$(${DATE} -d "@${snap_etime}" "+%d-%b-%y %H:%M:%S")
delta=$(( mytime - snap_etime ))
fi
if [[ "${VERBOSE}" -eq 1 && "${LIST}" -eq 0 ]]; then
echo "Considering $snapfs for destruction:"
echo " Delta: ${delta}"
fi
if [[ "${LIST}" -eq 1 ]]; then
if [[ "${delta}" -ge "${S_TIME}" ]]; then
#expired
if [[ ${VERBOSE} -eq 1 ]]; then
used=$(zfs list -Housed "${snapfs}")
printf "%41s %-15s %-18s%s %s\n" "${snapfs}" "${created}" "${expiry}" " *" "${used}"
else
printf "%41s %-15s %-18s%s\n" "${snapfs}" "${created}" "${expiry}" " *"
fi
else
if [[ "${LIST_EXPIRED}" -eq 0 ]]; then
# just list not expired if filter not set
#not expired
if [[ ${VERBOSE} -eq 1 ]]; then
used=$(zfs list -Housed "${snapfs}")
printf "%41s %-15s %-18s%s %s\n" "${snapfs}" "${created}" "${expiry}" " " "${used}"
else
printf "%41s %-15s %-18s\n" "${snapfs}" "${created}" "${expiry}"
fi
fi
fi
else
#List not set, just destroy
#give it a bit of wiggle room -- 60 secs
#but, destroy anyway, if forced
if [[ "${delta}" -ge "${S_TIME}" || ${FORCE} -eq 1 ]]; then
zfs destroy ${ZFS_ARG} "${snapfs}"
status "Destroy failed: zfs destroy ${ZFS_ARG} ${snapfs}"
if [[ "${VERBOSE}" -eq 1 && "${LIST}" -eq 0 ]]; then
if [[ ${FORCE} -eq 1 ]]; then
echo " Forced Destroyed!"
echo ""
else
echo " Expired -> Destroyed!"
echo ""
fi
fi
else
if [[ "${VERBOSE}" -eq 1 && "${LIST}" -eq 0 ]]; then
echo " Not Expired -> Preserved"
echo ""
fi
fi
fi
done
done
exit 0