#!/bin/bash
TEMP_DIR=~
if [[ $# -eq 0 ]]; then
/usr/bin/printf "USAGE: backup files --bucket=BUCKET --name=remote/archive/name [OPTIONS] FILES\n"
exit 1
fi
source /usr/local/lib/mylib.bash
bucket=""
remote_archive_name=""
remote_checksum_name=""
local_archive_name=""
local_checksum_name=""
positional=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
/usr/bin/printf "backup files [OPTIONS]\n"
/usr/bin/printf "Options:\n"
/usr/bin/printf " -h, --help Show help text\n"
/usr/bin/printf " -b=BUCKET, --bucket=BUCKET Push files to remote bucket [docs|media|work]\n"
/usr/bin/printf " -n NAME, --name NAME Name of archive\n"
exit 0
;;
-b=docs|--bucket=docs)
bucket="dominic-docs"
shift
;;
-b=media|--bucket=media)
bucket="dominic-media"
shift
;;
-b=work|--bucket=work)
bucket="dominic-work"
shift
;;
-b=*|--bucket=*)
/usr/bin/printf "Invalid bucket '%s'\n" "$1";
exit 1
;;
-n|--name)
base_name="$(fn_basename "$2")"
safe_name="$(fn_basename "$2" | sed -e 's/\//_/g')"
remote_archive_name="${base_name}.tar.zstd"
remote_checksum_name="${base_name}.sha256"
local_archive_name=${TEMP_DIR}/"${safe_name}.tar.zstd"
local_checksum_name=${TEMP_DIR}/"${safe_name}.sha256"
shift; shift
;;
*)
positional+=("$1")
shift
;;
esac
done
# error if no filenames given, or if no bucket given, or if no remote name given
if [[ "${#positional[@]}" -lt 1 ]]; then
/usr/bin/printf "No input filenames given\n"
exit 1
elif [[ -z "$bucket" ]]; then
/usr/bin/printf "No remote bucket given\n"
exit 1
elif [[ -z "$remote_archive_name" ]]; then
/usr/bin/printf "No remote filename given\n"
exit 1
fi
# main routine
/usr/local/bin/mktar --compress=zstd --checksum=sha256 --name "$local_archive_name" "${positional[@]}" \
&& ~/.local/bin/b2 upload-file "$bucket" "$local_archive_name" "$remote_archive_name" >/dev/null\
&& ~/.local/bin/b2 upload-file "$bucket" "$local_checksum_name" "$remote_checksum_name" >/dev/null\
&& /usr/bin/rm --force "$local_archive_name" \
&& /usr/bin/rm --force "$local_checksum_name"