Added new option to expect empty remote lists when backing up to a new disk
[zfs-ubuntu/.git] / zfs-replicate
index b0241e853f9630cbfc8684e91a5af59752228df4..a5a3ad9653787db0fcd5798e5fccddc60c7082fc 100755 (executable)
@@ -18,6 +18,16 @@ remote=$1
 remote_fs=$2
 remote_pool=${2%%/*}
 
+# return non-zero exit code if any command in the pipe fails
+set -o pipefail
+
+# get the backup pool from the command line or the config file if not specified
+if [[ -n $3 ]]; then
+    backup_pool=$3
+else
+    backup_pool=${backup_pool%% *} # use the first backup pool if none specified
+fi
+
 # Setup our cleanup and exit trap
 cleanup() {
   if [[ -e "$local_list" ]]; then
@@ -33,17 +43,17 @@ cleanup() {
 }
 fatal_and_exit() {
   echo -e 2>&1 "$1"
-  # Destroy the backup markers on the local filesystem if they exist
+  # Destroy the current backup markers from the local backup_pool and remote_pool if they exist
   if [[ -n "$current_backup_marker" ]]; then
-    zfs list -t snapshot ${backup_pool}/${current_backup_marker} > /dev/null 2>&1
+    # Local backup pool current backup marker
+    $zfs list -t snapshot ${backup_pool}/${current_backup_marker} > /dev/null 2>&1
     if [ $? == 0 ]; then
       $zfs destroy ${backup_pool}/${current_backup_marker}
     fi
-  fi
-  if [[ -n "$previous_backup_marker" ]]; then
-    zfs list -t snapshot ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1 
+    # Remote pool current backup marker
+    $ssh $remote zfs list -t snapshot ${current_backup_marker} > /dev/null 2>&1
     if [ $? == 0 ]; then
-      $zfs destroy ${backup_pool}/${previous_backup_marker}
+      $ssh $remote $zfs destroy ${current_backup_marker}
     fi
   fi
   # send email notification
@@ -56,6 +66,27 @@ fatal_and_exit() {
 trap fatal_and_exit INT
 trap cleanup EXIT
 
+# Declare a function to handle the replicate operation
+replicate() {
+  zfs_send="$1"
+  zfs_recv="$zfs receive -vF -d ${backup_pool}/${remote_pool}"
+  glue="$throttle $throttle_opt"
+  if [[ $throttle_enable == 1 && -e $throttle ]]; then
+    # handle using the glue in the local and remote host case properly
+    if [[ -z "$ssh" ]]; then
+      # local host glue case
+      $zfs_send | $glue | $zfs_recv 
+    else
+      # remote host glue case
+      $ssh $remote "$zfs_send | $glue" | $zfs_recv
+    fi
+  else 
+    # no glue case - works for both the local and remote host cases
+    $ssh $remote $zfs_send | $zfs_recv
+  fi
+  # The return code of the zfs_send | zfs_recv operation will be returned to the caller
+}
+
 # Make sure we have valid arguments
 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
   fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
@@ -139,7 +170,7 @@ $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 [ $? != 0 ]; then
+if [[ $? != 0 ]] && [[ $expect_empty_remote_list == 0 ]]; then
   fatal_and_exit "-E- local $zfs list on $hostname command failed" $mailto
 fi
 
@@ -175,18 +206,16 @@ if [ $no_markers == 0 ]; then
   # 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} 
+  $zfs rollback -rf ${backup_pool}/${previous_backup_marker} 
   if [ $? != 0 ]; then
-    fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
+    sleep 120
+    $zfs rollback -rf ${backup_pool}/${previous_backup_marker}
+    if [ $? != 0 ]; then
+      fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
+    fi
   fi
   # Now it should be safe to send the snaps
-  if [[ $throttle_enable == 1 && -e $throttle ]]; then
-    $ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} | 
-        $throttle $throttle_opt | $zfs receive -vF -d ${backup_pool}/${remote_pool}
-  else 
-    $ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} |
-        $zfs receive -vF -d ${backup_pool}/${remote_pool}
-  fi
+  replicate "$zfs send -Rc -I${previous_backup_marker} ${current_backup_marker}"
   if [ $? != 0 ]; then
     fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
   fi
@@ -205,13 +234,7 @@ else
 
   if [[ -n "$common" ]]; then
     # We found a common snapshot, incrementally send the new snaps
-    if [[ $throttle_enable == 1 && -e $throttle ]]; then
-      $ssh $remote $zfs send -R -I${common/*@/@} ${current_backup_marker} |
-          $throttle $throttle_opt | $zfs receive -vF -d ${backup_pool}/${remote_pool}
-    else
-      $ssh $remote $zfs send -R -I${common/*@/@} ${current_backup_marker} |
-          $zfs receive -vF -d ${backup_pool}/${remote_pool}
-    fi
+    replicate "$zfs send -Rc -I${common/*@/@} ${current_backup_marker}"
     if [ $? != 0 ]; then
       fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
     fi
@@ -233,13 +256,7 @@ else
       fi
     fi
     # Send the full filesystem
-    if [[ $throttle_enable == 1 && -e $throttle ]]; then
-      $ssh $remote $zfs send -R ${current_backup_marker} |
-          $throttle $throttle_opt | $zfs receive -vF -d ${backup_pool}/${remote_pool}
-    else
-      $ssh $remote $zfs send -R ${current_backup_marker} |
-          $zfs receive -vF -d ${backup_pool}/${remote_pool}
-    fi
+    replicate "$zfs send -Rc ${current_backup_marker}"
     if [ $? != 0 ]; then
       fatal_and_exit "-E- remote full $zfs send command failed on $hostname" $mailto
     fi
@@ -250,6 +267,7 @@ fi
 # don't check the return codes here because these may not exist, and that is ok
 $zfs destroy ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1
 $ssh $remote $zfs destroy ${previous_backup_marker} > /dev/null 2>&1
+sleep 1
 
 # Rename the current backup marker to be the previous backup marker
 $zfs rename ${backup_pool}/${current_backup_marker} ${backup_pool}/${previous_backup_marker}