@@ -41,6 +41,7 @@ opt_skip_scrub=''
4141opt_verbose=' '
4242opt_pre_snapshot=' '
4343opt_post_snapshot=' '
44+ opt_do_snapshots=1
4445
4546# Global summary statistics.
4647DESTRUCTION_COUNT=' 0'
@@ -71,6 +72,7 @@ print_usage ()
7172 -g, --syslog Write messages into the system log.
7273 -r, --recursive Snapshot named filesystem and all descendants.
7374 -v, --verbose Print info messages.
75+ --destroy-only Only destroy older snapshots, do not create new ones.
7476 name Filesystem and volume names, or '//' for all ZFS datasets.
7577"
7678}
@@ -150,6 +152,7 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
150152 local GLOB=" $4 "
151153 local TARGETS=" $5 "
152154 local KEEP=' '
155+ local RUNSNAP=1
153156
154157 # global DESTRUCTION_COUNT
155158 # global SNAPSHOT_COUNT
@@ -158,15 +161,21 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
158161
159162 for ii in $TARGETS
160163 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 " ]
163165 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
170179
171180 # Retain at most $opt_keep number of old snapshots of this filesystem,
172181 # including the one that was just recently created.
@@ -202,7 +211,7 @@ GETOPT=$(getopt \
202211 --longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \
203212 --longoptions=event:,keep:,label:,prefix:,sep: \
204213 --longoptions=debug,help,quiet,syslog,verbose \
205- --longoptions=pre-snapshot:,post-snapshot: \
214+ --longoptions=pre-snapshot:,post-snapshot:,destroy-only \
206215 --options=dnshe:l:k:p:rs:qgv \
207216 -- " $@ " ) \
208217 || exit 128
321330 opt_post_snapshot=" $2 "
322331 shift 2
323332 ;;
333+ (--destroy-only)
334+ opt_do_snapshots=' '
335+ shift 1
336+ ;;
324337 (--)
325338 shift 1
326339 break
@@ -525,11 +538,28 @@ SNAPNAME="$opt_prefix${opt_label:+$opt_sep$opt_label}-$DATE"
525538# The expression for matching old snapshots. -YYYY-MM-DD-HHMM
526539SNAPGLOB=" $opt_prefix ${opt_label: +?$opt_label } ????????????????"
527540
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 "
530545
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
533563
534564test -n " $opt_dry_run " \
535565 && print_log info " Doing a dry run. Not running these commands..."
0 commit comments