@@ -41,6 +41,7 @@ opt_skip_scrub=''
41
41
opt_verbose=' '
42
42
opt_pre_snapshot=' '
43
43
opt_post_snapshot=' '
44
+ opt_do_snapshots=1
44
45
45
46
# Global summary statistics.
46
47
DESTRUCTION_COUNT=' 0'
@@ -71,6 +72,7 @@ print_usage ()
71
72
-g, --syslog Write messages into the system log.
72
73
-r, --recursive Snapshot named filesystem and all descendants.
73
74
-v, --verbose Print info messages.
75
+ --destroy-only Only destroy older snapshots, do not create new ones.
74
76
name Filesystem and volume names, or '//' for all ZFS datasets.
75
77
"
76
78
}
@@ -150,6 +152,7 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
150
152
local GLOB=" $4 "
151
153
local TARGETS=" $5 "
152
154
local KEEP=' '
155
+ local RUNSNAP=1
153
156
154
157
# global DESTRUCTION_COUNT
155
158
# global SNAPSHOT_COUNT
@@ -158,15 +161,21 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
158
161
159
162
for ii in $TARGETS
160
163
do
161
- do_run " $opt_pre_snapshot $ii $NAME "
162
- if [ $? -eq 0 ] && do_run " zfs snapshot $PROPS $FLAGS '$ii @$NAME '"
164
+ if [ -n " $opt_do_snapshots " ]
163
165
then
164
- do_run " $opt_post_snapshot $ii $NAME "
165
- SNAPSHOT_COUNT=$(( $SNAPSHOT_COUNT + 1 ))
166
- else
167
- WARNING_COUNT=$(( $WARNING_COUNT + 1 ))
168
- continue
169
- fi
166
+ if [ " $opt_pre_snapshot " != " " ]
167
+ then
168
+ do_run " $opt_pre_snapshot $ii $NAME " || RUNSNAP=0
169
+ fi
170
+ if [ $RUNSNAP -eq 1 ] && do_run " zfs snapshot $PROPS $FLAGS '$ii @$NAME '"
171
+ then
172
+ [ " $opt_post_snapshot " != " " ] && do_run " $opt_post_snapshot $ii $NAME "
173
+ SNAPSHOT_COUNT=$(( $SNAPSHOT_COUNT + 1 ))
174
+ else
175
+ WARNING_COUNT=$(( $WARNING_COUNT + 1 ))
176
+ continue
177
+ fi
178
+ fi
170
179
171
180
# Retain at most $opt_keep number of old snapshots of this filesystem,
172
181
# including the one that was just recently created.
@@ -202,7 +211,7 @@ GETOPT=$(getopt \
202
211
--longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \
203
212
--longoptions=event:,keep:,label:,prefix:,sep: \
204
213
--longoptions=debug,help,quiet,syslog,verbose \
205
- --longoptions=pre-snapshot:,post-snapshot: \
214
+ --longoptions=pre-snapshot:,post-snapshot:,destroy-only \
206
215
--options=dnshe:l:k:p:rs:qgv \
207
216
-- " $@ " ) \
208
217
|| exit 128
321
330
opt_post_snapshot=" $2 "
322
331
shift 2
323
332
;;
333
+ (--destroy-only)
334
+ opt_do_snapshots=' '
335
+ shift 1
336
+ ;;
324
337
(--)
325
338
shift 1
326
339
break
@@ -525,11 +538,28 @@ SNAPNAME="$opt_prefix${opt_label:+$opt_sep$opt_label}-$DATE"
525
538
# The expression for matching old snapshots. -YYYY-MM-DD-HHMM
526
539
SNAPGLOB=" $opt_prefix ${opt_label: +?$opt_label } ????????????????"
527
540
528
- test -n " $TARGETS_REGULAR " \
529
- && print_log info " Doing regular snapshots of $TARGETS_REGULAR "
541
+ if [ -n " $opt_do_snapshots " ]
542
+ then
543
+ test -n " $TARGETS_REGULAR " \
544
+ && print_log info " Doing regular snapshots of $TARGETS_REGULAR "
530
545
531
- test -n " $TARGETS_RECURSIVE " \
532
- && print_log info " Doing recursive snapshots of $TARGETS_RECURSIVE "
546
+ test -n " $TARGETS_RECURSIVE " \
547
+ && print_log info " Doing recursive snapshots of $TARGETS_RECURSIVE "
548
+
549
+ if test -n " $opt_keep " && [ " $opt_keep " -ge " 1" ]
550
+ then
551
+ print_log info " Destroying all but the newest $opt_keep snapshots of each dataset."
552
+ fi
553
+ elif test -n " $opt_keep " && [ " $opt_keep " -ge " 1" ]
554
+ then
555
+ test -n " $TARGETS_REGULAR " \
556
+ && print_log info " Destroying all but the newest $opt_keep snapshots of $TARGETS_REGULAR "
557
+
558
+ test -n " $TARGETS_RECURSIVE " \
559
+ && print_log info " Recursively destroying all but the newest $opt_keep snapshots of $TARGETS_RECURSIVE "
560
+ else
561
+ print_log notice " Only destroying snapshots, but count of snapshots to preserve not given. Nothing to do."
562
+ fi
533
563
534
564
test -n " $opt_dry_run " \
535
565
&& print_log info " Doing a dry run. Not running these commands..."
0 commit comments