#!/bin/bash MYPID=$$ ROOT_DIR="/mnt/storage1_loop/borg" BORG_GROUP="borg-backup" THIS_HOST="132.230.4.17" BORG_VERSION="1.1.11" # What is deployed to remote machines... # Should match the local installed version perror() { echo "[ERROR] $*" [ "$$" != "$MYPID" ] && kill "$MYPID" exit 1 } declare -rg MYPID ROOT_DIR BORG_GROUP [ -d "$ROOT_DIR" ] || perror "No such directory: $ROOT_DIR" echo -n "Please enter host name or address of machine to back up (pref. a host name): " read -r name trash || perror "Nothing entered" [ -n "$name" ] || perror "Nothing entered" [ -z "$trash" ] || perror "Must not contain spaces" host=$name basedir="$ROOT_DIR/$name" [ -d "$basedir" ] && perror "Target $basedir already exists" mkdir "$basedir" || perror "Cannot create $basedir" echo -n "Please enter system user to create for this repo: " read -r name trash || perror "Nothing entered" [ -n "$name" ] || perror "Nothing entered" [ -z "$trash" ] || perror "Must not contain spaces" user=$name adduser --home "$basedir" --no-create-home --disabled-password --gecos "Backupuser for $host" "$user" || perror "Could not create user $user" adduser "$user" "$BORG_GROUP" || perror "Could not add user $user to group $BORG_GROUP" mkdir "$basedir/repo" "$basedir/.ssh" || perror "Could not create .ssh and repo" ssh-keygen -N '' -q -f "$basedir/id_borg" || perror "could not create ssh keypair" pubkey=$(cat "$basedir/id_borg.pub") [ -n "$pubkey" ] || perror "Borked pubkey $basedir/id_borg.pub" echo 'command="borg serve --append-only --restrict-to-path '"$basedir"'/repo",no-port-forwarding,no-X11-forwarding,no-pty,no-agent-forwarding,no-user-rc' \ "$pubkey" > "$basedir/.ssh/authorized_keys" rm "$basedir/id_borg.pub" chown -R "${user}:${user}" "$basedir" echo "Deploying borg to $host now (ssh root@$host)...." cat > "$basedir/deployscript" < ".ssh/id_borgbackup" chmod 0600 ".ssh/id_borgbackup" if ! grep -q 'Host ${THIS_HOST}' ".ssh/config"; then echo "Appending section to .ssh/config..." cat >> ".ssh/config" < "/opt/scripts/borg-backup.sh" <