mirror of
https://github.com/m8tin/cis.git
synced 2026-08-01 11:47:31 +02:00
Refactoring
This commit is contained in:
@@ -43,13 +43,9 @@ 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#*@}"
|
||||
_COMMON_SNAPSHOT_CANDIDATE="${3:?"printFoundCommonSnapshot(): Missing third parameter COMMON_SNAPSHOT_CANDIDATE"}"
|
||||
readonly _ZFS _RECEIVERHOST _COMMON_SNAPSHOT_CANDIDATE
|
||||
|
||||
# Nothing to do
|
||||
[ "${_COMMON_SNAPSHOT_CANDIDATE}" == "@" ] \
|
||||
&& return 0
|
||||
|
||||
while read -r _ROW
|
||||
do
|
||||
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}_")
|
||||
}
|
||||
|
||||
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() {
|
||||
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"}"
|
||||
_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")
|
||||
_ZFS=$(composition.printZfsVerified "${_COMPOSITION}")
|
||||
_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.
|
||||
! _COMMON_SNAPSHOT=$(printFoundCommonSnapshot "${_ZFS}" "${_RECEIVERHOST}" "${_RECEIVERS_SNAPSHOT}") \
|
||||
&& echo "Failure in sync-send.sh: abort" >&2 \
|
||||
&& return 1
|
||||
|
||||
[ "${_COMMON_SNAPSHOT}" == "" ] \
|
||||
&& zfs snapshot "${_NEW_SNAPSHOT}" \
|
||||
&& zfs send -c -R "${_NEW_SNAPSHOT}" \
|
||||
&& return 0
|
||||
|
||||
[ "${_COMMON_SNAPSHOT}" != "" ] \
|
||||
&& removeReceiverhostsSyncSnapshotsExeptTheCommonOne "${_ZFS}" "${_RECEIVERHOST}" "${_COMMON_SNAPSHOT}" \
|
||||
&& zfs snapshot "${_NEW_SNAPSHOT}" \
|
||||
@@ -126,28 +118,16 @@ function send() {
|
||||
|
||||
|
||||
|
||||
# Parameter 1: Only alphanumeric characters allowed and [.-] if not leading (due to: -oProxyCommand=...).
|
||||
# Parameter 2: Only alphanumeric characters allowed and [._-] if not leading (due to: -oProxyCommand=...).
|
||||
# Parameter 3: Only alphanumeric characters allowed and [._:-] if not leading (due to: -oProxyCommand=...), but can be empty.
|
||||
# Parameter 4: Only alphanumeric characters allowed and [._:-] if not leading (due to: -oProxyCommand=...), but can be empty.
|
||||
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
|
||||
base.set COMPOSITION "${1}" "${REGEX[COMPOSITION]}"
|
||||
base.set RECEIVERHOST "${2}" "${REGEX[HOST]}"
|
||||
base.set RECEIVERS_SNAPSHOT "${3}" "${REGEX[SNAPSHOT]}"
|
||||
base.set RESUME_TOKEN "${4}" "${REGEX[RESUMETOKEN]}" optional
|
||||
|
||||
! composition.shouldBeSyncedByGivenHost "${COMPOSITION}" "${RECEIVERHOST}" \
|
||||
&& base.abort "no sync-host available" \
|
||||
"Host '${RECEIVERHOST}' is no sync-host for composition: '${COMPOSITION}'"
|
||||
|
||||
# Resume mode
|
||||
if [ "${RECEIVERS_SNAPSHOT}" == '@RESUME' ] && [ -n "${RESUME_TOKEN}" ]; then
|
||||
sendResume "${RESUME_TOKEN}"
|
||||
|
||||
# Exit preserving the code
|
||||
exit $?
|
||||
fi
|
||||
|
||||
send "${COMPOSITION}" "${RECEIVERHOST}" "${RECEIVERS_SNAPSHOT}" \
|
||||
send "${COMPOSITION}" "${RECEIVERHOST}" "${RECEIVERS_SNAPSHOT}" "${RESUME_TOKEN}" \
|
||||
&& exit 0
|
||||
|
||||
print.failure "Something unexpected happend."
|
||||
|
||||
@@ -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
|
||||
print.data "Removing foreign snapshot: ${_SNAP} ... " \
|
||||
&& destroySyncSnapshot "${_ZFS}" "${_SNAP}" \
|
||||
&& print.good "done\n"
|
||||
&& print.done
|
||||
done
|
||||
|
||||
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
|
||||
print.data "Removing outdated snapshot: ${_SNAP} ... " \
|
||||
&& destroySyncSnapshot "${_ZFS}" "${_SNAP}" \
|
||||
&& print.good "done\n"
|
||||
&& print.done
|
||||
done
|
||||
|
||||
return 0
|
||||
@@ -102,7 +102,7 @@ function receive() {
|
||||
(
|
||||
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)
|
||||
if [ -n "${_RESUME_TOKEN}" ] && [ "${_RESUME_TOKEN}" != "-" ]; then
|
||||
print.important "Resume token present trying to resume at ${_RESUME_TOKEN}\n"
|
||||
@@ -110,11 +110,16 @@ function receive() {
|
||||
else
|
||||
_RESUME_TOKEN=""
|
||||
_COMMON_SNAPSHOT=$(zfs list -H -o name -S creation -t snapshot "${_ZFS_BACKUP}" 2> /dev/null | head -n 1)
|
||||
[ -n "${_COMMON_SNAPSHOT}" ] \
|
||||
&& print.data "Rolling back to newest snapshot: ${_COMMON_SNAPSHOT} ... " \
|
||||
if [ -n "${_COMMON_SNAPSHOT}" ]; then
|
||||
print.data "Newest snapshot found, rolling back: ${_COMMON_SNAPSHOT} ... " \
|
||||
&& zfs rollback -r "${_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
|
||||
|
||||
# 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=...).
|
||||
base.set COMPOSITION "${2}" '^[a-zA-Z0-9][a-zA-Z0-9_-]*$' optional
|
||||
base.set COMPOSITION "${2}" "${REGEX[COMPOSITION]}" optional
|
||||
|
||||
case "${1}" in
|
||||
--onceAll)
|
||||
|
||||
Reference in New Issue
Block a user