diff options
author | Jakob Odersky <jakob@odersky.com> | 2020-02-25 19:42:07 +0100 |
---|---|---|
committer | Jakob Odersky <jakob@odersky.com> | 2020-02-25 20:08:31 +0100 |
commit | 595131b8e3ffe99e9187c7adb5e73e922bd931e1 (patch) | |
tree | e2ce6d110858d8077517b602c33637ec704a1ad5 /resteasy/libexec | |
download | resteasy-595131b8e3ffe99e9187c7adb5e73e922bd931e1.tar.gz resteasy-595131b8e3ffe99e9187c7adb5e73e922bd931e1.tar.bz2 resteasy-595131b8e3ffe99e9187c7adb5e73e922bd931e1.zip |
Initial commit
Diffstat (limited to 'resteasy/libexec')
-rwxr-xr-x | resteasy/libexec/resteasy-find-uuid | 22 | ||||
-rw-r--r-- | resteasy/libexec/resteasy-helper.sh | 45 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-inhibit | 13 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-init | 16 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-mount | 27 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-prepare-drive | 35 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-run-backup | 74 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-umount | 12 | ||||
-rwxr-xr-x | resteasy/libexec/resteasy-uninhibit | 13 |
9 files changed, 257 insertions, 0 deletions
diff --git a/resteasy/libexec/resteasy-find-uuid b/resteasy/libexec/resteasy-find-uuid new file mode 100755 index 0000000..331d10a --- /dev/null +++ b/resteasy/libexec/resteasy-find-uuid @@ -0,0 +1,22 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ "$#" -eq 0 ]] || die_with_usage "too many arguments" + +mapfile -t uuids < <(lsblk \ + --noheadings \ + --list \ + --output PARTTYPE,PARTUUID \ + | grep "$RESTEASY_TYPE_UUID" \ + | cut -d' ' -f 2) + +[[ ${#uuids[@]} -gt 0 ]] || die "no resteasy device found" +[[ ${#uuids[@]} -lt 2 ]] || die "more than one resteasy devices found; this command only supports one device plugged in at a time" + +echo "${uuids[0]}" diff --git a/resteasy/libexec/resteasy-helper.sh b/resteasy/libexec/resteasy-helper.sh new file mode 100644 index 0000000..6547626 --- /dev/null +++ b/resteasy/libexec/resteasy-helper.sh @@ -0,0 +1,45 @@ +# shellcheck shell=bash +# shellcheck disable=SC2034 + +# This shell-scriptlet provides utility functions to other resteasy scripts. +# It is meant to be sourced, and should never be executed directly. + +# override $0 to the command's pretty name +BASH_ARGV0=$(basename "$0" | sed 's/-/ /') + +RESTEASY_TYPE_UUID=8e8e0bae-5627-4acd-8fd9-70873806d42e +RESTEASY_MOUNT_POINT=/run/resteasy/mount + +die() { + printf >&2 '%s\n' "$*" + exit 1 +} + +usage() { + echo "usage: $0 $USAGE" + exit 1 +} + +die_with_usage() { + printf >&2 '%s\n' "$*" + echo "usage: $0 $USAGE" >&2 + exit 1 +} + + +log_status() { + echo "status: $1" >&2 + # Send status to a dbus channel, useful for integration with desktop + # notifications. + # Note the "|| true" prevents this function from failing in case dbus is not + # available. + dbus-send --system --type=signal \ + /io/crashbox/resteasy/Main \ + "io.crashbox.resteasy.Info.Status" \ + "string:$1" || true +} + +if [[ ${1:-} == '-h' ]] || [[ ${1:-} == '--help' ]]; then + echo "usage: $0 $USAGE" + exit 0 +fi diff --git a/resteasy/libexec/resteasy-inhibit b/resteasy/libexec/resteasy-inhibit new file mode 100755 index 0000000..0a4b5e6 --- /dev/null +++ b/resteasy/libexec/resteasy-inhibit @@ -0,0 +1,13 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ "$#" -eq 0 ]] || die_with_usage "too many arguments" +[[ $EUID -eq 0 ]] || die "must be root" + +systemctl --no-ask-password --quiet mask --now --runtime resteasy.service diff --git a/resteasy/libexec/resteasy-init b/resteasy/libexec/resteasy-init new file mode 100755 index 0000000..3b98bc5 --- /dev/null +++ b/resteasy/libexec/resteasy-init @@ -0,0 +1,16 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ $# -eq 0 ]] || die_with_usage "too many arguments" +mountpoint -q "$RESTEASY_MOUNT_POINT" || die "no device mounted on $RESTEASY_MOUNT_POINT; run 'resteasy mount' beforehand" +export BORG_REPO="$RESTEASY_MOUNT_POINT/borgbackup/$HOSTNAME" +export BORG_PASSCOMMAND="cat /etc/resteasy" + +mkdir -p "$BORG_REPO" +borg init --encryption=repokey diff --git a/resteasy/libexec/resteasy-mount b/resteasy/libexec/resteasy-mount new file mode 100755 index 0000000..05bbb2a --- /dev/null +++ b/resteasy/libexec/resteasy-mount @@ -0,0 +1,27 @@ +#!/bin/bash +USAGE="[ro]" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +flags= +if [[ "$#" -ge 1 ]]; then + case "$1" in + ro) + shift + flags+=--read-only + ;; + *) + die_with_usage "unknown option: $1" + ;; + esac +fi + +uuid=$(resteasy find-uuid) +mkdir -p "$RESTEASY_MOUNT_POINT" +mount $flags "/dev/disk/by-partuuid/$uuid" "$RESTEASY_MOUNT_POINT" + +echo "$RESTEASY_MOUNT_POINT" diff --git a/resteasy/libexec/resteasy-prepare-drive b/resteasy/libexec/resteasy-prepare-drive new file mode 100755 index 0000000..6da4899 --- /dev/null +++ b/resteasy/libexec/resteasy-prepare-drive @@ -0,0 +1,35 @@ +#!/bin/bash +USAGE="<block_device>" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ $# -eq 1 ]] || die_with_usage "no device given" +device="$1" +[[ -b $device ]] || die "$device is not a block device" + +echo "WARNING: this will erase all data on $device." >&2 +read -r -p "Continue? yes/[no]" yn >&2 +[[ $yn == "yes" ]] || die "aborting" + +sgdisk \ + --zap-all \ + --clear \ + --new=1:0:0 \ + --change-name=1:resteasy \ + --typecode=1:"$RESTEASY_TYPE_UUID" \ + "$device" + +# the kernel needs some time to update partition information +sleep 5 +sgdisk --info=1 "$device" + +# note the '2p': this extract the second line of lsblk, which will correspond to +# the first partition on the device +partition_uuid=$(lsblk "$device" --noheadings --output=PARTUUID | sed --quiet 2p) +[[ -n $partition_uuid ]] || die "cannot determine partition UUID after reformatting" + +mkfs.ext4 "/dev/disk/by-partuuid/$partition_uuid" diff --git a/resteasy/libexec/resteasy-run-backup b/resteasy/libexec/resteasy-run-backup new file mode 100755 index 0000000..4d2cfb2 --- /dev/null +++ b/resteasy/libexec/resteasy-run-backup @@ -0,0 +1,74 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ "$#" -eq 0 ]] || die_with_usage "too many arguments" +[[ $EUID -eq 0 ]] || die "must be root" + +failed() { + log_status "errors encountered during backup" +} +trap failed EXIT + +borg --version + +# location of the mounted borg repository +export BORG_REPO="$RESTEASY_MOUNT_POINT/borgbackup/$HOSTNAME" + +# set some environment variables to prevent borg from asking interactive questions +export BORG_RELOCATED_REPO_ACCESS_IS_OK=no # relocated repos aren't cache-friendly +export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=no +export BORG_PASSCOMMAND="cat /etc/resteasy" + +# backup the system into an archive named after +# the machine this script is currently running on +log_status "starting backup" +borg create \ + --verbose \ + --filter AME \ + --list \ + --stats \ + --show-rc \ + --compression lz4 \ + --exclude-caches \ + --exclude '/var/cache/' \ + --exclude '/var/tmp/' \ + --exclude '/root/.cache/' \ + --exclude '/var/lib/docker/devicemapper/' \ + --exclude '/home/*/.cache/' \ + --exclude '/home/*/.local/share/Steam' \ + ::'{hostname}-{now}' \ + /etc \ + /home \ + /root \ + /srv \ + /usr/local \ + /var + +# unnecessary, but just to be extra sure that data has made it onto disk +sync + +log_status "pruning repository" + +# Use the `prune` subcommand to maintain archives of THIS machine. The +# '{hostname}-' prefix is very important to limit prune's operation to this +# machine's archives and not apply to other machines' archives too +borg prune \ + --list \ + --prefix '{hostname}-' \ + --show-rc \ + --keep-daily 7 \ + --keep-weekly 4 \ + --keep-monthly 12 \ + --keep-yearly 10 + +borg check + +log_status "backup completed successfully" +trap - EXIT + diff --git a/resteasy/libexec/resteasy-umount b/resteasy/libexec/resteasy-umount new file mode 100755 index 0000000..08c0451 --- /dev/null +++ b/resteasy/libexec/resteasy-umount @@ -0,0 +1,12 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ "$#" -eq 0 ]] || die_with_usage "too many arguments" + +umount "$RESTEASY_MOUNT_POINT" diff --git a/resteasy/libexec/resteasy-uninhibit b/resteasy/libexec/resteasy-uninhibit new file mode 100755 index 0000000..08371ec --- /dev/null +++ b/resteasy/libexec/resteasy-uninhibit @@ -0,0 +1,13 @@ +#!/bin/bash +USAGE="" + +# shellcheck source=resteasy-helper.sh +source "$(resteasy --exec-path)/resteasy-helper.sh" + +set -o errexit +set -o nounset + +[[ "$#" -eq 0 ]] || die_with_usage "too many arguments" +[[ $EUID -eq 0 ]] || die "must be root" + +systemctl --no-ask-password --quiet unmask --runtime resteasy.service |