From 599259bcdc8e8d36e7be25cfd065a33b2bd49d3e Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Tue, 16 Mar 2021 17:59:22 +0100 Subject: [PATCH] new: [vps] add local command ``vps`` This command is intended to be run from the local vps being targeted. Signed-off-by: Valentin Lab --- bin/vps | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 bin/vps diff --git a/bin/vps b/bin/vps new file mode 100755 index 0000000..2d7ab94 --- /dev/null +++ b/bin/vps @@ -0,0 +1,109 @@ +#!/bin/bash + + +. /etc/shlib + +include common +include parse +include cmdline +include config + + +[[ "${BASH_SOURCE[0]}" != "${0}" ]] && SOURCED=true + +version=0.1 +desc='Install backup' +help="" + + + + +[ "$SOURCED" ] && return 0 + +## +## Command line processing +## + + +cmdline.spec.gnu +cmdline.spec.reporting + +cmdline.spec.gnu install +cmdline.spec.gnu backup + + +cmdline.spec::cmd:install:run() { + + : +} + + + +cmdline.spec:install:cmd:backup:run() { + + : :posarg: BACKUP_SERVER 'Target backup server' + + : :optval: --service-name,-s "YAML service name in compose + file to check for existence of key. + Defaults to 'rsync-backup'" + : :optval: --compose-file,-f "Compose file location. Defaults to + the value of '\$DEFAULT_COMPOSE_FILE'" + + local service_name compose_file + + [ -e "/etc/compose/local.conf" ] && source /etc/compose/local.conf + + compose_file=${opt_compose_file:-$DEFAULT_COMPOSE_FILE} + service_name=${opt_service_name:-rsync-backup} + + if ! [ -e "$compose_file" ]; then + err "Compose file not found in '$compose_file'." + return 1 + fi + + ## XXXvlab: far from perfect as it mimics and depends internal + ## logic of current default way to get a domain in compose-core + host=$(hostname) + if ! egrep "^$host:" "$compose_file" >/dev/null && + ! egrep "^\s+domain:\s+$host\s*$" "$compose_file" >/dev/null; then + err "Can't find domain '$host' in compose file '$compose_file'." + return 1 + fi + + ip=$(getent ahosts "$host" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+" | head -n 1 | cut -f 1 -d " ") || return 1 + my_ip=$(curl -s myip.kal.fr) + if [ "$ip" != "$my_ip" ]; then + err "IP of '$host' ($ip) doesn't match mine ($my_ip)." + return 1 + fi + + if [ -e "/root/.ssh/rsync_rsa" ]; then + if ! [ -e "/root/.ssh/rsync_rsa.pub" ]; then + err "Didn't find public key in '/root/.ssh/rsync_rsa.pub'. (Private key is present !)." + return 1 + fi + else + Wrap -d "Creating rsync key pair" -- \ + ssh-keygen -t rsa -N \"\" -f /root/.ssh/rsync_rsa -C "rsync@$host" + fi + + if egrep "^$service_name:" "$compose_file" >/dev/null; then + err "There's already a backup service named '$service_name'" + return 1 + fi + + cat <> "$compose_file" + +$service_name: + options: + ident: $host + target: $BACKUP_SERVER + private-key: | +$(cat /root/.ssh/rsync_rsa | sed -r 's/^/ /g') +EOF + + +} + + +cmdline::parse "$@"