- if $foundcommon; then
- if [ -z "$base" ]; then
- echo >&2 "Sending '$1/$snap'"
- zfs send "$1@$snap" > $tmpdir/zfs.part
- mv $tmpdir/zfs.part $tmpdir/zfs.data
- zfs recv "$destinationpool/$fsname" < $tmpdir/zfs.data
- rm -f $tmpdir/zfs.data
- zfs set readonly=on "$destinationpool"
- zfs set atime=off "$destinationpool"
- zfs set sharenfs=off "$destinationpool"
- zfs set mountpoint=legacy "$destinationpool"
- zfs unmount "$destinationpool/$fsname"
- zfs rollback "$destinationpool/$fsname@$snap"
- else
- echo >&2 "Sending '$1@$base' -> '$1/$snap'"
- zfs send -i "$1@$base" "$1@$snap" > $tmpdir/zfs.part
- mv $tmpdir/zfs.part $tmpdir/zfs.data
- zfs recv "$destinationpool/$fsname" < $tmpdir/zfs.data
- rm -f $tmpdir/zfs.data
- # zfs unmount "$destinationpool/$fsname"
- # zfs rollback "$destinationpool/$fsname@$snap"
+# Create the current backup marker snapshot on the remote system
+$ssh $remote $zfs snapshot ${current_backup_marker}
+if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote $zfs snapshot on $hostname command failed" $mailto
+fi
+
+# Check to see if the previous backup marker exists in the remote snapshot list.
+# Check to see if the previous backup marker exists in the local snapshot list.
+# If the previous backup markers exists, perform an incremental replicate. Else:
+# 1) check to see if a common snapshot exists, and perform an incremental replicate.
+# 2) if no common snapshot exists, destroy the local filesystem, and perform a full replicate.
+grep -q ${previous_backup_marker} $remote_list
+no_markers=$?
+grep -q ${previous_backup_marker} $local_list
+no_markers=$(($no_markers || $?))
+
+if [ $no_markers == 0 ]; then
+ # We found backup markers, incrementally send the new snaps
+
+ # First, rollback the local backup pool to the previous backup marker in case the previous
+ # backup was interrupted for some reason. If we don't do this, the zfs send -R command
+ # below may complain about snaps already existing as it tries to resend from the
+ # previous backup marker again from a previously interrupted replicate.
+ $zfs rollback -r ${backup_pool}/${previous_backup_marker}
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
+ fi
+ # Now it should be safe to send the snaps
+ replicate "$zfs send -R -I${previous_backup_marker} ${current_backup_marker}"
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
+ fi
+else
+ # We didn't find any backup markers, next check to see if we have a common snapshot.
+
+ # See what the most recent snapshot on the remote end is.
+ latest=$(tail -n 1 $remote_list)
+
+ # I did this to make sure that diff would always display the most recent common
+ # Since we're keying off the context of the diff, we need to ensure we will get context
+ # by injecting a known difference in case no others exist in the lists.
+ echo bogus.remote >> $remote_list
+ echo bogus.local >> $local_list
+ common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
+
+ if [[ -n "$common" ]]; then
+ # We found a common snapshot, incrementally send the new snaps
+ replicate "$zfs send -R -I${common/*@/@} ${current_backup_marker}"
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
+ fi
+ else
+ # We did not find any markers or a common snapshot
+ # At this point, we'll have to send the entire filesystem
+ # Destroy the local filesystem if it exists before receving the full replicate
+ zfs list ${backup_pool}/${remote_fs} > /dev/null 2>&1
+ if [ $? == 0 ]; then
+ if [[ $destroy_local_filesystem_on_full_replicate == 1 ]]; then
+ $zfs destroy -r ${backup_pool}/${remote_fs}
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote full $zfs destroy command failed on $hostname" $mailto
+ fi
+ else
+ echo "-W- We need to destroy a local filesystem before receiving a full stream."
+ echo " However, since the option is set to prevent this, skipping replicate operation."
+ fatal_and_exit "unable to destroy local filesystem:\n$zfs destroy -r ${backup_pool}/${remote_fs} not able to run on $hostname" $mailto
+ fi
+ fi
+ # Send the full filesystem
+ replicate "$zfs send -R ${current_backup_marker}"
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote full $zfs send command failed on $hostname" $mailto