From 5901c4ff45184e3abf4e7c220e5c40330188f825 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sun, 25 Apr 2021 09:12:56 +0200 Subject: [PATCH] new: [0km] add ``vps-install backup`` which handle all stage of backup Signed-off-by: Valentin Lab --- README.org | 22 ++++++++++++ bin/0km | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 5b07a54..846aa1a 100644 --- a/README.org +++ b/README.org @@ -745,6 +745,28 @@ export MYSQL_CONTAINER=mailcowdockerized_mysql-mailcow_1 =compose= à backupper. - un serveur =mailcow= est un serveur faisant tourner l'installation =mailcow=. + +***** Via la commande 0km sur un hôte admin + +L'instruction suivante doit être executée sur un hôte ayant la commande +=0km= d'installée et les accès vers le VPS et un compte administration +du serveur d'archivage. + +#+begin_src sh +0km vps-install backup myadmin@core-06.0k.io:10023 myvps.fr +#+end_src + +Note: on peut spécifier autant de vps que l'on souhaite en fin de +ligne de commande. L'installation des vps se fera en parallèle et +vers le serveur d'archive spécifié en premier argument. + +Note: cette commande peut-être executé plusieurs fois sur le même +hôte : si tout est déjà installé, cette commande n'aura pour seul +effet que de relancer une synchronisation vers le backup. + + +***** Via la commande vps sur le vps lui-même + A faire depuis le serveur compose ou mailcow: #+begin_src sh diff --git a/bin/0km b/bin/0km index 5fba286..a87b3a3 100755 --- a/bin/0km +++ b/bin/0km @@ -234,6 +234,76 @@ vps_check() { } +vps_install_backup() { + local vps="$1" admin server + vps_connection_check "$vps" &1); then + err "Command 'vps install backup $server' failed." + return 1 + fi + + already_present= + if e "$out" | grep "^II Entry for service .* is already present" >/dev/null 2>&1; then + already_present=1 + info "Backup entry is already present in 'compose.yml' of '$vps'" + fi + out="${out%$'\n'}" + out="${out#*$'\n'}" + key="${out%\'*}" + key="${key##*\'}" + + if ! [[ "$key" =~ ^"ssh-rsa "[a-zA-Z0-9/+]+" "[a-zA-Z0-9._-]+"@"[a-zA-Z0-9._-]+$ ]]; then + err "Unexpected output from 'vps install backup $server'. Can't find key." + echo "$out" | prefix " ${GRAY}|$NORMAL " >&2 + echo " Extracted key:" >&2 + echo "$key" | prefix " ${GRAY}|$NORMAL " >&2 + return 1 + fi + + if [ "$type" == "compose" ] && [ -z "$already_present" ]; then + ssh:run "root@$vps" -- compose --debug up || { + err "Command 'compose --debug up' failed." + return 1 + } + fi + + dest="$server" + dest="${dest%/*}" + ssh_options=() + if [[ "$dest" == *":"* ]]; then + port="${dest##*:}" + dest="${dest%%:*}" + ssh_options=(-p "$port") + else + port="" + dest="${dest%%:*}" + fi + + cmd=(ssh "${ssh_options[@]}" "$admin"@"$dest" ssh-key add "$key") + echo "${WHITE}Launching:${NORMAL} ${cmd[@]}" + + "${cmd[@]}" || { + err "Failed add key to backup server '$dest'." + return 1 + } + echo "${WHITE}Launching backup${NORMAL} from '$vps'" + + ssh:run "root@$vps" -- vps backup || { + err "First backup failed to run." + return 1 + } + + echo "Backup is ${GREEN}up and running${NORMAL}." +} + + vps_udpate() { local vps="$1" vps_connection_check "$vps" || return 1 @@ -438,8 +508,39 @@ cmdline.spec::cmd:vps-check:run() { } -cmdline.spec.gnu vps-update +cmdline.spec.gnu vps-install +cmdline.spec::cmd:vps-install:run() { + : +} + +cmdline.spec.gnu backup +cmdline.spec:vps-install:cmd:backup:run() { + + : :posarg: BACKUP_TARGET 'Backup target. + (ie: myadmin@backup.domain.org:10023/256)' + : :posarg: [VPS...] 'Target host(s) to check' + + if [ "${#VPS[@]}" == 0 ]; then + warn "VPS list provided in command line is empty. Nothing will be done." + return 0 + fi + + if ! [[ "$BACKUP_TARGET" == *"@"* ]]; then + err "Missing admin account identifier in backup target." + echo " Have you forgottent to specify an account, ie 'myadmin@' ?)" + return 1 + fi + + admin=${BACKUP_TARGET%%@*} + server=${BACKUP_TARGET#*@} + p0 "$admin" "$server" | + vps_mux vps_install_backup "${VPS[@]}" +} + + + +cmdline.spec.gnu vps-update cmdline.spec::cmd:vps-update:run() { : :posarg: [VPS...] 'Target host to check'