dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit bad0dcc7b1532465271bf541762f430e5d7c2bd9
parent 8019304606c655a41948d86d0ac42d45222fc064
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Thu, 13 Jun 2019 12:46:35 +0200

Move backup script to POSIX sh, improve dwm status refresh

Diffstat:
M.local/bin/backup-b2 | 101++++++++++++++++++++++++++++++++++++++++++-------------------------------------
M.local/bin/dwmstatus-refresh | 10+++++++---
2 files changed, 61 insertions(+), 50 deletions(-)

diff --git a/.local/bin/backup-b2 b/.local/bin/backup-b2 @@ -1,6 +1,7 @@ -#!/usr/bin/env bash +#!/bin/sh +set -e -version=1.0 +version=1.1.0 b2_account="$(pass Online/backblaze.com-anders@adamsgaard.dk-account-id)" b2_key="$(pass Online/backblaze.com-anders@adamsgaard.dk-master-application-key)" @@ -12,9 +13,11 @@ sgn_key="$enc_key" PASSPHRASE="$(pass gpg-anders@adamsgaard.dk)" SIGN_PASSPHRASE="$PASSPHRASE" -opts="--progress --sign-key $sgn_key --encrypt-key $enc_key" +duplicity_with_opts() { + duplicity --progress --sign-key $sgn_key --encrypt-key $enc_key "$@" +} -function show_help { +show_help() { echo "usage: ${0##*/} [OPTIONS] [COMMAND[S]]" echo "is a wrapper for the duplicity command for communicating with an" echo "encrypted backup in backblaze B2 storage." @@ -34,84 +37,95 @@ function show_help { echo " -- do not consider any following args as options" } -function show_version { +show_version() { echo "${0##*/} version $version" echo "Licensed under the GNU Public License, v3+" echo "written by Anders Damsgaard, anders@adamsgaard.dk" echo "https://gitlab.com/admesg/dotfiles" } -function die { +die() { printf '%s\n' "$1" >&2 unset PASSPHRASE unset SIGN_PASSPHRASE exit 1 } -function duplicity_backup_b2 { +duplicity_backup_b2() { local_dir="$1" b2_dir="$2" echo "Backing up home..." - duplicity $opts \ + duplicity_with_opts \ --full-if-older-than 30D \ "$local_dir" b2://"$b2_account":"$b2_key"@"$b2_dir" echo "Cleaning up old backups..." - duplicity $opts \ + duplicity_with_opts \ remove-older-than 90D \ b2://"$b2_account":"$b2_key"@"$b2_dir" echo "Cleaning up failures..." - duplicity $opts \ + duplicity_with_opts \ cleanup --force \ b2://"$b2_account":"$b2_key"@"$b2_dir" echo "B2 collection status:" - duplicity $opts \ + duplicity_with_opts \ collection-status \ b2://"$b2_account":"$b2_key"@"$b2_dir" } -function status_b2 { +status_b2() { printf "\n## %s\n" "$1" - duplicity $opts \ + duplicity_with_opts \ collection-status \ b2://"$b2_account":"$b2_key"@"$1" } -function list_status_b2 { +list_status_b2() { status_b2 "$(hostname)-new-home" status_b2 "$(hostname)-new-etc" status_b2 "$(hostname)-new-usr" } -function backup_b2 { +backup_b2() { duplicity_backup_b2 /home "$(hostname)-new-home" duplicity_backup_b2 /etc "$(hostname)-new-etc" duplicity_backup_b2 /usr "$(hostname)-new-usr" } -function duplicity_list_backup_b2 { - duplicity $opts \ +duplicity_list_backup_b2() { + duplicity_with_opts \ list-current-files \ b2://"$b2_account":"$b2_key"@"$1" } -function list_backups_b2 { +list_backups_b2() { duplicity_list_backup_b2 "$(hostname)-new-home" duplicity_list_backup_b2 "$(hostname)-new-etc" duplicity_list_backup_b2 "$(hostname)-new-usr" } -# TODO: allow restore outside of new-home -function restore_from_backup { - [ "$2" ] || die 'error: supply a <source> and <destination>' - [ "$3" ] || die 'error: supply a <source> and <destination>' - duplicity $opts \ +restore_from_backup_b2() { + [ -n "$1" ] || die 'error: supply a <source> and <destination>' + [ -n "$2" ] || die 'error: missing <destination>' + case "$1" in + ad*) + archive=idkfa-new-home + ;; + etc*) + archive=idkfa-new-etc + ;; + usr*) + archive=idkfa-new-usr + ;; + esac + + duplicity_with_opts \ --file-to-restore "$1" \ - b2://"$b2_account":"$b2_key"@idkfa-new-home "$2" + b2://"$b2_account":"$b2_key"@"$archive" "$2" } while :; do @@ -144,29 +158,22 @@ if [ $# -lt 1 ]; then die 'error: no COMMAND specified, see --help' fi -for command in "$@"; do - case "$command" in - backup) - backup_b2 - ;; - status) - list_status_b2 - ;; - list) - list_backups_b2 - ;; - restore) - [ -e "$2" ] || die 'error: supply a <source> and <destination>' - [ -e "$3" ] || die 'error: supply a <source> and <destination>' - restore_from_backup_b2 "$2" "$3" - shift - shift - ;; - *) - die 'error: invalid COMMAND specified, see --help' - break - esac -done +case "$1" in + backup) + backup_b2 + ;; + status) + list_status_b2 + ;; + list) + list_backups_b2 + ;; + restore) + restore_from_backup_b2 "$2" "$3" + ;; + *) + die 'error: invalid COMMAND specified, see --help' +esac unset PASSPHRASE unset SIGN_PASSPHRASE diff --git a/.local/bin/dwmstatus-refresh b/.local/bin/dwmstatus-refresh @@ -1,6 +1,10 @@ #!/bin/sh -# From lukesmithxyz/voidrice +# Originally from lukesmithxyz/voidrice -kill "$(pstree -lp |\ - grep -- -dwmstatus\([0-9] | sed "s/.*sleep(\([0-9]\+\)).*/\1/")" +pid="$(pstree -lp | grep -- '-dwmstatus([0-9]' |\ + sed "s/.*sleep(\([0-9]\+\)).*/\1/")" + +if echo "$pid" | grep -E -q '^[0-9]+$'; then + kill "$pid" +fi