+# Make sure we have valid arguments
+if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
+ fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
+fi
+
+# check for localhost
+if [[ $remote = "localhost" ]]; then
+ remote=""
+ ssh=""
+fi
+
+# Make sure the local backup pool and local receiving filesystem exist, or print some errors
+zpool list -H "$backup_pool" >/dev/null 2>&1
+if [ $? != 0 ]; then
+ fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." $mailto
+fi
+zfs list "$backup_pool/$remote_pool" >/dev/null 2>&1
+if [ $? != 0 ]; then
+ echo >&2 "-I- The local filesystem for the remote pool, '$backup_pool/$remote_pool' doesn't seem to exist."
+ echo >&2 " Creating the local filesystem to receive the remote pool into: $backup_pool/$remote_pool"
+ $zfs create $backup_pool/$remote_pool
+ if [ $? != 0 ]; then
+ fatal_and_exit "-E- remote $zfs on $hostname create command failed" $mailto
+ fi
+fi
+
+# 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}