| 1 | #!/usr/bin/env bash |
| 2 | # ========================================================================== |
| 3 | # _ _ _ ____ ____ _____ |
| 4 | # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 | # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 | # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 | # |
| 9 | # --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 | # https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 | # Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -euo pipefail |
| 33 | |
| 34 | |
| 35 | # ###### Usage ############################################################## |
| 36 | usage () { |
| 37 | local exitCode="$1" |
| 38 | echo >&2 "Usage: $0 [-N|--nodeid node_id] [-L|--local local_directory] [-R|--remote remote_location] [-C|--collector collector] [-K|--key ssh_private_key_file] [-H|--known-hosts known_hosts_file] [-q|--quiet] [-w|--verbose] [-h|--help] [-v|--version]" |
| 39 | exit "${exitCode}" |
| 40 | } |
| 41 | |
| 42 | |
| 43 | # ###### Version ############################################################ |
| 44 | version () { |
| 45 | echo "HiPerConTracer-Sync @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@" |
| 46 | exit 0 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | # ###### Main program ####################################################### |
| 52 | |
| 53 | # ====== Handle arguments =================================================== |
| 54 | if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then |
| 55 | echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!" |
| 56 | exit 1 |
| 57 | fi |
| 58 | UNAME="$(uname)" |
| 59 | case "${UNAME}" in |
| 60 | FreeBSD) |
| 61 | GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)" |
| 62 | ;; |
| 63 | NetBSD) |
| 64 | GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)" |
| 65 | ;; |
| 66 | OpenBSD) |
| 67 | GETOPT="$(command -v gnugetopt || true)" |
| 68 | ;; |
| 69 | Darwin) |
| 70 | GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)" |
| 71 | ;; |
| 72 | *) |
| 73 | GETOPT="$(command -v getopt || true)" |
| 74 | ;; |
| 75 | esac |
| 76 | if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then |
| 77 | echo >&2 "ERROR: Enhanced/GNU getopt is required!" |
| 78 | exit 1 |
| 79 | fi |
| 80 | options="$(${GETOPT} -o N:L:R:C:K:H:qwhv --long nodeid:,local:,remote:,collector:,key:,known-hosts:,quiet,verbose,help,version -a -- "$@")" |
| 81 | # shellcheck disable=SC2181 |
| 82 | if [[ $? -ne 0 ]]; then |
| 83 | usage 1 |
| 84 | fi |
| 85 | |
| 86 | NODE_ID= |
| 87 | LOCAL="/var/hipercontracer/data" |
| 88 | REMOTE="/var/hipercontracer/data" |
| 89 | COLLECTOR= |
| 90 | KEY="/var/hipercontracer/ssh/id_ed25519" |
| 91 | KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts" |
| 92 | VERBOSE=1 |
| 93 | |
| 94 | eval set -- "${options}" |
| 95 | while [ $# -gt 0 ] ; do |
| 96 | case "$1" in |
| 97 | -N | --nodeid) |
| 98 | NODE_ID="$2" |
| 99 | shift |
| 100 | ;; |
| 101 | -L | --local) |
| 102 | LOCAL="$2" |
| 103 | shift |
| 104 | ;; |
| 105 | -R | --remote) |
| 106 | REMOTE="$2" |
| 107 | shift |
| 108 | ;; |
| 109 | -C | --collector) |
| 110 | COLLECTOR="$2" |
| 111 | shift |
| 112 | ;; |
| 113 | -K | --key) |
| 114 | KEY="$2" |
| 115 | shift |
| 116 | ;; |
| 117 | -H | --known-hosts) |
| 118 | KNOWN_HOSTS="$2" |
| 119 | shift |
| 120 | ;; |
| 121 | -w | --verbose) |
| 122 | VERBOSE=1 |
| 123 | ;; |
| 124 | -q | --quiet) |
| 125 | VERBOSE=0 |
| 126 | ;; |
| 127 | -h | --help) |
| 128 | usage 0 |
| 129 | ;; |
| 130 | -v | --version) |
| 131 | version |
| 132 | ;; |
| 133 | --) |
| 134 | shift |
| 135 | break |
| 136 | ;; |
| 137 | *) |
| 138 | # This should not happen: wrong getopt parameters, or missing case? |
| 139 | echo >&2 "INTERNAL ERROR: Unhandled option $1!" |
| 140 | exit 1 |
| 141 | ;; |
| 142 | esac |
| 143 | shift |
| 144 | done |
| 145 | |
| 146 | if [ "${LOCAL}" == "" ] || [ "${REMOTE}" == "" ] || [ "${COLLECTOR}" == "" ] ; then |
| 147 | usage 1 |
| 148 | fi |
| 149 | if [ ! -d "${LOCAL}" ] ; then |
| 150 | echo >&2 "ERROR: Local directory ${LOCAL} does not exist!" |
| 151 | exit 1 |
| 152 | fi |
| 153 | if [ ! -e "${KEY}" ] ; then |
| 154 | echo >&2 "ERROR: SSH private key file ${KEY} does not exist!" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then |
| 159 | echo >&2 "ERROR: No or invalid Node ID specified!" |
| 160 | exit 1 |
| 161 | fi |
| 162 | if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then |
| 163 | echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" |
| 164 | exit 1 |
| 165 | fi |
| 166 | |
| 167 | |
| 168 | # ====== Run RSync ========================================================== |
| 169 | rsyncOptions="-rt" # --remove-source-files |
| 170 | if [ ${VERBOSE} -ne 0 ] ; then |
| 171 | rsyncOptions="${rsyncOptions} --info=progress2" |
| 172 | else |
| 173 | rsyncOptions="${rsyncOptions} -q" |
| 174 | fi |
| 175 | rsyncOptions="${rsyncOptions} --exclude '*.tmp'" |
| 176 | rsyncOptions="${rsyncOptions} -e 'ssh -i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}'" |
| 177 | |
| 178 | user="node${NODE_ID}" |
| 179 | command="rsync ${rsyncOptions} ${LOCAL}/ ${user}@${COLLECTOR}:${REMOTE}/${user}/" |
| 180 | if [ ${VERBOSE} -ne 0 ] ; then |
| 181 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running RSync: ${command}\x1b[0m" |
| 182 | fi |
| 183 | sh -c "${command}" && status=0 || status=$? |
| 184 | if [ "${status}" -ne 0 ] ; then |
| 185 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") RSync failed: ${command}\x1b[0m" |
| 186 | exit 1 |
| 187 | else |
| 188 | ssh -tt -i "${KEY}" -oUserKnownHostsFile="${KNOWN_HOSTS}" "${user}"@"${COLLECTOR}" "date -u +\"%Y-%m-%d %H:%M:%S\" >${REMOTE}/${user}/last-sync.txt" || true |
| 189 | if [ ${VERBOSE} -ne 0 ] ; then |
| 190 | echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" |
| 191 | fi |
| 192 | fi |