Skip to content

Commit a0df1eb

Browse files
committed
Merge pull request #30 from mbaynton/remove-only
--destroy-only switch, --{pre,post} fixes
2 parents 0be4466 + bfe4c91 commit a0df1eb

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

src/zfs-auto-snapshot.8

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ Snapshot named filesystem and all descendants.
6464
\fB\-v\fR, \fB\-\-verbose\fR
6565
Print info messages.
6666
.TP
67-
\fB\-\-pre-snapshot\fR
68-
Command to run before snapshotting. It is passed the
69-
filesystem and snapshot name. If it returns non-zero,
70-
snapshotting this filesystem is aborted.
71-
.TP
72-
\fB\-\-post-snapshot\fR
73-
Command to run after snapshotting. It is passed the
74-
filesystem and snapshot name.
67+
\fB\-\-pre-snapshot\fR=\fICOMMAND\fR
68+
Command to run before each dataset is snapshotted.
69+
It is passed the dataset and snapshot name. If it
70+
returns non-zero, snapshotting this dataset is
71+
aborted.
72+
.TP
73+
\fB\-\-post-snapshot\fR=\fICOMMAND\fR
74+
Command to run after each dataset is snapshotted.
75+
It is passed the dataset and snapshot name.
76+
.TP
77+
\fB\-\-destroy-only\fR
78+
Do not create new snapshots, but do destroy older
79+
snapshots. Has no effect unless used with \fB\-k\fR.
80+
.IP
81+
A non-obvious use may be constructon of cron jobs or
82+
scripts that run pre-snapshot command(s), then run
83+
zfs-auto-snapshot (without \fB\-k\fR) to quickly
84+
snapshot all datasets, then run post-snapshot
85+
command(s) and clean up with zfs-auto-snapshot
86+
\fB\-\-destroy-only\fR.
7587
.TP
7688
name
7789
Filesystem and volume names, or '//' for all ZFS datasets.

src/zfs-auto-snapshot.sh

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ opt_skip_scrub=''
4141
opt_verbose=''
4242
opt_pre_snapshot=''
4343
opt_post_snapshot=''
44+
opt_do_snapshots=1
4445

4546
# Global summary statistics.
4647
DESTRUCTION_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
@@ -321,6 +330,10 @@ do
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
526539
SNAPGLOB="$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

534564
test -n "$opt_dry_run" \
535565
&& print_log info "Doing a dry run. Not running these commands..."

0 commit comments

Comments
 (0)