Skip to content

Commit

Permalink
Implement --send-fallback for the snapshot replication.
Browse files Browse the repository at this point in the history
This allows a --send-incr to fallback to --send-full if it can't find
an earlier snapshot.
  • Loading branch information
FransUrbo committed Apr 3, 2015
1 parent 4eaa6ee commit 26fd9dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/zfs-auto-snapshot.8
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ Send zfs full backup to remote hostname (or IP address) and put it in remote po
\fB\-\-send\-incr\fR=[\fIremote host\fR]:[\fIremote pool\fR]
Send zfs incremental backup to remote hostname (or IP address) and put it in remote pool.
.TP
\fB\-\-send\-fallback\fR
Allow to fallback from incremental to full snapshot replication.
.sp
This implies \fI-R\fR to \fBzfs send\fR and \fI-F\fR to \fBzfs receive\fR.
.TP
\fB\-\-send\-opts\fR=\fIOPTS\fR
Option(s) passed to 'zfs send'.
.TP
Expand Down
36 changes: 30 additions & 6 deletions src/zfs-auto-snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ opt_send_opts=''
opt_recv_opts=''
opt_send_ssh_opts=''
opt_send_mbuf_opts=''
opt_send_fallback=''
opt_sep='_'
opt_setauto=''
opt_syslog=''
Expand Down Expand Up @@ -77,6 +78,7 @@ print_usage ()
-q, --quiet Suppress warnings and notices at the console.
--send-full=F Send zfs full backup. Unimplemented.
--send-incr=F Send zfs incremental backup. Unimplemented.
--send-fallback Fallback from incremental to full if needed.
--send-opts=F Option(s) passed to 'zfs send'.
--recv-opts=F Option(s) passed to 'zfs receive'.
--send-ssh-opts Option(s) passed to 'ssh'.
Expand Down Expand Up @@ -231,8 +233,8 @@ do_send () # snapname, oldglob
local jj

[ -n "$opt_send_mbuf_opts" ] && remote="mbuffer $opt_send_mbuf_opts |"
remote="$remote ssh $opt_send_ssh_opts $opt_send_host "
remote="$remote zfs receive $opt_recv_opts $opt_recv_pool"
remote="$remote ssh $opt_send_ssh_opts $opt_send_host"
remote="$remote zfs receive $opt_recv_opts"

# STEP 1: Go throug all snapshots we've created
for ii in $SNAPS_DONE
Expand Down Expand Up @@ -263,22 +265,33 @@ do_send () # snapname, oldglob
# 1: We change from incremental to full.
# 2: We accept that the user have said INCR, and stick with
# it.
if [ "$opt_send_type" = "incr" -a -z "$last_snap" ]; then
# Normally we do point 2, but if --send-fallback is specified,
# we allow it and convert to a full send instead.
if [ "$opt_send_type" = "incr" -a -z "$last_snap" -a -z "$opt_send_fallback" ]; then
if [ -n "$opt_verbose" ]; then
echo "WARNING: No previous snapshots exist but we where called"
echo " with --send-incr. Can not continue."
echo " Please rerun with --send-full."
echo " Or use --send-fallback."
fi
return 1
fi

if [ -n "$opt_recursive" -a -n "$last_snap" ]; then
if [ -n "$opt_recursive" ]; then
# STEP 3: Again, go through ALL snapshots that exists, but this
# time only look for the snapshots that 'starts with'
# the dataset/volume in question AND 'ends with'
# the exact snapshot name/date in step 2.
for jj in $SNAPSHOTS_OLD
do
# When trying to find snapshots recurively, we MUST have a 'last_snap'
# value. Othervise, it will match ALL snapshots for dset (if we had
# used '"^$dset.*@$GLOB" only).
if [ -z "$last_snap" ] && echo "$jj" | grep -qE "^$dset.*@$GLOB"; then
# Use this as last snapshot name
last_snap="${jj#*@}"
fi

if echo "$jj" | grep -qE "^$dset.*@$last_snap"; then
SNAPS_SEND="$SNAPS_SEND
$jj"
Expand All @@ -297,9 +310,15 @@ $jj"

if [ $RUNSEND -eq 1 ]; then
if [ "$opt_send_type" = "incr" ]; then
do_run "zfs send $opt_send_opts -i $jj $ii | $remote" || RUNSEND=0
if [ "$jj" = "$ii" -a -n "$opt_send_fallback" ]; then
do_run "zfs send $opt_send_opts -R $ii | $remote -F $opt_recv_pool" \
|| RUNSEND=0
else
do_run "zfs send $opt_send_opts -i $jj $ii | $remote $opt_recv_pool" \
|| RUNSEND=0
fi
else
do_run "zfs send $opt_send_opts -R $jj | $remote" || RUNSEND=0
do_run "zfs send $opt_send_opts -R $jj | $remote $opt_recv_pool" || RUNSEND=0
fi
fi

Expand All @@ -321,6 +340,7 @@ GETOPT=$(getopt \
--longoptions=pre-snapshot:,post-snapshot:,destroy-only \
--longoptions=send-full:,send-incr:,send-opts:,recv-opts: \
--longoptions=send-ssh-opts:,send-mbuf-opts:,pre-send:,post-send: \
--longoptions=send-fallback \
--options=dnshe:l:k:p:rs:qgv \
-- "$@" ) \
|| exit 128
Expand Down Expand Up @@ -421,6 +441,10 @@ do
opt_recv_pool=$(echo "$2" | sed 's,.*:,,')
shift 2
;;
(--send-fallback)
opt_send_fallback=1
shift 1
;;
(--send-opts)
opt_send_opts="$2"
shift 2
Expand Down

0 comments on commit 26fd9dd

Please sign in to comment.