From aa7a3f1177611ef7148c40a94e10774bed4ad09a Mon Sep 17 00:00:00 2001 From: "max.mehl" Date: Sat, 29 Jun 2019 16:46:14 +0200 Subject: [PATCH] add possibility to define SSH key --- config.cfg.sample | 3 +++ ssh-checker.sh | 33 +++++++++++++-------------------- uberspace-backup.sh | 31 ++++++++++++------------------- 3 files changed, 28 insertions(+), 39 deletions(-) diff --git a/config.cfg.sample b/config.cfg.sample index 16b2bd6..847a394 100644 --- a/config.cfg.sample +++ b/config.cfg.sample @@ -9,3 +9,6 @@ GPG=6775E8DDD8CEABCC83E38CEHE6334BCA29DF8192 # Maximum number of backups that shall be retained (0 to disable automatic deletion) MAXBAK=3 + +# SSH key +#SSH_KEY="~/.ssh/mykey_rsa" diff --git a/ssh-checker.sh b/ssh-checker.sh index 2bc4d02..ea56c93 100755 --- a/ssh-checker.sh +++ b/ssh-checker.sh @@ -1,21 +1,6 @@ #!/bin/bash -######################################################################## -# Copyright (C) 2017 Max Mehl -######################################################################## -# -# 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 . -# +# SPDX-Copyright: 2019 Max Mehl +# SPDX-License-Identifier: GPL-3.0-or-later ######################################################################## # # Reads hosts file and checks SSH access. If not possible with public @@ -28,7 +13,15 @@ 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 [ ! -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 + SSH_KEY_ARG="-i ${SSHKEY}" +else + # defaults + SSH_KEY_ARG="" + SSH_KEY=~/.ssh/id_rsa +fi ARG1="$1" @@ -48,7 +41,7 @@ while read line; do echo "[INFO] Trying ${RHOST}" - STATUS=$(ssh -n -o BatchMode=yes -o ConnectTimeout=5 ${RHOST} "echo -n"; echo $?) + STATUS=$(ssh -n -o BatchMode=yes -o ConnectTimeout=5 ${SSH_KEY_ARG} ${RHOST} "echo -n"; echo $?) if [ $STATUS != 0 ]; then echo -n "[ERROR] No SSH login possible for ${RHOST}. " @@ -57,7 +50,7 @@ while read line; do exit 1 else echo "Adding public key with password: " - cat ~/.ssh/id_rsa.pub | ssh ${RHOST} 'cat >> ~/.ssh/authorized_keys' + cat "${SSH_KEY}".pub | ssh ${RHOST} 'cat >> ~/.ssh/authorized_keys' fi else echo "[SUCCESS] SSH login possible for ${RHOST}." diff --git a/uberspace-backup.sh b/uberspace-backup.sh index b141e56..5d5720c 100755 --- a/uberspace-backup.sh +++ b/uberspace-backup.sh @@ -1,21 +1,6 @@ #!/bin/bash -######################################################################## -# Copyright (C) 2017 Max Mehl -######################################################################## -# -# 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 . -# +# SPDX-Copyright: 2019 Max Mehl +# SPDX-License-Identifier: GPL-3.0-or-later ######################################################################## # # Saves specific files and directories from a remote server via SSH. @@ -28,7 +13,15 @@ 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 [ ! -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 + SSH_KEY_ARG="-i ${SSHKEY}" +else + # defaults + SSH_KEY_ARG="" + SSH_KEY=~/.ssh/id_rsa +fi # Get current date DATE=$(date +"%Y-%m-%d_%H-%M") @@ -94,7 +87,7 @@ while read line; do # RSYNC logecho "${RHOST}: Downloading ${SOURCE} to ${DEST}" - rsync -a -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o Compression=no -T -x" ${RHOST}:${SOURCE}/ "${DEST}"/ + rsync -a -e "ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o Compression=no -T -x ${SSH_KEY_ARG}" ${RHOST}:${SOURCE}/ "${DEST}"/ # Pack backup directory, and delete uncompressed one logecho "${RHOST}: Archiving $(basename ${DEST})"