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 1605b87314cce9ea5a4e199e0cfc98cce63a1279
parent 266f7d7e150867fae0f5e05093b7448fb3d48a73
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Wed, 24 Apr 2019 09:36:10 +0200

Add list and restore functionality to backup-b2 script

Diffstat:
M.Xresources | 4++--
Abin/backup-b2 | 156+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dbin/backup-b2.sh | 46----------------------------------------------
3 files changed, 158 insertions(+), 48 deletions(-)

diff --git a/.Xresources b/.Xresources @@ -5,8 +5,8 @@ Xcursor.size: 16 ! terminal colors ------------------------------------------------------------ ! see https://ciembor.github.io/4bit -*.foreground: #c5c8c6 -*.background: #1d1f21 +*.foreground: #4d4d4c +*.background: #ffffff /* black */ *color0: #434944 diff --git a/bin/backup-b2 b/bin/backup-b2 @@ -0,0 +1,156 @@ +#!/usr/bin/env bash + +version=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)" + +# from `gpg --list-public-keys anders@adamsgaard.dk` +enc_key="5C959DF243CE4DD17A5B2610B790F4AD1BF858FE" +sgn_key="$enc_key" + +PASSPHRASE="$(pass gpg-anders@adamsgaard.dk)" +SIGN_PASSPHRASE="$PASSPHRASE" + +opts="--progress --sign-key $sgn_key --encrypt-key $enc_key" + +function 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." + echo "COMMAND can be one of the following:" + echo " backup Backup /home, /etc, and /user, and clean out" + echo " old backups and failures" + echo " list List all files in latest backup set" + echo " restore SRC DST Restore SRC file or dir from backup to local" + echo " DST. The SRC must be the path listed as the" + echo " last column from the 'list' command" + echo "if no COMMAND is specified, the default is 'backup'" + echo + echo "OPTIONS are one or more of the following:" + echo " -h, --help show this message" + echo " -v, --version show version and license information" + echo " -V, --verbose show verbose information" + echo " -- do not consider any following args as options" +} + +function 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 { + printf '%s\n' "$1" >&2 + unset PASSPHRASE + unset SIGN_PASSPHRASE + exit 1 +} + +function duplicity_backup_b2 { + + local_dir="$1" + b2_dir="$2" + + echo "Backing up home..." + duplicity $opts \ + --full-if-older-than 30D \ + "$local_dir" b2://"$b2_account":"$b2_key"@"$b2_dir" + + echo "Cleaning up old backups..." + duplicity $opts \ + remove-older-than 90D \ + b2://"$b2_account":"$b2_key"@"$b2_dir" + + echo "Cleaning up failures..." + duplicity $opts \ + cleanup --force \ + b2://"$b2_account":"$b2_key"@"$b2_dir" + + echo "B2 collection status:" + duplicity $opts \ + collection-status \ + b2://"$b2_account":"$b2_key"@"$b2_dir" +} + +function 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 \ + list-current-files \ + b2://"$b2_account":"$b2_key"@"$1" +} + +function 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 \ + --file-to-restore "$1" \ + b2://"$b2_account":"$b2_key"@idkfa-new-home "$2" +} + +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'Error: Unknown option specified' + ;; + *) # No more options + break + esac + shift +done + +export PASSPHRASE +export SIGN_PASSPHRASE + +if [ $# -lt 1 ]; then + die 'error: no COMMAND specified, see --help' +fi + +for command in "$@"; do + case "$command" in + backup) + backup_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 + +unset PASSPHRASE +unset SIGN_PASSPHRASE diff --git a/bin/backup-b2.sh b/bin/backup-b2.sh @@ -1,46 +0,0 @@ -#!/usr/bin/env bash - -b2_account="$(pass Online/backblaze.com-anders@adamsgaard.dk-account-id)" -b2_key="$(pass Online/backblaze.com-anders@adamsgaard.dk-master-application-key)" - -# from `gpg --list-public-keys anders@adamsgaard.dk` -enc_key="5C959DF243CE4DD17A5B2610B790F4AD1BF858FE" -sgn_key="$enc_key" - -export PASSPHRASE="$(pass gpg-anders@adamsgaard.dk)" -export SIGN_PASSPHRASE="$PASSPHRASE" - -opts="--progress --sign-key $sgn_key --encrypt-key $enc_key" - -function duplicity_b2 { - - local_dir="$1" - b2_dir="$2" - - echo "Backing up home..." - duplicity $opts \ - --full-if-older-than 30D \ - "$local_dir" b2://"$b2_account":"$b2_key"@"$b2_dir" - - echo "Cleaning up old backups..." - duplicity $opts \ - remove-older-than 90D \ - b2://"$b2_account":"$b2_key"@"$b2_dir" - - echo "Cleaning up failures..." - duplicity $opts \ - cleanup --force \ - b2://"$b2_account":"$b2_key"@"$b2_dir" - - echo "B2 collection status:" - duplicity $opts \ - collection-status \ - b2://"$b2_account":"$b2_key"@"$b2_dir" -} - -duplicity_b2 /home "$(hostname)-new-home" -duplicity_b2 /etc "$(hostname)-new-etc" -duplicity_b2 /usr "$(hostname)-new-usr" - -unset PASSPHRASE -unset SIGN_PASSPHRASE