From 0ea35528229aae4eb3f6d68e8972729d67fb16ed Mon Sep 17 00:00:00 2001 From: Turbo Fredriksson Date: Fri, 3 Apr 2015 00:13:53 +0200 Subject: [PATCH] Implement {pre,post}-send option for the snapshot replication. --- src/zfs-auto-snapshot.8 | 10 ++++++++++ src/zfs-auto-snapshot.sh | 31 +++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/zfs-auto-snapshot.8 b/src/zfs-auto-snapshot.8 index 1ca11b0..2c904ae 100644 --- a/src/zfs-auto-snapshot.8 +++ b/src/zfs-auto-snapshot.8 @@ -80,6 +80,16 @@ aborted. Command to run after each dataset is snapshotted. It is passed the dataset and snapshot name. .TP +\fB\-\-pre-send\fR=\fICOMMAND\fR +Command to run before each snapshot is sent to +remote host. It is passed the snapshot name. If +it returns non-zero, sending this dataset is +aborted. +.TP +\fB\-\-post-send\fR=\fICOMMAND\fR +Command to run after each snapshot have been sent. +It is passed the snapshot name. +.TP \fB\-\-destroy-only\fR Do not create new snapshots, but do destroy older snapshots. Has no effect unless used with \fB\-k\fR. diff --git a/src/zfs-auto-snapshot.sh b/src/zfs-auto-snapshot.sh index 91f0c1b..55b9dfd 100755 --- a/src/zfs-auto-snapshot.sh +++ b/src/zfs-auto-snapshot.sh @@ -46,6 +46,8 @@ opt_skip_scrub='' opt_verbose='' opt_pre_snapshot='' opt_post_snapshot='' +opt_pre_send='' +opt_post_send='' opt_do_snapshots=1 # Global summary statistics. @@ -220,6 +222,7 @@ do_send () # snapname, oldglob local GLOB="$2" local ii local remote="ssh $opt_send_host zfs receive $opt_recv_opts $opt_recv_pool" + local RUNSEND=1 # STEP 1: Go throug all snapshots we've created for ii in $SNAPS_DONE @@ -278,11 +281,22 @@ $jj" # STEP 4: Go through all snapshots that is to be transfered and send them. for jj in $SNAPS_SEND do - if [ "$opt_send_type" = "incr" ]; then - echo "zfs send $opt_send_opts -i $jj $ii | $remote" - else - echo "zfs send $opt_send_opts -R $jj | $remote" + if [ -n "$opt_pre_send" ]; then + do_run "$opt_pre_send $jj" || RUNSEND=0 + fi + + if [ $RUNSEND -eq 1 ]; then + if [ "$opt_send_type" = "incr" ]; then + do_run "zfs send $opt_send_opts -i $jj $ii | $remote" || RUNSEND=0 + else + do_run "zfs send $opt_send_opts -R $jj | $remote" || RUNSEND=0 + fi + fi + + if [ $RUNSEND = 1 -a -n "$opt_post_send" ]; then + do_run "$opt_post_send $jj" || RUNSEND=0 fi + done done } @@ -296,6 +310,7 @@ GETOPT=$(getopt \ --longoptions=debug,help,quiet,syslog,verbose \ --longoptions=pre-snapshot:,post-snapshot:,destroy-only \ --longoptions=send-full:,send-incr:,send-opts:,recv-opts: \ + --longoptions=pre-send:,post-send: \ --options=dnshe:l:k:p:rs:qgv \ -- "$@" ) \ || exit 128 @@ -438,6 +453,14 @@ do opt_post_snapshot="$2" shift 2 ;; + (--pre-send) + opt_pre_send="$2" + shift 2 + ;; + (--post-send) + opt_post_send="$2" + shift 2 + ;; (--destroy-only) opt_do_snapshots='' shift 1