From f9f951867c3271f7c434116e1c10a0e2a0635146 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 25 Jun 2021 15:35:13 +0000 Subject: [PATCH] Add script for OpenBSD remote backups --- bin/Makefile | 2 +- bin/rbucmd.sh | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 bin/rbucmd.sh diff --git a/bin/Makefile b/bin/Makefile index 7c05283..499fe8c 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -15,7 +15,7 @@ PREFIX = $(HOME) MANPREFIX = $(PREFIX)/local BIN = ZZZ browser credentials fetch flac2ogg imgresize invidious \ - nyaasearch plumb pstsrv rfcopen screenshot sekrit \ + nyaasearch plumb pstsrv rbucmd rfcopen screenshot sekrit \ w3m-copy-link MAN1 = sekrit.1 BROWSER_LINKS = tor-browser diff --git a/bin/rbucmd.sh b/bin/rbucmd.sh new file mode 100644 index 0000000..2ee1e5f --- /dev/null +++ b/bin/rbucmd.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# env +# Written in 2021 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +usage() +{ + printf "Usage: %s [-c] [-l login_name] host backup-name\n" \ + "${0##*/}" >&2 + exit 1 +} + +err() +{ + printf "%s: %s\n" "${0##*/}" "$*" >&2 + exit 1 +} + +login_name=$(id -nu) +agent_mode=yes +while getopts cl: flag; do + case $flag in + c) agent_mode=confirm ;; + l) login_name=$OPTARG ;; + *) usage + esac +done +shift $((OPTIND - 1)) +if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then + usage +fi +host=$1 +backup_name=$2 + +[ -f "$host/bulist" ] || err "no bulist present in \"$h/\"" + +eval $(ssh-agent -s) +trap 'eval $(ssh-agent -ks)' EXIT INT QUIT TERM + +set -e + +# Load correct key into ssh-agent. +ssh -o AddKeysToAgent=$agent_mode "$login_name@$host" true + +cd "$host" +tmpdir=$(ssh "$login_name@$host" mktemp -dt rbucmd.XXXXXX) + +sftp "$login_name@$host" <\"$tmpdir/bu.sh\"" </tmp/pkg-info.txt && + doas xargs pax -wzf "$tmpdir/$backup_name.tgz" <"$tmpdir/bulist" && + doas chown "$login_name:$login_name" "$tmpdir/$backup_name.tgz" +EOF + +# -t is needed for doas to read a password. +ssh -t "$login_name@$host" sh "$tmpdir/bu.sh" + +sftp -o "user=$login_name" "$host" <