#!/bin/bash
CACHE_DIR=~/.cache/backup-legal
LEGAL_DIR=/hdd/documents/legal
TEMP_DIR=~
if [[ $# -eq 0 ]]; then
/usr/bin/printf "USAGE: backup legal [OPTIONS]\n"
exit 1
fi
source /usr/local/lib/mylib.bash
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
/usr/bin/printf "backup legal [OPTIONS]\n"
/usr/bin/printf "Options:\n"
/usr/bin/printf " -h, --help Show help text\n"
/usr/bin/printf " -p, --push Push new documents to backups\n"
/usr/bin/printf " -u, --update Update cache of backups\n"
exit 0
;;
-u|--update)
/usr/bin/printf "Updating local cache of legal documents on backup server...\n"
/usr/bin/mkdir -p "$CACHE_DIR"
cd "$CACHE_DIR"
/usr/bin/rm *
~/.local/bin/b2 ls --recursive dominic-docs legal \
| /usr/bin/grep -e '\.tar\.zstd\.age$' \
| /usr/bin/sed -e 's/\.tar\.zstd\.age//' -e 's/^legal\///' -e 's/\//-/' \
| /usr/bin/uniq \
| /usr/bin/xargs /usr/bin/touch
exit 0
;;
-p|--push)
/usr/bin/printf "Pushing new legal documents to the backup server...\n"
/usr/bin/mkdir -p "$CACHE_DIR"
skipped=0
/usr/bin/printf "Enter a passphrase: "
read -s passphrase
/usr/bin/printf "\n"
for doc_name in $(/usr/bin/find "${LEGAL_DIR}" -mindepth 1 -maxdepth 1); do
doc_name="$(/usr/bin/basename "$doc_name")"
base_name="$(fn_basename "$doc_name")"
if [ -f "$CACHE_DIR/${base_name}" ]; then
#/usr/bin/printf "Skipping %s...\n" "$base_name"
skipped=$((skipped+1))
else
#/usr/bin/printf "Pushing %s...\n" "$base_name"
cd "${LEGAL_DIR}" \
&& /usr/local/bin/mktar-batch --compress=zstd --checksum=sha256 --encrypt=age --passphrase "$passphrase" --name ${TEMP_DIR}/${base_name}.tar.zstd.age ${doc_name} \
&& ~/.local/bin/b2 upload-file dominic-docs ${TEMP_DIR}/${base_name}.tar.zstd.age legal/${base_name}.tar.zstd.age >/dev/null \
&& ~/.local/bin/b2 upload-file dominic-docs ${TEMP_DIR}/${base_name}.sha256 legal/${base_name}.sha256 >/dev/null \
&& /usr/bin/touch "${CACHE_DIR}/${base_name}"
/usr/bin/rm --force ${TEMP_DIR}/${base_name}.tar.zstd.age
/usr/bin/rm --force ${TEMP_DIR}/${base_name}.sha256
fi
done
if [[ $skipped -ne 0 ]]; then
/usr/bin/printf "Skipped %s documents\n" "$skipped"
fi
exit 0
;;
*)
/usr/bin/printf "Unknown option '%s'\n" "$1"
exit 1
;;
esac
done