From dfb2a6a7b5ba9330a8b4febda4e023a2c82d0a36 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 8 May 2021 09:29:58 +0200 Subject: [PATCH] new: [rsync-backup] use key commentary to store backup type Signed-off-by: Valentin Lab --- rsync-backup/hooks/install.d/60-install.sh | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/rsync-backup/hooks/install.d/60-install.sh b/rsync-backup/hooks/install.d/60-install.sh index 07dd75e0..5854d348 100755 --- a/rsync-backup/hooks/install.d/60-install.sh +++ b/rsync-backup/hooks/install.d/60-install.sh @@ -4,16 +4,20 @@ set -eux -[ "${DOMAIN}" ] || { +[ -n "${DOMAIN}" ] || { echo "Error: you must set \$DOMAIN prior to running this script." >&2 exit 1 } -[ "${BACKUP_SERVER}" ] || { +[ -n "${BACKUP_SERVER}" ] || { echo "Error: you must set \$BACKUP_SERVER prior to running this script." >&2 exit 1 } +KEY_BACKUP_ID=${KEY_BACKUP_ID:-rsync} + +KEY_COMMENTARY="$KEY_BACKUP_ID@$DOMAIN" + MIRROR_DIR_PATH="${MIRROR_DIR_PATH:-$PWD/resources/bin/mirror-dir}" [ -e "$MIRROR_DIR_PATH" ] || { echo "Error: you must set \$MIRROR_DIR_PATH or be the root of the charm to run this script." >&2 @@ -35,8 +39,17 @@ getent passwd rsync >/dev/null || chown rsync:rsync /var/lib/rsync ## rsync ssh key creation -[ -e /var/lib/rsync/.ssh/id_rsa ] || - su -c 'ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -C rsync@'"$DOMAIN" - rsync +if [ -e /var/lib/rsync/.ssh/id_rsa.pub ]; then + ## Mainly for update of old solution + content=$(cat /var/lib/rsync/.ssh/id_rsa.pub) + commentary=${content##* } + if [ "${commentary}" != "$KEY_COMMENTARY" ]; then + echo "Updating ssh key commentary from '${commentary}' to '$KEY_COMMENTARY'" >&2 + sed -ri "s/ [^ ]+\$/ $KEY_COMMENTARY/" /var/lib/rsync/.ssh/id_rsa.pub + fi +else + su -c 'ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa -q -C '"$KEY_COMMENTARY" - rsync +fi dest="$BACKUP_SERVER" if [[ "$dest" == *"/"* ]]; then