dotfiles

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

backup-b2 (4320B)


      1 #!/bin/sh
      2 set -e
      3 
      4 version=1.1.1
      5 
      6 b2_account="$(pass Online/backblaze.com-anders@adamsgaard.dk-account-id)"
      7 b2_key="$(pass Online/backblaze.com-anders@adamsgaard.dk-master-application-key)"
      8 
      9 # from `gpg --list-public-keys anders@adamsgaard.dk`
     10 enc_key="5C959DF243CE4DD17A5B2610B790F4AD1BF858FE"
     11 sgn_key="$enc_key"
     12 
     13 PASSPHRASE="$(pass gpg-anders@adamsgaard.dk)"
     14 SIGN_PASSPHRASE="$PASSPHRASE"
     15 
     16 duplicity_with_opts() {
     17 	duplicity --progress --sign-key $sgn_key --encrypt-key $enc_key "$@"
     18 }
     19 
     20 show_help() {
     21 	echo "usage: ${0##*/} [OPTIONS] [COMMAND[S]]"
     22 	echo "is a wrapper for the duplicity command for communicating with an"
     23 	echo "encrypted backup in backblaze B2 storage."
     24 	echo "COMMAND can be one of the following:"
     25 	echo "   backup              Backup /home, /etc, and /user, and clean out"
     26 	echo "                       old backups and failures"
     27 	echo "   list                List all files in latest backup set"
     28 	echo "   status              Show status of all backups"
     29 	echo "   restore SRC DST     Restore SRC file or dir from backup to local"
     30 	echo "                       DST. The SRC must be the path listed as the"
     31 	echo "                       last column from the 'list' command"
     32 	echo
     33 	echo "OPTIONS are one or more of the following:"
     34 	echo "   -h, --help          show this message"
     35 	echo "   -v, --version       show version and license information"
     36 	echo "   -V, --verbose       show verbose information"
     37 	echo "   --                  do not consider any following args as options"
     38 }
     39 
     40 show_version() {
     41 	echo "${0##*/} version $version"
     42 	echo "Licensed under the GNU Public License, v3+"
     43 	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
     44 	echo "https://gitlab.com/admesg/dotfiles"
     45 }
     46 
     47 die() {
     48 	printf '%s\n' "$1" >&2
     49 	unset PASSPHRASE
     50 	unset SIGN_PASSPHRASE
     51 	exit 1
     52 }
     53 
     54 duplicity_backup_b2() {
     55 	local_dir="$1"
     56 	b2_dir="$2"
     57 
     58 	echo "Backing up $1..."
     59 	if [ "$local_dir" = "/home" ]; then
     60 		duplicity_with_opts \
     61 		    --full-if-older-than 30D \
     62 		    --exclude ~/tmp --exclude ~/.cache --exclude ~/videos/tmp \
     63 		    "$local_dir" b2://"$b2_account":"$b2_key"@"$b2_dir"
     64 	else
     65 		duplicity_with_opts \
     66 		    --full-if-older-than 30D \
     67 		    "$local_dir" b2://"$b2_account":"$b2_key"@"$b2_dir"
     68 	fi
     69 
     70 	echo "Cleaning up old backups..."
     71 	duplicity_with_opts \
     72 	    remove-older-than 90D \
     73 	    b2://"$b2_account":"$b2_key"@"$b2_dir"
     74 
     75 	echo "Cleaning up failures..."
     76 	duplicity_with_opts \
     77 	    cleanup --force \
     78 	    b2://"$b2_account":"$b2_key"@"$b2_dir"
     79 
     80 	echo "B2 collection status:"
     81 	duplicity_with_opts \
     82 	    collection-status \
     83 	    b2://"$b2_account":"$b2_key"@"$b2_dir"
     84 }
     85 
     86 status_b2() {
     87 	printf "\n## %s\n" "$1"
     88 	duplicity_with_opts \
     89 	    collection-status \
     90 	    b2://"$b2_account":"$b2_key"@"$1"
     91 }
     92 
     93 list_status_b2() {
     94 	status_b2 "$(hostname)-new-home"
     95 	status_b2 "$(hostname)-new-etc"
     96 	status_b2 "$(hostname)-new-usr"
     97 }
     98 
     99 backup_b2() {
    100 	duplicity_backup_b2 /home "$(hostname)-new-home"
    101 	duplicity_backup_b2 /etc "$(hostname)-new-etc"
    102 	# duplicity_backup_b2 /usr "$(hostname)-new-usr"
    103 }
    104 
    105 duplicity_list_backup_b2() {
    106 	duplicity_with_opts \
    107 	    list-current-files \
    108 	    b2://"$b2_account":"$b2_key"@"$1"
    109 }
    110 
    111 list_backups_b2() {
    112 	duplicity_list_backup_b2 "$(hostname)-new-home"
    113 	duplicity_list_backup_b2 "$(hostname)-new-etc"
    114 	duplicity_list_backup_b2 "$(hostname)-new-usr"
    115 }
    116 
    117 restore_from_backup_b2() {
    118 	[ -n "$1" ] || die 'error: supply a <source> and <destination>'
    119 	[ -n "$2" ] || die 'error: missing <destination>'
    120 	case "$1" in
    121 		ad*)
    122 			archive=idkfa-new-home
    123 			;;
    124 		etc*)
    125 			archive=idkfa-new-etc
    126 			;;
    127 		usr*)
    128 			archive=idkfa-new-usr
    129 			;;
    130 	esac
    131 
    132 	duplicity_with_opts \
    133 		--file-to-restore "$1" \
    134 		b2://"$b2_account":"$b2_key"@"$archive" "$2"
    135 }
    136 
    137 while :; do
    138 	case "$1" in
    139 		-h|-\?|--help)
    140 			show_help
    141 			exit 0
    142 			;;
    143 		-v|--version)
    144 			show_version
    145 			exit 0
    146 			;;
    147 		--) # end all options
    148 			shift
    149 			break
    150 			;;
    151 		-?*)
    152 			die 'Error: Unknown option specified'
    153 			;;
    154 		*)  # No more options
    155 			break
    156 	esac
    157 	shift
    158 done
    159 
    160 export PASSPHRASE
    161 export SIGN_PASSPHRASE
    162 
    163 if [ $# -lt 1 ]; then
    164 	die 'error: no COMMAND specified, see --help'
    165 fi
    166 
    167 case "$1" in
    168 	backup)
    169 		backup_b2
    170 		;;
    171 	status)
    172 		list_status_b2
    173 		;;
    174 	list)
    175 		list_backups_b2
    176 		;;
    177 	restore)
    178 		restore_from_backup_b2 "$2" "$3"
    179 		;;
    180 	*)
    181 		die 'error: invalid COMMAND specified, see --help'
    182 esac
    183 
    184 unset PASSPHRASE
    185 unset SIGN_PASSPHRASE