1. Download to a local directory, archive/encrypt there 1. Push encrypted archive to remote folder This helps when the backup destination is e.g. a NFS drive. Also, introduce harder checks and fix some flaws. Reviewed-on: #1 Co-authored-by: Max Mehl <mail@mehl.mx> Co-committed-by: Max Mehl <mail@mehl.mx>
72 lines
2.1 KiB
Bash
Executable File
72 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx>
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
########################################################################
|
|
#
|
|
# Reads hosts file and checks SSH access. If not possible with public
|
|
# key, this script tries to place the system's public key on the host
|
|
# via a normal (password-based) SSH access attempt.
|
|
#
|
|
########################################################################
|
|
|
|
CURDIR=$(dirname "$(readlink -f "$0")")
|
|
if [ ! -e "$CURDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
|
source "$CURDIR"/config.cfg
|
|
|
|
if [ ! -e "${HOSTS}" ]; then echo "Missing hosts file. Please set a correct value of HOSTS= in your config file. Current value: ${HOSTS}"; exit 1; fi
|
|
|
|
if [ -n "${SSH_KEY}" ]; then
|
|
SSH_KEY_ARG="-i ${SSH_KEY}"
|
|
else
|
|
# defaults
|
|
SSH_KEY_ARG=""
|
|
SSH_KEY=~/.ssh/id_rsa
|
|
fi
|
|
|
|
ARG1="$1"
|
|
|
|
function trim {
|
|
sed -r -e 's/^\s*//g' -e 's/\s*$//g'
|
|
}
|
|
|
|
while read -r line; do
|
|
# if line is a comment, go to next line
|
|
if echo "$line" | grep -qE "^\s*#"; then continue; fi
|
|
|
|
RHOST=$(echo "$line" | cut -d";" -f1 | trim)
|
|
|
|
# Jump to next line if this line's host does not match host of ARG1 (if given)
|
|
if [[ "${ARG1}" != "" ]] && [[ "${ARG1}" != "${RHOST}" ]]; then
|
|
continue
|
|
fi
|
|
|
|
# Get SSH port if needed
|
|
if echo "$RHOST" | grep -q ":"; then
|
|
RPORT=$(echo "$RHOST" | cut -d":" -f2)
|
|
RHOST=$(echo "$RHOST" | cut -d":" -f1)
|
|
RPORT_ARG="-p ${RPORT}"
|
|
else
|
|
# defaults
|
|
RPORT=""
|
|
RPORT_ARG=""
|
|
fi
|
|
|
|
echo "[INFO] Trying ${RHOST}"
|
|
|
|
STATUS=$(ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=5 ${RPORT_ARG} ${SSH_KEY_ARG} "${RHOST}" "echo -n"; echo $?)
|
|
|
|
if [ $STATUS != 0 ]; then
|
|
echo -n "[ERROR] No SSH login possible for ${RHOST}. "
|
|
if [[ "${ARG1}" != "" ]]; then
|
|
echo "Aborting."
|
|
exit 1
|
|
else
|
|
echo "Adding public key with password: "
|
|
cat "${SSH_KEY}".pub | ssh -o StrictHostKeyChecking=no ${RPORT_ARG} ${SSH_KEY_ARG} "${RHOST}" 'cat >> ~/.ssh/authorized_keys'
|
|
fi
|
|
else
|
|
echo "[SUCCESS] SSH login possible for ${RHOST}."
|
|
fi
|
|
|
|
done < "$HOSTS"
|