+# Make sure we have valid arguments
+if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
+ echo "Usage: $0 <hostname> <zfs filesystem>"
+ exit 1
+fi
+
+# Make sure the local pool and local receiving filesystem exist, or print some errors
+zpool list -H "$local_pool" >/dev/null 2>&1
+if [ $? != 0 ]; then
+ echo >&2 "-E- The local pool, '$local_pool' doesn't seem to exist."
+ exit 1
+fi
+zfs list "$local_pool/$remote_pool" >/dev/null 2>&1
+if [ $? != 0 ]; then
+ echo >&2 "-I- The local filesystem for the remote pool, '$local_pool/$remote_pool' doesn't seem to exist."
+ echo >&2 " Creating the local filesystem to receive the remote pool into: $local_pool/$remote_pool"
+ $zfs create $local_pool/$remote_pool
+ if [ $? != 0 ]; then
+ echo "-E- remote $zfs create command failed"
+ exit 1
+ fi
+fi
+
+# Obtain the zpool guid for the local pool
+local_pool_guid=`zpool get guid $local_pool 2>&1 | grep $local_pool | awk '{ print $3 }'`
+zpool get guid $local_pool > /dev/null 2>&1
+if [ $? != 0 ]; then
+ echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
+ exit 1
+fi
+
+# Turn on shell verbosity
+set -x
+
+# Create the remote lockdir before continuing with the replicate
+# Spinlock on creating the lock
+maxsleeptime=60
+maxattempts=100
+attempts=0
+while true; do
+ ssh $remote mkdir "$remote_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 "-W- The zfs filesystem has been locked down. Skipping replicate operation."
+ ssh $remote ls -ld $remote_lockdir | /usr/bin/mailx -s "zfs-replicate-all unable to obtain zfs admin lock" $mailto
+ exit 1
+ fi
+done
+
+# Declare a cleanup() method to remove the remote lockdir
+cleanup() { ssh $remote rm -rf "$remote_lockdir"; }
+trap cleanup EXIT
+
+# Setup our backup marker names
+current_backup_marker=${remote_fs}@current-backup-${local_pool_guid}
+previous_backup_marker=${remote_fs}@previous-backup-${local_pool_guid}
+