-if [ $nodstsnaps == 0 ]; then
- zfs list -rH -t snapshot $srcfs | grep "$srcfs@" | awk '{print $1}' > /tmp/source-list
- zfs list -rH -t snapshot $dstfs | grep "$dstfs@" | sed "s,$dstpool/,," | awk '{print $1}' > /tmp/destination-list
- diff -u /tmp/source-list /tmp/destination-list | grep -v '^+++' | awk '/^\+/ {print}' | sed "s,^\+,$dstpool/," > /tmp/obsolete-snapshots
- rm -f /tmp/source-list /tmp/destination-list
-
- echo >&2 "Removing obsolete backups from the destination pool"
- for snapshot in $(cat /tmp/obsolete-snapshots); do
- echo >&2 "Removing '$snapshot' from destination."
- [ $test == 0 ] && zfs destroy "$snapshot"
- done
-
- echo >&2 "Rolling back to the most recent snapshot on the destination."
- [ $test == 0 ] && zfs rollback $(zfs list -rH -t snapshot $dstfs | grep "$dstfs@" | awk '{snap=$1} END {print snap}')
-
- echo >&2 "Calculating the most recent common snapshot between the two filesystems."
- if zfs list -H "$dstfs" > /dev/null 2>&1; then
- for snap in $(zfs list -rH -t snapshot "$dstfs" | grep "$dstfs@" |
- sed 's,.*@,,' | awk '{print$1}'); do
- if zfs list -rH -t snapshot "$fs" | grep "$fs@" | sed 's,.*@,,' | awk '{print$1}' | grep "^${snap}$" >/dev/null 2>&1; then
- common=$snap
- fi
- done
+# Obtain the zpool guid for the local backup pool
+backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
+zpool get guid $backup_pool > /dev/null 2>&1
+if [ $? != 0 ]; then
+ fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" $mailto
+fi
+
+# Turn on shell verbosity
+set -x
+
+# Create the remote lockdir before continuing with the replicate
+# Spinlock on creating the lock
+maxsleeptime=60
+maxattempts=500
+attempts=0
+while true; do
+ $ssh $remote mkdir "$lockdir" >/dev/null 2>&1
+ if [ $? != 0 ]; then
+ # Another zfs admin tool is running.
+ # Wait a random amount of time and try again
+ ransleep=$(($RANDOM % $maxsleeptime))
+ sleep $ransleep
+ ((attempts=attempts+1))
+ else
+ # No other zfs admin tool is running, we can now.
+ break
+ fi
+ if [[ $attempts -gt $maxattempts ]]; then
+ # We've exceeded our maximum while loop count
+ echo "-E- The zfs filesystem has been locked down. Skipping replicate operation."
+ fail_msg=`$ssh $remote ls -ld $lockdir 2>&1`
+ fatal_and_exit "zfs-replicate-all on $hostname unable to obtain zfs admin lock:\n$fail_msg" $mailto
+ fi
+done
+
+# Setup our backup marker names
+current_backup_marker=${remote_fs}@current-backup-${backup_pool_guid}
+previous_backup_marker=${remote_fs}@previous-backup-${backup_pool_guid}
+
+# List the snapshots on the remote machine.
+remote_list=$(mktemp /tmp/replicate.XXXXXX)
+$ssh $remote \
+ $zfs list -H -t snapshot |
+ grep ^${remote_fs}@ |
+ awk '{print$1}' > $remote_list
+if [[ $? != 0 ]] && [[ $expect_empty_remote_list == 0 ]]; then
+ echo "-W- Unable to find $remote_fs on the remote host $hostname. Unable to proceed since the"
+ echo " expect_empty_remote_list option has not been set to allow this, skipping replicate operation."
+ fatal_and_exit "-E- remote $zfs list on $hostname for $remote_fs command failed" $mailto
+fi
+
+# List the snapshots on the local machine.
+# Don't list the current backup marker if it exists on the local side.
+# If you do, it can mess up the common finding algorithm below.
+local_list=$(mktemp /tmp/replicate.XXXXXX)
+$zfs list -H -t snapshot |
+ grep ^${backup_pool}/${remote_fs}@ |
+ grep -v ^${backup_pool}/${current_backup_marker} |
+ awk "{gsub(/^$backup_pool./,\"\",\$1); print\$1}" > $local_list
+# If no local snapshots exist, we may need to send the entire filesystem, which we'll do later
+# So, no error check here as a non-zero return code means the local filesystem or snaps are missing
+# We'll catch this later on as a case where we need to send the entire filesystem
+
+# Destroy the current backup marker snapshot on the remote system if it exists
+grep -q ${current_backup_marker} $remote_list
+if [ $? == 0 ]; then
+ $ssh $remote $zfs destroy ${current_backup_marker}
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote $zfs destroy $current_backup_marker on $hostname command failed" $mailto