changeset 429:bbdb1ab0ea00

Implement "-k" option for "fzfs copy-tree" to keep received snapshots
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 22 Sep 2023 09:22:25 +0200
parents 824e88618376
children a05c5d520f86
files docs/man/man8/fzfs-copy-tree.rst sbin/fzfs
diffstat 2 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/docs/man/man8/fzfs-copy-tree.rst	Thu Sep 21 18:42:41 2023 +0200
+++ b/docs/man/man8/fzfs-copy-tree.rst	Fri Sep 22 09:22:25 2023 +0200
@@ -9,7 +9,7 @@
 Synopsis
 --------
 
-**fzfs copy-tree** [**-A**] [**-M** `mountpoint`] [**-n**] [**-u**] `source-dataset` `dest-dataset`
+**fzfs copy-tree** [**-A**] [**-M** `mountpoint`] [**-k**] [**-n**] [**-u**] `source-dataset` `dest-dataset`
 
 
 Description
@@ -43,6 +43,12 @@
    Set the `mountpoint` property for the root `dest-dataset` to `mountpoint`.
    All children will be set to inherit it.
 
+.. option:: -k
+
+   When copying from a snapshot source a corresponding snapshot will be
+   copied to the target dataset. By default this snapshot will be
+   deleted. With this option this snapshot is kept.
+
 .. option:: -n
 
    Dry-run. Do not really create and copy datasets but show what would
--- a/sbin/fzfs	Thu Sep 21 18:42:41 2023 +0200
+++ b/sbin/fzfs	Fri Sep 22 09:22:25 2023 +0200
@@ -256,7 +256,7 @@
 #:
 command_copy_tree() {
     local _ds_source _ds_target
-    local _opt_mountpoint _opt_mount_noauto _opt_nomount _opt_dry_run
+    local _opt_mountpoint _opt_mount_noauto _opt_nomount _opt_keep _opt_dry_run
 
     local _ds_source_base _ds_source_snapshot _snapshot_suffix
     local _ds_tree _ds _ds_relname _ds_canmount
@@ -265,9 +265,10 @@
     _opt_mountpoint=""
     _opt_mount_noauto=""
     _opt_nomount=""
+    _opt_keep=""
     _opt_dry_run=""
 
-    while getopts "AM:nu" _opt ; do
+    while getopts "AM:knu" _opt ; do
         case ${_opt} in
             A)
                 _opt_mount_noauto="-o canmount=noauto"
@@ -275,6 +276,9 @@
             M)
                 _opt_mountpoint="${OPTARG}"
                 ;;
+            k)
+                _opt_keep="yes"
+                ;;
             n)
                 _opt_dry_run="-n"
                 ;;
@@ -375,12 +379,14 @@
     done
     # Reset to default
     IFS=$' \t\n'
-    # Remove received snapshots
+    # Remove received snapshots by default
     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}'"
+        if [ -z "${_opt_keep}" ]; 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
     fi
 }