From 1aad3d96faf064a482b5d7ab656c2d5fe43c8abf Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 18:57:05 +0100 Subject: [PATCH 1/9] Bugfixes: canmount, config log, init repl --- bashclub-zsync/usr/bin/bashclub-zsync | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 656dc5b..9907d71 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -98,10 +98,10 @@ if [[ $source == "" ]]; then log "source is empty, switching to local mode." ssh= sshport= - log "Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\n" + log "Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" else sshport=-p$sshport - log "Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\n" + log "Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" fi local_os_id=$($grep -E "^ID=" /etc/os-release | $cut -d'=' -f2) @@ -167,7 +167,8 @@ fi for name in "${syncvols[@]}"; do log "Replicate $name" - if [[ $($ssh $sshcipher $sshport $source zfs get -H -o value type $name) == "filesystem" ]]; then + fstype=$($ssh $sshcipher $sshport $source zfs get -H -o value type $name) + if [[ $fstype == "filesystem" ]]; then mp=-xmountpoint cm=-ocanmount=noauto else @@ -192,8 +193,8 @@ for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start initial replication"; fi IFS=$'\n' for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do - log "Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" - $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) + log "Start initial replication: $snap => $target/$(echo $name | rev | $cut -d'/' -f1 | rev)" + $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | rev | $cut -d'/' -f1 | rev) done fi @@ -201,7 +202,7 @@ for name in "${syncvols[@]}"; do guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | $tail -1) last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1") IFS=$'\n' - if [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then + if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then $zfs set canmount=noauto $target/$name fi for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do -- 2.30.2 From 2ab3bf7d29028a78348007c8b4791894a90f81f7 Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 18:58:28 +0100 Subject: [PATCH 2/9] Chenge path in Readme to dev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f946f94..f35c721 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ ZFS replication script by Thorsten Spille #### Download and make executable ~~~ -wget -q --no-cache -O /usr/bin/bashclub-zsync https://git.bashclub.org/bashclub/zsync/raw/branch/main/bashclub-zsync/usr/bin/bashclub-zsync +wget -q --no-cache -O /usr/bin/bashclub-zsync https://git.bashclub.org/bashclub/zsync/raw/branch/dev/bashclub-zsync/usr/bin/bashclub-zsync chmod +x /usr/bin/bashclub-zsync bashclub-zsync ~~~ -- 2.30.2 From 62e31fdfba1f09dc68b76a981c8b4202414e05f4 Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 19:20:14 +0100 Subject: [PATCH 3/9] Fix dataset creation --- bashclub-zsync/usr/bin/bashclub-zsync | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 9907d71..636cb61 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -181,7 +181,7 @@ for name in "${syncvols[@]}"; do if ! $zfs list -H $target/$name > /dev/null 2>&1 ; then if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi prefix="" - for part in $(echo $target/$(echo $name | $cut -d'/' -f1) | $sed "s/\// /g"); do + for part in $(echo $target/$(echo $name | rev | $cut -d'/' -f1 | rev) | $sed "s/\// /g"); do if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi log "Creating $prefix$part" -- 2.30.2 From 4cd065ab7710011c070f18b640dd9fa266aefd2e Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 19:56:07 +0100 Subject: [PATCH 4/9] Revert dataset creation & initial replication fix --- bashclub-zsync/usr/bin/bashclub-zsync | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 636cb61..dde5f79 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -181,7 +181,7 @@ for name in "${syncvols[@]}"; do if ! $zfs list -H $target/$name > /dev/null 2>&1 ; then if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi prefix="" - for part in $(echo $target/$(echo $name | rev | $cut -d'/' -f1 | rev) | $sed "s/\// /g"); do + for part in $(echo $target/$(echo $name | $cut -d'/' -f1) | $sed "s/\// /g"); do if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi log "Creating $prefix$part" @@ -193,8 +193,8 @@ for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start initial replication"; fi IFS=$'\n' for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do - log "Start initial replication: $snap => $target/$(echo $name | rev | $cut -d'/' -f1 | rev)" - $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | rev | $cut -d'/' -f1 | rev) + log "Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" + $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) done fi -- 2.30.2 From 2d18b3205142223d0c15d90a3f6e784ec605b946 Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 21:54:10 +0100 Subject: [PATCH 5/9] Detect incompatible target, fix logmessages --- bashclub-zsync/usr/bin/bashclub-zsync | 44 +++++++++++++++------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index dde5f79..4ab96c8 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -76,7 +76,7 @@ function log() { # load config file if [ -f $conf ]; then - log "Reading configuration $conf" + log "[INFO] Reading configuration $conf" source $conf else mkdir -p $(dirname $conf) @@ -90,18 +90,18 @@ min_keep=$min_keep zfs_auto_snapshot_keep=$zfs_auto_snapshot_keep zfs_auto_snapshot_label=$zfs_auto_snapshot_label EOF - log "Initial config file created. Please adjust and restart script. Exiting..." + log "[INFO] Initial config file created. Please adjust and restart script. Exiting..." usage 0 fi if [[ $source == "" ]]; then - log "source is empty, switching to local mode." + log "[INFO] source is empty, switching to local mode." ssh= sshport= - log "Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" + log "[INFO] Configuration:\n\ttarget=$target\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" else sshport=-p$sshport - log "Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" + log "[INFO] Configuration:\n\ttarget=$target\n\tsource=$source\n\tsshport=$sshport\n\ttag=$tag\n\tsnapshot_filter=$snapshot_filter\n\tmin_keep=$min_keep\nzfs_auto_snapshot_keep=$zfs_auto_snapshot_keep\nzfs_auto_snapshot_label=$zfs_auto_snapshot_label\n" fi local_os_id=$($grep -E "^ID=" /etc/os-release | $cut -d'=' -f2) @@ -127,8 +127,8 @@ else sshcipher=-cchacha20-poly1305@openssh.com fi -include_list="Included datasets:\n" -exclude_list="Excluded datasets:\n" +include_list="[INFO] Included datasets:\n" +exclude_list="[INFO] Excluded datasets:\n" IFS=$'\n' for zvol in $($ssh $sshcipher $sshport $source "zfs get -H -o name,value,source -t filesystem,volume $tag"); do name=$(echo $zvol | $cut -f1) @@ -166,7 +166,7 @@ if [ $zfs_auto_snapshot_keep -gt 0 ]; then fi for name in "${syncvols[@]}"; do - log "Replicate $name" + log "[INFO] Replicate $name" fstype=$($ssh $sshcipher $sshport $source zfs get -H -o value type $name) if [[ $fstype == "filesystem" ]]; then mp=-xmountpoint @@ -184,7 +184,7 @@ for name in "${syncvols[@]}"; do for part in $(echo $target/$(echo $name | $cut -d'/' -f1) | $sed "s/\// /g"); do if [ $($zfs list $prefix$part > /dev/null 2>&1 ; echo $?) -gt 0 ]; then if [[ $debug == "-v" ]]; then log "[DEBUG] $prefix$part does not exist"; fi - log "Creating $prefix$part" + log "[INFO] Creating $prefix$part" $zfs create -o canmount=noauto $autosnap -p $prefix$part fi prefix="$prefix$part/" @@ -193,23 +193,27 @@ for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start initial replication"; fi IFS=$'\n' for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do - log "Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" + log "[INFO] Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) done fi if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start incremental replication"; fi guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | $tail -1) - last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1") - IFS=$'\n' - if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then - $zfs set canmount=noauto $target/$name + if [[ $guid != "" ]]; then + last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1") + IFS=$'\n' + if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then + $zfs set canmount=noauto $target/$name + fi + for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do + log "[INFO] Replicating delta of $last => $snap to $target/$name" + $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name + last=$snap + done + else + log "[ERROR] No snapshot found on $target/$name to add incremental snapshots to. The target dataset (with all children) needs to be deleted and recreated via replication." fi - for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do - log "Replicating delta of $last => $snap to $target/$name" - $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name - last=$snap - done if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start deletion of old snapshots"; fi filter=$(echo -e $snapshot_filter | sed "s/|/\n/g") @@ -223,7 +227,7 @@ for name in "${syncvols[@]}"; do for snap in $snaps_to_delete; do if [[ $snap_count -gt $min_keep ]]; then - log "Deleting $snap" + log "[INFO] Deleting $snap" if [[ $debug == "-v" ]]; then log "[DEBUG] $name - snap_count=$snap_count, min_keep=$min_keep"; fi $zfs destroy $debug $snap snap_count=$(expr $snap_count - 1) -- 2.30.2 From 4b23888a13a8d6f84c4fea819a24f304ed0d1d27 Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 22:20:54 +0100 Subject: [PATCH 6/9] Add returncode --- bashclub-zsync/usr/bin/bashclub-zsync | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 4ab96c8..64be7e2 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -16,6 +16,7 @@ wc=$(which wc) tr=$(which tr) sed=$(which sed) zfs_auto_snapshot=$(which zfs-auto-snapshot) +rc=0 debug= #### default config file, can be changed with parameter -c @@ -213,6 +214,7 @@ for name in "${syncvols[@]}"; do done else log "[ERROR] No snapshot found on $target/$name to add incremental snapshots to. The target dataset (with all children) needs to be deleted and recreated via replication." + if [ $rc -eq 0 ]; then rc=1; fi fi if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start deletion of old snapshots"; fi @@ -241,3 +243,4 @@ for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - No snapshots found with filter $snapshot_filter"; fi fi done +exit $rc \ No newline at end of file -- 2.30.2 From da27f61fdb80fe2f87128c9b8a2ed0097764dbbe Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 22:40:27 +0100 Subject: [PATCH 7/9] Additional error handling and return code --- bashclub-zsync/usr/bin/bashclub-zsync | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 64be7e2..a53ad10 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -196,6 +196,7 @@ for name in "${syncvols[@]}"; do for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do log "[INFO] Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) + if [ $? -gt $rc ]; then rc=1; fi done fi @@ -203,15 +204,21 @@ for name in "${syncvols[@]}"; do guid=$($zfs list -H -o guid -s creation -t snapshot $target/$name | $tail -1) if [[ $guid != "" ]]; then last=$($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -t snapshot $name | grep $guid | tail -1 | cut -f1") - IFS=$'\n' - if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then - $zfs set canmount=noauto $target/$name + if [[ $last != "" ]]; then + IFS=$'\n' + if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then + $zfs set canmount=noauto $target/$name + fi + for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do + log "[INFO] Replicating delta of $last => $snap to $target/$name" + $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name + if [ $? -gt $rc ]; then rc=1; fi + last=$snap + done + else + log "[ERROR] No matching snapshot (guid: $guid) found on source filesystem. This can be outdated snapshots on target or a previously deleted and new created dataset $name on the source filesystem." + if [ $rc -eq 0 ]; then rc=1; fi fi - for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do - log "[INFO] Replicating delta of $last => $snap to $target/$name" - $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name - last=$snap - done else log "[ERROR] No snapshot found on $target/$name to add incremental snapshots to. The target dataset (with all children) needs to be deleted and recreated via replication." if [ $rc -eq 0 ]; then rc=1; fi -- 2.30.2 From e90697ae5930627fe5ac3e98039f4e723c9d721f Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 23:23:30 +0100 Subject: [PATCH 8/9] Fis snapshot filtering --- bashclub-zsync/usr/bin/bashclub-zsync | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index a53ad10..06bb261 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -41,7 +41,7 @@ snapshot_filter="hourly|daily|weekly|monthly" # minimum count of snapshots per filter to keep min_keep=3 -# number of zfs snapshots to keep on source (0 = snapshot function disabled) +# number of zfs snapshots to keep on source (0 or 1 = snapshot function disabled) zfs_auto_snapshot_keep=0 # make snapshot via zfs-auto-snapshot before replication @@ -156,7 +156,7 @@ else fi fi -if [ $zfs_auto_snapshot_keep -gt 0 ]; then +if [ $zfs_auto_snapshot_keep -gt 1 ]; then log "[DEBUG] Running zfs-auto-snapshot" $ssh $sshcipher $sshport $source "which zfs-auto-snapshot > /dev/null || exit 0 ; zfs-auto-snapshot --quiet --syslog --label=$zfs_auto_snapshot_label --keep=$zfs_auto_snapshot_keep //" if [[ $snapshot_filter == "" ]]; then @@ -177,7 +177,7 @@ for name in "${syncvols[@]}"; do cm= fi - if [[ $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name 2>/dev/null | grep -E \"$snapshot_filter\" | wc -l | tr -d ' '") -gt 0 ]]; then + if [[ $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name 2>/dev/null | grep -E \"@.*($snapshot_filter)\" | wc -l | tr -d ' '") -gt 0 ]]; then IFS=$' ' if ! $zfs list -H $target/$name > /dev/null 2>&1 ; then if [[ $debug == "-v" ]]; then log "[DEBUG] $target/$name does not exist"; fi @@ -193,7 +193,7 @@ for name in "${syncvols[@]}"; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Start initial replication"; fi IFS=$'\n' - for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"$snapshot_filter\" | tail -1"); do + for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"@.*($snapshot_filter)\" | tail -1"); do log "[INFO] Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) if [ $? -gt $rc ]; then rc=1; fi @@ -209,7 +209,7 @@ for name in "${syncvols[@]}"; do if [[ $fstype == "filesystem" ]] && [[ $($zfs get -H -o value canmount $target/$name) != "noauto" ]]; then $zfs set canmount=noauto $target/$name fi - for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"$snapshot_filter\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do + for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"@.*($snapshot_filter)\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do log "[INFO] Replicating delta of $last => $snap to $target/$name" $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name if [ $? -gt $rc ]; then rc=1; fi @@ -229,10 +229,10 @@ for name in "${syncvols[@]}"; do IFS=$'\n' for interval in $filter ; do if [[ $debug == "-v" ]]; then log "[DEBUG] $name - Checking interval $interval"; fi - guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep -E \"(.*)@(.*)$interval\" | cut -f1 | tail -1") + guid=$($ssh $sshcipher $sshport $source "zfs list -H -o guid,name -S creation -t snapshot $name | grep -E \"@.*$interval\" | cut -f1 | tail -1") if [[ "$(echo -e "$guid" | sed 's/\n//g')" != "" ]]; then - snaps_to_delete=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | $grep -E "(.*)@(.*)$interval" | $grep --after-context=200 $guid | $grep -v $guid | $cut -f1) - snap_count=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | $grep -E "(.*)@(.*)$interval" | $wc -l | $tr -d ' ') + snaps_to_delete=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | $grep -E "@.*$interval" | $grep --after-context=200 $guid | $grep -v $guid | $cut -f1) + snap_count=$($zfs list -H -o name,guid -S creation -t snapshot $target/$name | $grep -E "@.*$interval" | $wc -l | $tr -d ' ') for snap in $snaps_to_delete; do if [[ $snap_count -gt $min_keep ]]; then -- 2.30.2 From 4b09edb962d85cc7b71b684477e9d8f9d5991340 Mon Sep 17 00:00:00 2001 From: "thorsten.spille" Date: Tue, 21 Nov 2023 23:55:14 +0100 Subject: [PATCH 9/9] Add log message, if repl fails --- bashclub-zsync/usr/bin/bashclub-zsync | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashclub-zsync/usr/bin/bashclub-zsync b/bashclub-zsync/usr/bin/bashclub-zsync index 06bb261..f7f3ab7 100644 --- a/bashclub-zsync/usr/bin/bashclub-zsync +++ b/bashclub-zsync/usr/bin/bashclub-zsync @@ -196,7 +196,7 @@ for name in "${syncvols[@]}"; do for snap in $($ssh $sshcipher $sshport $source "zfs list -H -t snapshot -o name -S creation $name | grep -E \"@.*($snapshot_filter)\" | tail -1"); do log "[INFO] Start initial replication: $snap => $target/$(echo $name | $cut -d'/' -f1)" $ssh $sshcipher $sshport $source "zfs send -w -p $debug $snap" | $zfs receive $mp $cm -x $tag -x com.sun:auto-snapshot $debug -dF $target/$(echo $name | $cut -d'/' -f1) - if [ $? -gt $rc ]; then rc=1; fi + if [ $? -gt $rc ]; then rc=1; log "[ERROR] initial replication to $target/$name failed."; fi done fi @@ -212,7 +212,7 @@ for name in "${syncvols[@]}"; do for snap in $($ssh $sshcipher $sshport $source "zfs list -H -o name,guid -s creation -t snapshot $name | grep -E \"@.*($snapshot_filter)\" | grep --after-context=200 $guid | grep -v $guid | cut -f1"); do log "[INFO] Replicating delta of $last => $snap to $target/$name" $ssh $sshcipher $sshport $source "zfs send -w $debug -i $last $snap" | zfs receive -x $tag -x com.sun:auto-snapshot -F $debug $target/$name - if [ $? -gt $rc ]; then rc=1; fi + if [ $? -gt $rc ]; then rc=1; log "[ERROR] incremental replication to $target/$name failed."; fi last=$snap done else -- 2.30.2