#!/usr/bin/env bash # ========================================================================== # _ _ _ ____ ____ _____ # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| # # --- High-Performance Connectivity Tracer (HiPerConTracer) --- # https://www.nntb.no/~dreibh/hipercontracer/ # ========================================================================== # # High-Performance Connectivity Tracer (HiPerConTracer) # Copyright (C) 2015-2026 by Thomas Dreibholz # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Contact: dreibh@simula.no # Bash options: set -euo pipefail # ###### Usage ############################################################## usage () { local exitCode="$1" 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]" exit "${exitCode}" } # ###### Version ############################################################ version () { echo "HiPerConTracer-Sync @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@" exit 0 } # ###### Main program ####################################################### # ====== Handle arguments =================================================== if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!" exit 1 fi UNAME="$(uname)" case "${UNAME}" in FreeBSD) GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)" ;; NetBSD) GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)" ;; OpenBSD) GETOPT="$(command -v gnugetopt || true)" ;; Darwin) GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)" ;; *) GETOPT="$(command -v getopt || true)" ;; esac if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then echo >&2 "ERROR: Enhanced/GNU getopt is required!" exit 1 fi options="$(${GETOPT} -o N:L:R:C:K:H:qwhv --long nodeid:,local:,remote:,collector:,key:,known-hosts:,quiet,verbose,help,version -a -- "$@")" # shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then usage 1 fi NODE_ID= LOCAL="/var/hipercontracer/data" REMOTE="/var/hipercontracer/data" COLLECTOR= KEY="/var/hipercontracer/ssh/id_ed25519" KNOWN_HOSTS="/var/hipercontracer/ssh/known_hosts" VERBOSE=1 eval set -- "${options}" while [ $# -gt 0 ] ; do case "$1" in -N | --nodeid) NODE_ID="$2" shift ;; -L | --local) LOCAL="$2" shift ;; -R | --remote) REMOTE="$2" shift ;; -C | --collector) COLLECTOR="$2" shift ;; -K | --key) KEY="$2" shift ;; -H | --known-hosts) KNOWN_HOSTS="$2" shift ;; -w | --verbose) VERBOSE=1 ;; -q | --quiet) VERBOSE=0 ;; -h | --help) usage 0 ;; -v | --version) version ;; --) shift break ;; *) # This should not happen: wrong getopt parameters, or missing case? echo >&2 "INTERNAL ERROR: Unhandled option $1!" exit 1 ;; esac shift done if [ "${LOCAL}" == "" ] || [ "${REMOTE}" == "" ] || [ "${COLLECTOR}" == "" ] ; then usage 1 fi if [ ! -d "${LOCAL}" ] ; then echo >&2 "ERROR: Local directory ${LOCAL} does not exist!" exit 1 fi if [ ! -e "${KEY}" ] ; then echo >&2 "ERROR: SSH private key file ${KEY} does not exist!" exit 1 fi if [[ ! "${NODE_ID}" =~ [0-9]+ ]] ; then echo >&2 "ERROR: No or invalid Node ID specified!" exit 1 fi if [ "${NODE_ID}" -lt 1 ] || [ "${NODE_ID}" -gt 9999 ] ; then echo >&2 "ERROR: Invalid number for Node ID: ${NODE_ID}!" exit 1 fi # ====== Run RSync ========================================================== rsyncOptions="-rt" # --remove-source-files if [ ${VERBOSE} -ne 0 ] ; then rsyncOptions="${rsyncOptions} --info=progress2" else rsyncOptions="${rsyncOptions} -q" fi rsyncOptions="${rsyncOptions} --exclude '*.tmp'" rsyncOptions="${rsyncOptions} -e 'ssh -i ${KEY} -oUserKnownHostsFile=${KNOWN_HOSTS}'" user="node${NODE_ID}" command="rsync ${rsyncOptions} ${LOCAL}/ ${user}@${COLLECTOR}:${REMOTE}/${user}/" if [ ${VERBOSE} -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Running RSync: ${command}\x1b[0m" fi sh -c "${command}" && status=0 || status=$? if [ "${status}" -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") RSync failed: ${command}\x1b[0m" exit 1 else ssh -tt -i "${KEY}" -oUserKnownHostsFile="${KNOWN_HOSTS}" "${user}"@"${COLLECTOR}" "date -u +\"%Y-%m-%d %H:%M:%S\" >${REMOTE}/${user}/last-sync.txt" || true if [ ${VERBOSE} -ne 0 ] ; then echo -e "\x1b[34m$(date +"%Y-%m-%d %H:%M:%S") Done.\x1b[0m" fi fi