Refactoring

This commit is contained in:
m8in
2026-06-27 16:47:38 +02:00
parent cb0a32ac0e
commit 48c040fe56
3 changed files with 34 additions and 48 deletions
+1
View File
@@ -196,6 +196,7 @@ function prepare.setREGEX() {
REGEX[DIRPATH]='/([a-zA-Z0-9._-]+/)*' REGEX[DIRPATH]='/([a-zA-Z0-9._-]+/)*'
REGEX[HOST]='[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?' REGEX[HOST]='[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?'
REGEX[PORT]='[0-9]{1,5}' REGEX[PORT]='[0-9]{1,5}'
REGEX[RESUMETOKEN]='[0-9][A-Za-z0-9_-]+'
REGEX[SNAPSHOT]='@[a-zA-Z]([a-zA-Z0-9.:_-]*[a-zA-Z0-9])?' REGEX[SNAPSHOT]='@[a-zA-Z]([a-zA-Z0-9.:_-]*[a-zA-Z0-9])?'
REGEX[SYNCSNAPSHOT]='@SYNC_[a-zA-Z0-9.:_-]*[a-zA-Z0-9]' REGEX[SYNCSNAPSHOT]='@SYNC_[a-zA-Z0-9.:_-]*[a-zA-Z0-9]'
REGEX[USER]='[a-zA-Z]([a-zA-Z0-9._-]*[a-zA-Z0-9])?' REGEX[USER]='[a-zA-Z]([a-zA-Z0-9._-]*[a-zA-Z0-9])?'
+21 -41
View File
@@ -43,13 +43,9 @@ function printFoundCommonSnapshot() {
local _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE local _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE
_ZFS="${1:?"printFoundCommonSnapshot(): Missing first parameter ZFS"}" _ZFS="${1:?"printFoundCommonSnapshot(): Missing first parameter ZFS"}"
_RECEIVERHOST="${2:?"printFoundCommonSnapshot(): Missing second parameter RECEIVERHOST"}" _RECEIVERHOST="${2:?"printFoundCommonSnapshot(): Missing second parameter RECEIVERHOST"}"
_COMMON_SNAPSHOT_CANDIDATE="@${3#*@}" _COMMON_SNAPSHOT_CANDIDATE="${3:?"printFoundCommonSnapshot(): Missing third parameter COMMON_SNAPSHOT_CANDIDATE"}"
readonly _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE readonly _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE
# Nothing to do
[ "${_COMMON_SNAPSHOT_CANDIDATE}" == "@" ] \
&& return 0
while read -r _ROW while read -r _ROW
do do
if [ "${_ROW}" == "${_ZFS}${_COMMON_SNAPSHOT_CANDIDATE}" ]; then if [ "${_ROW}" == "${_ZFS}${_COMMON_SNAPSHOT_CANDIDATE}" ]; then
@@ -84,37 +80,33 @@ function removeReceiverhostsSyncSnapshotsExeptTheCommonOne() {
done < <(zfs list -H -o name -S creation -t snapshot "${_ZFS}" | grep -E "^${_ZFS}@SYNC_${_RECEIVERHOST}_") done < <(zfs list -H -o name -S creation -t snapshot "${_ZFS}" | grep -E "^${_ZFS}@SYNC_${_RECEIVERHOST}_")
} }
function sendResume() {
local _RESUME_TOKEN
_RESUME_TOKEN="${1:?"sendResume(): Missing first parameter RESUME_TOKEN"}"
readonly _RESUME_TOKEN
zfs send -t "${_RESUME_TOKEN:?"Missing RESUME_TOKEN"}" \
&& return 0
return 1
}
function send() { function send() {
local _COMPOSITION _RECEIVERHOST _RECEIVERS_SNAPSHOT _NOW _ZFS _NEW_SNAPSHOT local _COMPOSITION _RECEIVERHOST _RECEIVERS_SNAPSHOT _RESUME_TOKEN _NOW _ZFS _NEW_SNAPSHOT
_COMPOSITION="${1:?"send(): Missing first parameter COMPOSITION"}" _COMPOSITION="${1:?"send(): Missing first parameter COMPOSITION"}"
_RECEIVERHOST="${2:?"send(): Missing second parameter RECEIVERHOST"}" _RECEIVERHOST="${2:?"send(): Missing second parameter RECEIVERHOST"}"
_RECEIVERS_SNAPSHOT="${3}" _RECEIVERS_SNAPSHOT="${3:?"send(): Missing third parameter RECEIVERS_SNAPSHOT"}"
_RESUME_TOKEN="${4}"
_NOW=$(date -u "+%Y-%m-%d_%H:%M:%SZ") _NOW=$(date -u "+%Y-%m-%d_%H:%M:%SZ")
_ZFS=$(composition.printZfsVerified "${_COMPOSITION}") _ZFS=$(composition.printZfsVerified "${_COMPOSITION}")
_NEW_SNAPSHOT="${_ZFS:?"Missing ZFS"}@SYNC_${_RECEIVERHOST:?"Missing RECEIVERHOST"}_${_NOW:?"Missing NOW"}" _NEW_SNAPSHOT="${_ZFS:?"Missing ZFS"}@SYNC_${_RECEIVERHOST:?"Missing RECEIVERHOST"}_${_NOW:?"Missing NOW"}"
readonly _COMPOSITION _RECEIVERHOST _RECEIVERS_SNAPSHOT _NOW _NEW_SNAPSHOT readonly _COMPOSITION _RECEIVERHOST _RECEIVERS_SNAPSHOT _RESUME_TOKEN _NOW _NEW_SNAPSHOT
[ "${RECEIVERS_SNAPSHOT}" == '@REPLICATION' ] \
&& [ -z "${RESUME_TOKEN}" ] \
&& zfs snapshot "${_NEW_SNAPSHOT}" \
&& zfs send -c -R "${_NEW_SNAPSHOT}" \
&& return 0
[ "${RECEIVERS_SNAPSHOT}" == '@RESUME' ] \
&& [ -n "${RESUME_TOKEN}" ] \
&& zfs send -t "${_RESUME_TOKEN}" \
&& return 0
# This common snapshot is the starting-point, if available. # This common snapshot is the starting-point, if available.
! _COMMON_SNAPSHOT=$(printFoundCommonSnapshot "${_ZFS}" "${_RECEIVERHOST}" "${_RECEIVERS_SNAPSHOT}") \ ! _COMMON_SNAPSHOT=$(printFoundCommonSnapshot "${_ZFS}" "${_RECEIVERHOST}" "${_RECEIVERS_SNAPSHOT}") \
&& echo "Failure in sync-send.sh: abort" >&2 \ && echo "Failure in sync-send.sh: abort" >&2 \
&& return 1 && return 1
[ "${_COMMON_SNAPSHOT}" == "" ] \
&& zfs snapshot "${_NEW_SNAPSHOT}" \
&& zfs send -c -R "${_NEW_SNAPSHOT}" \
&& return 0
[ "${_COMMON_SNAPSHOT}" != "" ] \ [ "${_COMMON_SNAPSHOT}" != "" ] \
&& removeReceiverhostsSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" \ && removeReceiverhostsSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" \
&& zfs snapshot "${_NEW_SNAPSHOT}" \ && zfs snapshot "${_NEW_SNAPSHOT}" \
@@ -126,28 +118,16 @@ function send() {
# Parameter 1: Only alphanumeric characters allowed and [.-] if not leading (due to: -oProxyCommand=...). base.set COMPOSITION "${1}" "${REGEX[COMPOSITION]}"
# Parameter 2: Only alphanumeric characters allowed and [._-] if not leading (due to: -oProxyCommand=...). base.set RECEIVERHOST "${2}" "${REGEX[HOST]}"
# Parameter 3: Only alphanumeric characters allowed and [._:-] if not leading (due to: -oProxyCommand=...), but can be empty. base.set RECEIVERS_SNAPSHOT "${3}" "${REGEX[SNAPSHOT]}"
# Parameter 4: Only alphanumeric characters allowed and [._:-] if not leading (due to: -oProxyCommand=...), but can be empty. base.set RESUME_TOKEN "${4}" "${REGEX[RESUMETOKEN]}" optional
base.set COMPOSITION "${1}" "${REGEX[COMPOSITION]}"
base.set RECEIVERHOST "${2}" "${REGEX[HOST]}"
base.set RECEIVERS_SNAPSHOT "${3}" "${REGEX[SNAPSHOT]}" optional
base.set RESUME_TOKEN "${4}" '^[a-zA-Z0-9][a-zA-Z0-9._:-]*$' optional
! composition.shouldBeSyncedByGivenHost "${COMPOSITION}" "${RECEIVERHOST}" \ ! composition.shouldBeSyncedByGivenHost "${COMPOSITION}" "${RECEIVERHOST}" \
&& base.abort "no sync-host available" \ && base.abort "no sync-host available" \
"Host '${RECEIVERHOST}' is no sync-host for composition: '${COMPOSITION}'" "Host '${RECEIVERHOST}' is no sync-host for composition: '${COMPOSITION}'"
# Resume mode send "${COMPOSITION}" "${RECEIVERHOST}" "${RECEIVERS_SNAPSHOT}" "${RESUME_TOKEN}" \
if [ "${RECEIVERS_SNAPSHOT}" == '@RESUME' ] && [ -n "${RESUME_TOKEN}" ]; then
sendResume "${RESUME_TOKEN}"
# Exit preserving the code
exit $?
fi
send "${COMPOSITION}" "${RECEIVERHOST}" "${RECEIVERS_SNAPSHOT}" \
&& exit 0 && exit 0
print.failure "Something unexpected happend." print.failure "Something unexpected happend."
+12 -7
View File
@@ -41,7 +41,7 @@ function removeForeignSyncSnapshots() {
zfs list -t snapshot -H -o name "${_ZFS}" | grep -- "${_ZFS}@SYNC" | grep -v -i "@SYNC_${_RECEIVERHOST}_" | while read _SNAP; do zfs list -t snapshot -H -o name "${_ZFS}" | grep -- "${_ZFS}@SYNC" | grep -v -i "@SYNC_${_RECEIVERHOST}_" | while read _SNAP; do
print.data "Removing foreign snapshot: ${_SNAP} ... " \ print.data "Removing foreign snapshot: ${_SNAP} ... " \
&& destroySyncSnapshot "${_ZFS}" "${_SNAP}" \ && destroySyncSnapshot "${_ZFS}" "${_SNAP}" \
&& print.good "done\n" && print.done
done done
return 0 return 0
@@ -61,7 +61,7 @@ function removeOutdatedSyncSnapshots() {
zfs list -t snapshot -H -o name "${_ZFS}" | grep -- "${_ZFS}@SYNC_${_RECEIVERHOST}_" | grep -v -i "${_NEWEST_SNAPSHOT}" | while read _SNAP; do zfs list -t snapshot -H -o name "${_ZFS}" | grep -- "${_ZFS}@SYNC_${_RECEIVERHOST}_" | grep -v -i "${_NEWEST_SNAPSHOT}" | while read _SNAP; do
print.data "Removing outdated snapshot: ${_SNAP} ... " \ print.data "Removing outdated snapshot: ${_SNAP} ... " \
&& destroySyncSnapshot "${_ZFS}" "${_SNAP}" \ && destroySyncSnapshot "${_ZFS}" "${_SNAP}" \
&& print.good "done\n" && print.done
done done
return 0 return 0
@@ -102,7 +102,7 @@ function receive() {
( (
flock -n 9 || exit 1 flock -n 9 || exit 1
_COMMON_SNAPSHOT="" local _COMMON_SNAPSHOT _RESUME_TOKEN
_RESUME_TOKEN=$(zfs get -H -o value receive_resume_token "${_ZFS_BACKUP}" 2> /dev/null) _RESUME_TOKEN=$(zfs get -H -o value receive_resume_token "${_ZFS_BACKUP}" 2> /dev/null)
if [ -n "${_RESUME_TOKEN}" ] && [ "${_RESUME_TOKEN}" != "-" ]; then if [ -n "${_RESUME_TOKEN}" ] && [ "${_RESUME_TOKEN}" != "-" ]; then
print.important "Resume token present trying to resume at ${_RESUME_TOKEN}\n" print.important "Resume token present trying to resume at ${_RESUME_TOKEN}\n"
@@ -110,11 +110,16 @@ function receive() {
else else
_RESUME_TOKEN="" _RESUME_TOKEN=""
_COMMON_SNAPSHOT=$(zfs list -H -o name -S creation -t snapshot "${_ZFS_BACKUP}" 2> /dev/null | head -n 1) _COMMON_SNAPSHOT=$(zfs list -H -o name -S creation -t snapshot "${_ZFS_BACKUP}" 2> /dev/null | head -n 1)
[ -n "${_COMMON_SNAPSHOT}" ] \ if [ -n "${_COMMON_SNAPSHOT}" ]; then
&& print.data "Rolling back to newest snapshot: ${_COMMON_SNAPSHOT} ... " \ print.data "Newest snapshot found, rolling back: ${_COMMON_SNAPSHOT} ... " \
&& zfs rollback -r "${_COMMON_SNAPSHOT}" \ && zfs rollback -r "${_COMMON_SNAPSHOT}" \
&& _COMMON_SNAPSHOT="@${_COMMON_SNAPSHOT#*@}" \ && _COMMON_SNAPSHOT="@${_COMMON_SNAPSHOT#*@}" \
&& print.good "done\n" && print.done
else
print.data "No snapshot found, prepare replication ... " \
&& _COMMON_SNAPSHOT='@REPLICATION' \
&& print.done
fi
fi fi
# 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. # 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.
@@ -223,7 +228,7 @@ function usage() {
# Parameter 2: only a subset of alphanumeric characters are allowed and [_-] if not leading (due to: -oProxyCommand=...). # Parameter 2: only a subset of alphanumeric characters are allowed and [_-] if not leading (due to: -oProxyCommand=...).
base.set COMPOSITION "${2}" '^[a-zA-Z0-9][a-zA-Z0-9_-]*$' optional base.set COMPOSITION "${2}" "${REGEX[COMPOSITION]}" optional
case "${1}" in case "${1}" in
--onceAll) --onceAll)