changeset 428:824e88618376

The first version of "fzfs copy-tree" completed
author Franz Glasner <hg@dom66.de>
date Thu, 21 Sep 2023 18:42:41 +0200
parents 7056e5b7712d
children bbdb1ab0ea00
files sbin/fzfs
diffstat 1 files changed, 34 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/sbin/fzfs	Thu Sep 21 12:03:09 2023 +0200
+++ b/sbin/fzfs	Thu Sep 21 18:42:41 2023 +0200
@@ -260,7 +260,7 @@
 
     local _ds_source_base _ds_source_snapshot _snapshot_suffix
     local _ds_tree _ds _ds_relname _ds_canmount
-    local _rflags
+    local _arg_canmount _arg_mp1 _arg_mp2
 
     _opt_mountpoint=""
     _opt_mount_noauto=""
@@ -299,6 +299,10 @@
         echo "ERROR: source dataset does not exist: ${_ds_source}" 1>&2;
         return 1;
     fi
+    if zfs get -H name "${_ds_target}" >/dev/null 2>&1; then
+        echo "ERROR: target dataset already exists: ${_ds_target}" 1>&2;
+        return 1;
+    fi
 
     _ds_source_base="${_ds_source%@*}"
     _ds_source_snapshot="${_ds_source##*@}"
@@ -311,9 +315,6 @@
         _snapshot_suffix=""
     fi
 
-    echo $_ds_source_base
-    echo $_ds_source_snapshot $_snapshot_suffix
-
     _ds_tree=""
 
     while IFS=$'\n' read -r _ds; do
@@ -322,7 +323,6 @@
         else
             _ds_tree="${_ds_tree}"$'\n'"${_ds}"
         fi
-        echo "X: $_ds"
     done <<EOF20ee7ea0781414fab8c305d3875d15e
 $(zfs list -H -r -t filesystem -o name -s name "${_ds_source_base}")
 EOF20ee7ea0781414fab8c305d3875d15e
@@ -334,19 +334,20 @@
             return 1
         fi
     done
+    IFS=$'\n'
     for _ds in ${_ds_tree}; do
+        # Reset IFS
+        IFS=$' \t\n'
+
         # Determine the relative name of the dataset
         _ds_relname="${_ds#${_ds_source_base}}"
 
         _ds_canmount=$(zfs get -H -o value canmount "${_ds}")
-        echo "REL: $_ds  --- $_ds_relname  --- $_ds_canmount"
-
-        _rflags=""
 
         if [ "${_ds_canmount}" = "off" ]; then
-            _rflags="-o canmount=off"
+            _arg_canmount="-o canmount=off"
         else
-            _rflags="${_opt_mount_noauto}"
+            _arg_canmount="${_opt_mount_noauto}"
         fi
 
         if [ -z "${_ds_relname}" ]; then
@@ -354,15 +355,34 @@
             # Source root to target root
             #
             if [ -z "${_opt_mountpoint}" ]; then
-                _rflags="${_rflags} -x mountpoint"
+                _arg_mp1=-x
+                _arg_mp2=mountpoint
             else
-                _rflags="${_rflags} -o mountpoint='${_opt_mountpoint}'"
+                _arg_mp1=-o
+                _arg_mp2="mountpoint=${_opt_mountpoint}"
             fi
         else
-            _rflags="${_rflags} -x mountpoint"
+            _arg_mp1=-x
+            _arg_mp2=mountpoint
+        fi
+        if [ -z "${_opt_dry_run}" ]; then
+            zfs send -Lec -p -v "${_ds}${_snapshot_suffix}" | zfs receive -v ${_opt_nomount} ${_arg_canmount} ${_arg_mp1} "${_arg_mp2}" "${_ds_target}${_ds_relname}"
+        else
+            echo "Would execute: zfs send -Lec -p -v '${_ds}${_snapshot_suffix}' | zfs receive -v ${_opt_nomount} ${_arg_canmount} ${_arg_mp1} '${_arg_mp2}' '${_ds_target}${_ds_relname}'"
         fi
-        echo "zfs send -Lec -p -v '${_ds}${_ds_source_snapshot}' | zfs receive -v ${_opt_nomount} ${_rflags} '${_ds_target}${_ds_relname}'"
+        # for the loop
+        IFS=$'\n'
     done
+    # Reset to default
+    IFS=$' \t\n'
+    # Remove received snapshots
+    if [ -n "${_ds_source_snapshot}" ]; then
+        if [ -z "${_opt_dry_run}" ]; then
+            zfs destroy -rv "${_ds_target}${_snapshot_suffix}"
+        else
+            echo "Would execute: zfs destroy -rv '${_ds_target}${_snapshot_suffix}'"
+        fi
+    fi
 }