From 30279d1056fa3e724694fca81483ab7f71ea754d Mon Sep 17 00:00:00 2001 From: m8in Date: Fri, 15 May 2026 23:47:44 +0200 Subject: [PATCH] Using base.module.sh, replaced basename and dirname --- script/check/runAllChecks.sh | 2 +- script/host/nginx/setup.sh | 14 +++--- script/host/user/addNormalUser.sh | 5 +- script/host/zfs/composition-sync/sync.sh | 3 +- .../host/zfs/destroy-all-snapshots-before.sh | 49 +++++++++++++++++++ script/host/zfs/fstrim.sh | 22 +++++++++ 6 files changed, 84 insertions(+), 11 deletions(-) create mode 100755 script/host/zfs/destroy-all-snapshots-before.sh create mode 100755 script/host/zfs/fstrim.sh diff --git a/script/check/runAllChecks.sh b/script/check/runAllChecks.sh index d4accc6..ae44919 100755 --- a/script/check/runAllChecks.sh +++ b/script/check/runAllChecks.sh @@ -34,7 +34,7 @@ function allChecks() { ! [ -x "${_CURRENT_CHECK}" ] \ && continue _CHECK_FOUND="true" - _NAME="$(basename ${_CURRENT_CHECK} | cut -d'.' -f1)" + _NAME="$(echo ${_CURRENT_CHECK##*/} | cut -d'.' -f1)" _CONTEXT="$(echo ${_NAME} | cut -d'_' -f1)" _CHECK="$(echo ${_NAME} | cut -d'_' -f2- | tr '_' ' ')" _RESULT="$("${_CURRENT_CHECK}" && echo OK || echo FAIL)" diff --git a/script/host/nginx/setup.sh b/script/host/nginx/setup.sh index 51e39a5..df7c0e7 100755 --- a/script/host/nginx/setup.sh +++ b/script/host/nginx/setup.sh @@ -1,11 +1,13 @@ #!/bin/bash +source /cis/core/base.module.sh + + function main() { - local _SCRIPTPATH _DH_PATH _SELF_SIGNED_PATH - _SCRIPTPATH="$(cd -- "$(dirname "$0")" > /dev/null 2>&1; pwd -P)" + local _DH_PATH _SELF_SIGNED_PATH _DH_PATH="/etc/ssl/private" _SELF_SIGNED_PATH="/etc/ssl/private" - readonly _SCRIPTPATH _DH_PATH _SELF_SIGNED_PATH + readonly _DH_PATH _SELF_SIGNED_PATH ! dpkg -s nginx > /dev/null 2>&1 \ && apt-get --yes install nginx-full \ @@ -35,9 +37,9 @@ function main() { #TODO Links erstellen # [ -d "/etc/nginx/" ] \ -# && cp "${_SCRIPTPATH}/etc_nginx_conf.d/"* "/etc/nginx/conf.d/" \ +# && cp "${CIS[SCRIPTDIR]:?"Missing global SCRIPTDIR"}/etc_nginx_conf.d/"* "/etc/nginx/conf.d/" \ # && mkdir -p /etc/nginx/ssl-trusted \ -# && cp "${_SCRIPTPATH}/etc_nginx_ssl-trusted/"* "/etc/nginx/ssl-trusted/" \ +# && cp "${CIS[SCRIPTDIR]:?"Missing global SCRIPTDIR"}/etc_nginx_ssl-trusted/"* "/etc/nginx/ssl-trusted/" \ # && mkdir -p /var/www/letsencrypt/.well-known/acme-challenge \ # && echo "Basis-Konfiguration erfolgreich erstellt." \ # || echo "Basis-Konfiguration bereits vorhanden." @@ -51,4 +53,4 @@ function main() { return 1 } -main "$@" && exit 0 || exit 1 +main && exit 0 || exit 1 diff --git a/script/host/user/addNormalUser.sh b/script/host/user/addNormalUser.sh index 380853d..f01233a 100755 --- a/script/host/user/addNormalUser.sh +++ b/script/host/user/addNormalUser.sh @@ -1,8 +1,7 @@ #!/bin/bash +source /cis/base/base.module.sh #WARNING: Used for core functionality in setup.sh # DO NOT rename the script and test changes well! -_SCRIPT_FOLDER="$(dirname $(readlink -f "${0}" 2> /dev/null) 2> /dev/null)" -_ROOT="${_SCRIPT_FOLDER%%/script/*}/" #Removes longest matching pattern '/script/*' from the end -source "${_ROOT:?"Missing ROOT"}core/addNormalUser.sh" "${1:?"Missing first parameter USER"}" && exit 0 || exit 1 +source "${CIS[COREROOT]:?"Missing global COREROOT"}addNormalUser.sh" "${1:?"Missing first parameter USER"}" && exit 0 || exit 1 diff --git a/script/host/zfs/composition-sync/sync.sh b/script/host/zfs/composition-sync/sync.sh index c0f9b39..5777b42 100755 --- a/script/host/zfs/composition-sync/sync.sh +++ b/script/host/zfs/composition-sync/sync.sh @@ -51,7 +51,8 @@ function addSessions() { local _COMPOSITION grep -lrE "^${_RECEIVERHOST}" ${_DEFINITIONS}compositions/*/${_SYNCHOSTS_FILE} | while read -r _CURRENT_SYNCHOSTS_FILE; do _SSH_PORT=$(grep -E "^${_RECEIVERHOST} usePort [0-9]*.*$" "${_CURRENT_SYNCHOSTS_FILE}" | cut -d' ' -f3 | xargs) - _COMPOSITION=$(basename $(dirname "${_CURRENT_SYNCHOSTS_FILE}")) + _COMPOSITION="${_CURRENT_SYNCHOSTS_FILE%/*}" + _COMPOSITION="${_COMPOSITION##*/}" startMissingScreenSession "${_COMPOSITION}" "${_SSH_PORT}" done } diff --git a/script/host/zfs/destroy-all-snapshots-before.sh b/script/host/zfs/destroy-all-snapshots-before.sh new file mode 100755 index 0000000..57da2b2 --- /dev/null +++ b/script/host/zfs/destroy-all-snapshots-before.sh @@ -0,0 +1,49 @@ +#!/bin/bash +source /cis/core/base.module.sh + + + +function listSnapshotsToDestroy() { + local _FILESYSTEM _SNAPSHOT + _SNAPSHOT="${1:?"Missing first parameter SNAPSHOT"}" + _FILESYSTEM="$(echo ${_SNAPSHOT} | cut -d@ -f1)" + readonly _FILESYSTEM _SNAPSHOT + + for _CURRENT in $(zfs list -Ho name -s creation -t snapshot "${_FILESYSTEM:?"Missing first parameter FILESYSTEM"}") + do + [ -z "${_CURRENT}" ] \ + && return 1 + + [ "${_SNAPSHOT}" == "${_CURRENT}" ] \ + && break + + echo "${_CURRENT}" + done + return 0 +} + +function main() { + local _SNAPSHOT + _SNAPSHOT="${1:?"Missing first parameter SNAPSHOT"}" + readonly _SNAPSHOT + + ! echo "${_SNAPSHOT}" | grep -q "@" \ + && echo "This is not a snapshot: ${_SNAPSHOT}" \ + && return 1 + + ! zfs list "${_SNAPSHOT}" &> /dev/null \ + && echo "The snapshot does not exist: ${_SNAPSHOT}" \ + && return 1 + + listSnapshotsToDestroy "${_SNAPSHOT}" | xargs -r -p -n1 zfs destroy \ + && return 0 + + return 1 +} + +base.set SNAPSHOT "${1}" '^[-0-9a-zA-Z_/]@[-0-9a-zA-Z_:.]' +main "${SNAPSHOT:?"Missing first parameter SNAPSHOT"}" && exit 0 + +echo "Something went wrong." +exit 1 + diff --git a/script/host/zfs/fstrim.sh b/script/host/zfs/fstrim.sh new file mode 100755 index 0000000..51c358b --- /dev/null +++ b/script/host/zfs/fstrim.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# trim all mounted or at least root file systems which support it +# call this file with cron + + + +function run(){ + local _FSTRIM _MAJOR_VERSION _START + _MAJOR_VERSION="$(lsb_release -r | cut -f2 | cut -d'.' -f1)" + + [ "${_MAJOR_VERSION:?"Missing MAJOR_VERSION"}" -ge "16" ] \ + && _FSTRIM="fstrim -v --all" \ + || _FSTRIM="fstrim -v /" + + _START="${SECONDS}" + readonly _FSTRIM _MAJOR_VERSION _START + + echo -e "$(date) - fstrim started: '$($_FSTRIM)' (duration: $(date -u -d @"$((${SECONDS} - $_START))" +'%-Hh %Mm %Ss'))" +} + +run > /var/log/fstrim.log