support non-standard SSH ports, fix some shellchecks

This commit is contained in:
2021-08-30 19:09:43 +02:00
parent 4c8127c388
commit 46903a4038
3 changed files with 65 additions and 41 deletions

View File

@@ -2,5 +2,5 @@
# SPDX-License-Identifier: CC0-1.0 # SPDX-License-Identifier: CC0-1.0
# Username@Hostname; Path1 | Path2 | Path3; Uberspace version (default = 7) # Username@Hostname; Path1 | Path2 | Path3; Uberspace version (default = 7)
root@server; /home
user@host.uberspace.de; %virtual | %mysql | /home/user/service user@host.uberspace.de; %virtual | %mysql | /home/user/service
root@server:2222; /home

View File

@@ -2,11 +2,11 @@
# SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx> # SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx>
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
######################################################################## ########################################################################
# #
# Reads hosts file and checks SSH access. If not possible with public # 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 # key, this script tries to place the system's public key on the host
# via a normal (password-based) SSH access attempt. # via a normal (password-based) SSH access attempt.
# #
######################################################################## ########################################################################
CURDIR=$(dirname "$(readlink -f "$0")") CURDIR=$(dirname "$(readlink -f "$0")")
@@ -15,7 +15,7 @@ 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 [ ! -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 [ ! -z "${SSH_KEY}" ]; then if [ -n "${SSH_KEY}" ]; then
SSH_KEY_ARG="-i ${SSH_KEY}" SSH_KEY_ARG="-i ${SSH_KEY}"
else else
# defaults # defaults
@@ -29,19 +29,31 @@ function trim {
sed -r -e 's/^\s*//g' -e 's/\s*$//g' sed -r -e 's/^\s*//g' -e 's/\s*$//g'
} }
while read line; do while read -r line; do
# if line is a comment, go to next line # if line is a comment, go to next line
if $(echo "$line" | grep -qE "^\s*#"); then continue; fi if echo "$line" | grep -qE "^\s*#"; then continue; fi
RHOST=$(echo "$line" | cut -d";" -f1 | trim) 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 if [[ "${ARG1}" != "" ]] && [[ "${ARG1}" != "${RHOST}" ]]; then
continue continue
fi 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}" echo "[INFO] Trying ${RHOST}"
STATUS=$(ssh -n -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=5 ${SSH_KEY_ARG} ${RHOST} "echo -n"; echo $?) 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 if [ $STATUS != 0 ]; then
echo -n "[ERROR] No SSH login possible for ${RHOST}. " echo -n "[ERROR] No SSH login possible for ${RHOST}. "
@@ -50,12 +62,12 @@ while read line; do
exit 1 exit 1
else else
echo "Adding public key with password: " echo "Adding public key with password: "
cat "${SSH_KEY}".pub | ssh ${RHOST} 'cat >> ~/.ssh/authorized_keys' cat "${SSH_KEY}".pub | ssh -o StrictHostKeyChecking=no ${RPORT_ARG} ${SSH_KEY_ARG} "${RHOST}" 'cat >> ~/.ssh/authorized_keys'
fi fi
else else
echo "[SUCCESS] SSH login possible for ${RHOST}." echo "[SUCCESS] SSH login possible for ${RHOST}."
fi fi
echo echo
done < "$HOSTS" done < "$HOSTS"

View File

