From c6a89cbc325db3925172d40b736434a53ef45197 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Wed, 4 Jan 2023 07:30:54 +0100 Subject: [PATCH] Reorder functions --- client/zzz_evobackup | 247 ++++++++++++++++++++++--------------------- 1 file changed, 126 insertions(+), 121 deletions(-) diff --git a/client/zzz_evobackup b/client/zzz_evobackup index d2de288..97a741f 100755 --- a/client/zzz_evobackup +++ b/client/zzz_evobackup @@ -126,6 +126,75 @@ lxc/*/rootfs/var/tmp ##### FUNCTIONS ####################################################### +local_tasks() { + log "START LOCAL_TASKS" + + # Remove previous error files + find "${LOCAL_BACKUP_DIR}/" -type f -name '*.err' -delete + + # You can comment or uncomment sections below to customize the backup + + ## OpenLDAP + # dump_ldap + + ## MySQL + + ### example with global and compressed mysqldump + # dump_mysql_global + ### example with compressed SQL dump (with data) for each databases + # dump_mysql_per_base + ### meta-data (grants, variables, schema…) + # dump_mysql_meta + ### example with two dumps for each table (.sql/.txt) for all databases + # dump_mysql_tabs + ### example with mysqlhotcopy + # dump_mysql_hotcopy + ### example for multiples MySQL instances + # dump_mysql_instances + + ## PostgreSQL + + ### example with global dump + # dump_postgresql_global + ### example with filtered tables ("only" or "except") + # dump_postgresql_filtered + ### example with compressed PostgreSQL dump for each databases + # dump_postgresql_per_base + + ## MongoDB + # dump_mongodb + + ## Redis + # dump_redis + + ## ElasticSearch + # dump_elasticsearch_snapshot + + ## RabbitMQ config + # dump_rabbitmq + + ## MegaCli config + # dump_megacli_config + + ## Dump network routes with mtr and traceroute (warning: could be long with aggressive firewalls) + dump_traceroute + + ## Dump various information about server state + dump_server_state + + ## Dump file access control lists + # dump_facl + + # Remove previous error files + for error_file in $(find "${LOCAL_BACKUP_DIR}/" -type f -name '*.err' -print); do + printf "### Content of %s ###\n" "${error_file}" >&2 + cat "${error_file}" >&2 + printf "########################################\n" >&2 + done + + log "STOP LOCAL_TASKS" +} + # shellcheck disable=SC2317 mysql_list_databases() { port=${1:-"3306"} @@ -628,74 +697,6 @@ dump_elasticsearch_snapshot() { log "LOCAL_TASKS - stop dump_elasticsearch_snapshot" } -local_tasks() { - log "START LOCAL_TASKS" - - # Remove previous error files - find "${LOCAL_BACKUP_DIR}/" -type f -name '*.err' -delete - - # You can comment or uncomment sections below to customize the backup - - ## OpenLDAP - # dump_ldap - - ## MySQL - - ### example with global and compressed mysqldump - # dump_mysql_global - ### example with compressed SQL dump (with data) for each databases - # dump_mysql_per_base - ### meta-data (grants, variables, schema…) - # dump_mysql_meta - ### example with two dumps for each table (.sql/.txt) for all databases - # dump_mysql_tabs - ### example with mysqlhotcopy - # dump_mysql_hotcopy - ### example for multiples MySQL instances - # dump_mysql_instances - - ## PostgreSQL - - ### example with global dump - # dump_postgresql_global - ### example with filtered tables ("only" or "except") - # dump_postgresql_filtered - ### example with compressed PostgreSQL dump for each databases - # dump_postgresql_per_base - - ## MongoDB - # dump_mongodb - - ## Redis - # dump_redis - - ## ElasticSearch - # dump_elasticsearch_snapshot - - ## RabbitMQ config - # dump_rabbitmq - - ## MegaCli config - # dump_megacli_config - - ## Dump network routes with mtr and traceroute (warning: could be long with aggressive firewalls) - dump_traceroute - - ## Dump various information about server state - dump_server_state - - ## Dump file access control lists - # dump_facl - - # Remove previous error files - for error_file in $(find "${LOCAL_BACKUP_DIR}/" -type f -name '*.err' -print); do - printf "### Content of %s ###\n" "${error_file}" >&2 - cat "${error_file}" >&2 - printf "########################################\n" >&2 - done - - log "STOP LOCAL_TASKS" -} build_rsync_main_cmd() { ################################################################### # /!\ WARNING /!\ WARNING /!\ WARNING /!\ WARNING /!\ WARNING /!\ # @@ -765,6 +766,61 @@ build_rsync_canary_cmd() { # output final command echo "${cmd}" } + +# Call test_server with "HOST:PORT" string +# It will return with 0 if the server is reachable. +# It will return with 1 and a message on stderr if not. +test_server() { + item=$1 + # split HOST and PORT from the input string + host=$(echo "${item}" | cut -d':' -f1) + port=$(echo "${item}" | cut -d':' -f2) + + # Test if the server is accepting connections + ssh -q -o "ConnectTimeout ${SSH_CONNECT_TIMEOUT}" "${host}" -p "${port}" -t "exit" + # shellcheck disable=SC2181 + if [ $? = 0 ]; then + # SSH connection is OK + return 0 + else + # SSH connection failed + new_error=$(printf "Failed to connect to \`%s' within %s seconds" "${item}" "${SSH_CONNECT_TIMEOUT}") + log "${new_error}" + SERVERS_SSH_ERRORS=$(printf "%s\\n%s" "${SERVERS_SSH_ERRORS}" "${new_error}" | sed -e '/^$/d') + + return 1 + fi +} +# Call pick_server with an optional positive integer to get the nth server in the list. +pick_server() { + increment=${1:-0} + list_length=$(echo "${SERVERS}" | wc -w) + + if [ "${increment}" -ge "${list_length}" ]; then + # We've reached the end of the list + new_error="No more server available" + log "${new_error}" + SERVERS_SSH_ERRORS=$(printf "%s\\n%s" "${SERVERS_SSH_ERRORS}" "${new_error}" | sed -e '/^$/d') + + # Log errors to stderr + printf "%s\\n" "${SERVERS_SSH_ERRORS}" >&2 + return 1 + fi + + # Extract the day of month, without leading 0 (which would give an octal based number) + today=$(/bin/date +%e) + # A salt is useful to randomize the starting point in the list + # but stay identical each time it's called for a server (based on hostname). + salt=$(hostname | cksum | cut -d' ' -f1) + # Pick an integer between 0 and the length of the SERVERS list + # It changes each day + item=$(( (today + salt + increment) % list_length )) + # cut starts counting fields at 1, not 0. + field=$(( item + 1 )) + + echo "${SERVERS}" | cut -d ' ' -f ${field} +} + sync_tasks() { n=0 server="" @@ -836,59 +892,7 @@ sync_tasks() { log "STOP SYNC_TASKS - server=${server}" } -# Call test_server with "HOST:PORT" string -# It will return with 0 if the server is reachable. -# It will return with 1 and a message on stderr if not. -test_server() { - item=$1 - # split HOST and PORT from the input string - host=$(echo "${item}" | cut -d':' -f1) - port=$(echo "${item}" | cut -d':' -f2) - - # Test if the server is accepting connections - ssh -q -o "ConnectTimeout ${SSH_CONNECT_TIMEOUT}" "${host}" -p "${port}" -t "exit" - # shellcheck disable=SC2181 - if [ $? = 0 ]; then - # SSH connection is OK - return 0 - else - # SSH connection failed - new_error=$(printf "Failed to connect to \`%s' within %s seconds" "${item}" "${SSH_CONNECT_TIMEOUT}") - log "${new_error}" - SERVERS_SSH_ERRORS=$(printf "%s\\n%s" "${SERVERS_SSH_ERRORS}" "${new_error}" | sed -e '/^$/d') - - return 1 - fi -} -# Call pick_server with an optional positive integer to get the nth server in the list. -pick_server() { - increment=${1:-0} - list_length=$(echo "${SERVERS}" | wc -w) - - if [ "${increment}" -ge "${list_length}" ]; then - # We've reached the end of the list - new_error="No more server available" - log "${new_error}" - SERVERS_SSH_ERRORS=$(printf "%s\\n%s" "${SERVERS_SSH_ERRORS}" "${new_error}" | sed -e '/^$/d') - - # Log errors to stderr - printf "%s\\n" "${SERVERS_SSH_ERRORS}" >&2 - return 1 - fi - - # Extract the day of month, without leading 0 (which would give an octal based number) - today=$(/bin/date +%e) - # A salt is useful to randomize the starting point in the list - # but stay identical each time it's called for a server (based on hostname). - salt=$(hostname | cksum | cut -d' ' -f1) - # Pick an integer between 0 and the length of the SERVERS list - # It changes each day - item=$(( (today + salt + increment) % list_length )) - # cut starts counting fields at 1, not 0. - field=$(( item + 1 )) - - echo "${SERVERS}" | cut -d' ' -f${field} -} +# Output a message to the log file log() { msg="${1:-$(cat /dev/stdin)}" pid=$$ @@ -896,6 +900,7 @@ log() { "$(/bin/date +"${DATE_FORMAT}")" "${PROGNAME}" "${pid}" "${msg}" \ >> "${LOGFILE}" } +# Output a message to stderr error() { msg="${1:-$(cat /dev/stdin)}" pid=$$