From ad7a075a6cc24f80ff54c43b81db63311fd50fe9 Mon Sep 17 00:00:00 2001 From: m8in Date: Sat, 18 Apr 2026 15:46:26 +0200 Subject: [PATCH] Improvments logic and messages --- script/host/zfs/composition-sync/sync-send.sh | 87 +++++++++++++------ script/host/zfs/composition-sync/sync.sh | 23 ++--- 2 files changed, 73 insertions(+), 37 deletions(-) diff --git a/script/host/zfs/composition-sync/sync-send.sh b/script/host/zfs/composition-sync/sync-send.sh index b20782a..56576f7 100755 --- a/script/host/zfs/composition-sync/sync-send.sh +++ b/script/host/zfs/composition-sync/sync-send.sh @@ -1,34 +1,70 @@ #!/bin/bash -function printFoundCommonSnapshot() { - local _ZFS _COMMON_SNAPSHOT_CANDIDATE - _ZFS="${1:?"printFoundCommonSnapshot(): Missing first parameter ZFS"}" - _COMMON_SNAPSHOT_CANDIDATE="${2:?"printFoundCommonSnapshot(): Missing second parameter COMMON_SNAPSHOT_CANDIDATE"}" - readonly _ZFS _COMMON_SNAPSHOT_CANDIDATE +function printNewestOrdinarySnapshot() { + local _ZFS _RECEIVERHOST + _ZFS="${1:?"printNewestOrdinarySnapshot(): Missing first parameter ZFS"}" + _RECEIVERHOST="${2:?"printNewestOrdinarySnapshot(): Missing second parameter RECEIVERHOST"}" + readonly _ZFS _RECEIVERHOST - local _FOUND_COMMON_SNAPSHOT - _FOUND_COMMON_SNAPSHOT="" + local _RESULT + _RESULT=$(zfs list -H -o name -S creation -t snapshot "${_ZFS}" | grep -vF '@SYNC' | head -n 1 | cut -d'@' -f2) + + [ -n "${_RESULT}" ] \ + && echo "@${_RESULT}" \ + && return 0 + + echo "none" + return 1 +} + +function printNewestSyncSnapshot() { + local _ZFS _RECEIVERHOST + _ZFS="${1:?"printNewestSyncSnapshot(): Missing first parameter ZFS"}" + _RECEIVERHOST="${2:?"printNewestSyncSnapshot(): Missing second parameter RECEIVERHOST"}" + readonly _ZFS _RECEIVERHOST + + local _RESULT + _RESULT=$(zfs list -H -o name -S creation -t snapshot "${_ZFS}" | grep -E "^${_ZFS}@SYNC_${_RECEIVERHOST}_" | head -n 1 | cut -d'@' -f2) + + [ -n "${_RESULT}" ] \ + && echo "@${_RESULT}" \ + && return 0 + + echo "none" + return 1 +} + +function printFoundCommonSnapshot() { + local _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE + _ZFS="${1:?"printFoundCommonSnapshot(): Missing first parameter ZFS"}" + _RECEIVERHOST="${2:?"printFoundCommonSnapshot(): Missing second parameter RECEIVERHOST"}" + _COMMON_SNAPSHOT_CANDIDATE="${3:?"printFoundCommonSnapshot(): Missing third parameter COMMON_SNAPSHOT_CANDIDATE"}" + readonly _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE + + # Nothing to do and nothing found + [ "${_COMMON_SNAPSHOT_CANDIDATE}" == "" ] \ + && return 1 while read -r _ROW do if [ "${_ROW}" == "${_ZFS}@${_COMMON_SNAPSHOT_CANDIDATE}" ]; then - _FOUND_COMMON_SNAPSHOT="${_ROW}" - break + echo "${_ROW}" + return 0 fi done < <(zfs list -H -o name -S creation -t snapshot "${_ZFS}") - [ $? -eq 0 ] \ - && echo "${_FOUND_COMMON_SNAPSHOT}" \ - && return 0 - + echo "No common snapshot found:" >&2 + echo " - snapshot candidate from receiver: @${_COMMON_SNAPSHOT_CANDIDATE}" >&2 + echo " - newest ordinary snapshot of sender: $(printNewestOrdinarySnapshot "${_ZFS}" "${_RECEIVERHOST}")" >&2 + echo " - newest sync snapshot of sender: $(printNewestSyncSnapshot "${_ZFS}" "${_RECEIVERHOST}")" >&2 return 1 } -function removeAllSyncSnapshotsExeptTheCommonOne() { +function removeReceiverhostsSyncSnapshotsExeptTheCommonOne() { local _ZFS _RECEIVERHOST _COMMON_SNAPSHOT - _ZFS="${1:?"removeAllSyncSnapshotsExeptTheCommonOne(): Missing first parameter ZFS"}" - _RECEIVERHOST="${2:?"removeAllSyncSnapshotsExeptTheCommonOne(): Missing second parameter RECEIVERHOST"}" - _COMMON_SNAPSHOT="${3:?"removeAllSyncSnapshotsExeptTheCommonOne(): Missing third parameter COMMON_SNAPSHOT"}" + _ZFS="${1:?"removeReceiverhostsSyncSnapshotsExeptTheCommonOne(): Missing first parameter ZFS"}" + _RECEIVERHOST="${2:?"removeReceiverhostsSyncSnapshotsExeptTheCommonOne(): Missing second parameter RECEIVERHOST"}" + _COMMON_SNAPSHOT="${3:?"removeReceiverhostsSyncSnapshotsExeptTheCommonOne(): Missing third parameter COMMON_SNAPSHOT"}" readonly _ZFS _RECEIVERHOST _COMMON_SNAPSHOT while read -r _ROW @@ -86,20 +122,16 @@ then # Resume mode if [ "${_RECEIVERS_SNAPSHOT}" == "RESUME" ]; then sendResume "${_RESUME_TOKEN}" + + # Exit preserving the code exit $? fi # This common snapshot is the starting-point, if available. - _COMMON_SNAPSHOT="$(printFoundCommonSnapshot ${_ZFS} ${_RECEIVERS_SNAPSHOT})" - - [ "${_COMMON_SNAPSHOT}" == "" ] \ - && [ "${_RECEIVERS_SNAPSHOT}" != "" ] \ - && echo "Requested snapshot '${_RECEIVERS_SNAPSHOT}' not available" \ + ! _COMMON_SNAPSHOT=$(printFoundCommonSnapshot "${_ZFS}" "${_RECEIVERHOST}" "${_RECEIVERS_SNAPSHOT}") \ + && echo "Failure in sync-send.sh: abort" >&2 \ && exit 1 - [ "${_COMMON_SNAPSHOT}" != "" ] \ - && removeAllSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" - # Now create the first or a further sync-snapshot as end-point. _NEW_SNAPSHOT="${_ZFS}@SYNC_${_RECEIVERHOST:?"RECEIVERHOST missing"}_$(date -u "+%Y-%m-%d_%H:%M:%S")" @@ -109,14 +141,15 @@ then && exit 0 [ "${_COMMON_SNAPSHOT}" != "" ] \ - && removeAllSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" \ + && removeReceiverhostsSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" \ && zfs snapshot "${_NEW_SNAPSHOT}" \ && zfs send -c -R -I "${_COMMON_SNAPSHOT}" "${_NEW_SNAPSHOT}" \ && exit 0 else - echo "Failure: At least one parameter is invalid" >&2 + echo "Failure in sync-send.sh: At least one parameter is invalid." >&2 exit 1 fi +echo "Failure in sync-send.sh: Something unexpected happend." >&2 exit 1 diff --git a/script/host/zfs/composition-sync/sync.sh b/script/host/zfs/composition-sync/sync.sh index fad2c6c..afae362 100755 --- a/script/host/zfs/composition-sync/sync.sh +++ b/script/host/zfs/composition-sync/sync.sh @@ -23,7 +23,7 @@ function stopObsoleteScreenSession() { readonly _RECEIVERHOST _SYNCHOSTS_FILE _SCREEN_SESSION _COMPOSITION _PID ! grep -qiE "^${_RECEIVERHOST}$" "${_DEFINITIONS}compositions/${_COMPOSITION}/${_SYNCHOSTS_FILE}" \ - && echo "Stopping screen session of composition-sync: ${_COMPOSITION}" \ + && echo "Stopping sync screen session of composition: ${_COMPOSITION}" \ && screen -XS "${_PID}" quit } @@ -45,7 +45,7 @@ function startMissingScreenSession() { readonly _COMPOSITION _SSH_PORT ! screen -ls | grep -qoE "[0-9]+\.compositionsync\.${_COMPOSITION}" \ - && echo "Starting screen session of composition-sync: ${_COMPOSITION}" \ + && echo "Starting screen sync session of composition: ${_COMPOSITION}" \ && screen -dmS "compositionsync.${_COMPOSITION}" "${_SCRIPT}" --loop "${_COMPOSITION}" "${_SSH_PORT}" } @@ -154,14 +154,17 @@ function receive() { # Add "-s" for resumable streams in the next line at zfs receive. Not done yet because of: cannot receive resume stream: kernel modules must be upgraded to receive this stream. ${_SSH_COMMAND} "sudo ${_SEND_SCRIPT:?"Missing SEND_SCRIPT"} \"${_RECEIVERHOST}\" \"${_COMPOSITION}\" \"${_COMMON_SNAPSHOT#${_ZFS}@}\" \"${_RESUME_TOKEN}\"" | zfs receive -v "${_ZFS}" - [ $? -ne 0 ] \ - && echo "Unable to receive stream unsing these settings:" \ - && echo " - Sending host: ${_SOURCEHOST}:${_SSH_PORT}" \ - && echo " - Receiving host: ${_RECEIVERHOST}" \ - && echo " - Composition: ${_COMPOSITION}" \ - && echo " - Offered snapshot: ${_COMMON_SNAPSHOT}" \ - && echo " - Resume token: ${_RESUME_TOKEN}" \ - && return 1 + if [ $? -ne 0 ]; then + echo "Unable to receive stream unsing these settings:" + echo " - Sending host: ${_SOURCEHOST}:${_SSH_PORT}" + echo " - Receiving host: ${_RECEIVERHOST}" + echo " - Composition: ${_COMPOSITION}" + echo " - Offered snapshot: ${_COMMON_SNAPSHOT}" + echo " - Resume token: ${_RESUME_TOKEN}" + echo "Current state of snapshots:" + zfs list -t snapshot "${_ZFS}" 2> /dev/null | tail + return 1 + fi protectZFS "${_ZFS}" removeForeignSyncSnapshots "${_RECEIVERHOST}" "${_ZFS}"