@@ -2,11 +2,11 @@
# SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx> # SPDX-FileCopyrightText: 2019 Max Mehl <mail [at] mehl [dot] mx>
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
######################################################################## ########################################################################
# #
# Saves specific files and directories from a remote server via SSH. # Saves specific files and directories from a remote server via SSH.
# Provides easy shortcuts for Uberspace.de hosts. # Provides easy shortcuts for Uberspace.de hosts.
# README.md provides more details. # README.md provides more details.
# #
######################################################################## ########################################################################
# Set correct UTF-8 encoding (for FreeBSD jail) # Set correct UTF-8 encoding (for FreeBSD jail)
@@ -18,7 +18,7 @@ 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 [ ! -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 [ ! -z "${SSH_KEY}" ]; then if [ -n "${SSH_KEY}" ]; then
SSH_KEY_ARG="-i ${SSH_KEY}" SSH_KEY_ARG="-i ${SSH_KEY}"
else else
# defaults # defaults
@@ -43,29 +43,41 @@ function logecho {
echo "$(pdate) $@" >> "$LOG" echo "$(pdate) $@" >> "$LOG"
} }
while read line; do while read -r line; do
# if line is a comment or blank, go to next line # if line is a comment or blank, go to next line
if $(echo "$line" | grep -qE "^\s*(#|$)"); then continue; fi if echo "$line" | grep -qE "^\s*(#|$)"; then continue; fi
RHOST=$(echo "$line" | cut -d";" -f1 | trim) RHOST=$(echo "$line" | cut -d";" -f1 | trim)
RUSER=$(echo "$RHOST" | cut -d"@" -f1) RUSER=$(echo "$RHOST" | cut -d"@" -f1)
ALLRDIR=$(echo "$line" | cut -d";" -f2 | trim) ALLRDIR=$(echo "$line" | cut -d";" -f2 | trim)
US_VERSION=$(echo "$line" | cut -d";" -f3 | trim) US_VERSION=$(echo "$line" | cut -d";" -f3 | trim)
# 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
logecho "${RHOST}: Starting backups" logecho "${RHOST}: Starting backups"
# Task ssh-checker.sh to check this host
if ! "${CURDIR}"/ssh-checker.sh "${RHOST}"; then if ! "${CURDIR}"/ssh-checker.sh "${RHOST}"; then
logecho "${RHOST}: ERROR when connecting via SSH. Please run ssh-checker.sh to debug." logecho "${RHOST}: ERROR when connecting via SSH. Please run ssh-checker.sh to debug."
logecho "${RHOST}: Aborting backup after an error." logecho "${RHOST}: Aborting backup after an error."
continue continue
fi fi
NORDIR=$(echo $ALLRDIR | grep -o "|" | wc -l) NORDIR=$(echo "$ALLRDIR" | grep -o "|" | wc -l)
NORDIR=$[$NORDIR + 1] NORDIR=$(($NORDIR + 1))
for ((i = 1; i <= $NORDIR; i++)); do for ((i = 1; i <= $NORDIR; i++)); do
RDIR=$(echo "$ALLRDIR" | cut -d"|" -f${i} | trim) RDIR=$(echo "$ALLRDIR" | cut -d"|" -f${i} | trim)
if [ "${RDIR}" == "%virtual" ]; then if [ "${RDIR}" == "%virtual" ]; then
RDIR=/var/www/virtual/${RUSER} RDIR=/var/www/virtual/${RUSER}
DEST="$BACKUPDIR/$RHOST/$DATE/virtual" DEST="$BACKUPDIR/$RHOST/$DATE/virtual"
@@ -81,7 +93,7 @@ while read line; do
else else
DEST="$BACKUPDIR/$RHOST/$DATE/$(basename "${RDIR}")" DEST="$BACKUPDIR/$RHOST/$DATE/$(basename "${RDIR}")"
fi fi
# Set Source directory, and make exception for %mysql # Set Source directory, and make exception for %mysql
SOURCE="${RDIR}" SOURCE="${RDIR}"
if [ "${RDIR}" == "mysql" ]; then if [ "${RDIR}" == "mysql" ]; then
@@ -91,28 +103,28 @@ while read line; do
SOURCE=/mysql_backup/current/${RUSER} SOURCE=/mysql_backup/current/${RUSER}
fi fi
fi fi
# Create backup destination if necessary # Create backup destination if necessary
if [ ! -e "${DEST}" ]; then mkdir -p "${DEST}"; fi if [ ! -e "${DEST}" ]; then mkdir -p "${DEST}"; fi
# RSYNC # RSYNC
logecho "${RHOST}: Downloading ${SOURCE} to ${DEST}" logecho "${RHOST}: Downloading ${SOURCE} to ${DEST}"
rsync -a -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o Compression=no -T -x ${SSH_KEY_ARG}" ${RHOST}:${SOURCE}/ "${DEST}"/ rsync -a -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o Compression=no -T -x ${RPORT_ARG} ${SSH_KEY_ARG}" "${RHOST}:${SOURCE}"/ "${DEST}"/
# Pack backup directory, and delete uncompressed one # Pack backup directory, and delete uncompressed one
logecho "${RHOST}: Archiving $(basename ${DEST})" logecho "${RHOST}: Archiving $(basename "${DEST}")"
tar cf ${DEST}.tar -C $(echo ${DEST} | sed "s|$(basename ${DEST})$||") $(basename ${DEST}) # TODO: avoid absolute paths tar cf "${DEST}".tar -C $(echo ${DEST} | sed "s|$(basename ${DEST})$||") $(basename ${DEST}) # TODO: avoid absolute paths
rm -rf ${DEST} rm -rf "${DEST}"
# Encrypt archive with GPG (it compresses at the same time) # Encrypt archive with GPG (it compresses at the same time)
logecho "${RHOST}: Encrypting and compressing $(basename ${DEST})" logecho "${RHOST}: Encrypting and compressing $(basename "${DEST}")"
gpg --output ${DEST}.tar.gpg --encrypt --recipient ${GPG} ${DEST}.tar gpg --output "${DEST}".tar.gpg --encrypt --recipient ${GPG} "${DEST}".tar
rm ${DEST}.tar rm "${DEST}".tar
# Delete all old directories except the $MAXBAK most recent # Delete all old directories except the $MAXBAK most recent
if [ $(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | wc -l | tr -d ' ') -gt $MAXBAK ]; then if [ $(ls -tp "${BACKUPDIR}"/"${RHOST}"/ | grep '/$' | wc -l | tr -d ' ') -gt $MAXBAK ]; then
logecho "${RHOST}: Removing older backups of $(basename ${DEST})" logecho "${RHOST}: Removing older backups of $(basename "${DEST}")"
ls -tpd "${BACKUPDIR}"/"${RHOST}"/* | grep '/$' | tail -n +$[$MAXBAK + 1] | xargs -0 | xargs rm -r -- ls -tpd "${BACKUPDIR}"/"${RHOST}"/* | grep '/$' | tail -n +$(($MAXBAK + 1)) | xargs -0 | xargs rm -r --
fi fi
done done