Skip to content

Commit 0be4466

Browse files
committed
Merge pull request #29 from mmalecki/pre-post
Add pre and post snapshot hooks
2 parents 9d2398e + 10f9b3e commit 0be4466

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/zfs-auto-snapshot.8

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ 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.
75+
.TP
6776
name
6877
Filesystem and volume names, or '//' for all ZFS datasets.
6978
.SH SEE ALSO

src/zfs-auto-snapshot.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ opt_setauto=''
3939
opt_syslog=''
4040
opt_skip_scrub=''
4141
opt_verbose=''
42+
opt_pre_snapshot=''
43+
opt_post_snapshot=''
4244

4345
# Global summary statistics.
4446
DESTRUCTION_COUNT='0'
@@ -156,8 +158,10 @@ do_snapshots () # properties, flags, snapname, oldglob, [targets...]
156158

157159
for ii in $TARGETS
158160
do
159-
if do_run "zfs snapshot $PROPS $FLAGS '$ii@$NAME'"
161+
do_run "$opt_pre_snapshot $ii $NAME"
162+
if [ $? -eq 0 ] && do_run "zfs snapshot $PROPS $FLAGS '$ii@$NAME'"
160163
then
164+
do_run "$opt_post_snapshot $ii $NAME"
161165
SNAPSHOT_COUNT=$(( $SNAPSHOT_COUNT + 1 ))
162166
else
163167
WARNING_COUNT=$(( $WARNING_COUNT + 1 ))
@@ -198,6 +202,7 @@ GETOPT=$(getopt \
198202
--longoptions=default-exclude,dry-run,fast,skip-scrub,recursive \
199203
--longoptions=event:,keep:,label:,prefix:,sep: \
200204
--longoptions=debug,help,quiet,syslog,verbose \
205+
--longoptions=pre-snapshot:,post-snapshot: \
201206
--options=dnshe:l:k:p:rs:qgv \
202207
-- "$@" ) \
203208
|| exit 128
@@ -308,6 +313,14 @@ do
308313
opt_verbose='1'
309314
shift 1
310315
;;
316+
(--pre-snapshot)
317+
opt_pre_snapshot="$2"
318+
shift 2
319+
;;
320+
(--post-snapshot)
321+
opt_post_snapshot="$2"
322+
shift 2
323+
;;
311324
(--)
312325
shift 1
313326
break

0 commit comments

Comments
 (0)