#!/bin/bash
CACHE_DIR=~/.cache/backup-music
MUSIC_DIR=/hdd/media/music
TEMP_DIR=~
if [[ $# -eq 0 ]]; then
/usr/bin/printf "USAGE: backup music [OPTIONS]\n"
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
/usr/bin/printf "backup music [OPTIONS]\n"
/usr/bin/printf "Options:\n"
/usr/bin/printf " -h, --help Show help text\n"
/usr/bin/printf " -p, --push Push new music to backups\n"
/usr/bin/printf " -u, --update Update cache of backups\n"
exit 0
;;
-u|--update)
/usr/bin/printf "Updating local cache of music on backup server...\n"
/usr/bin/mkdir -p "$CACHE_DIR"
cd "$CACHE_DIR"
/usr/bin/rm *
~/.local/bin/b2 ls --recursive dominic-media music \
| /usr/bin/grep -e '\.tar\.gz\.gpg$' \
| /usr/bin/sed -e 's/\.tar\.gz\.gpg//' -e 's/^music\///' -e 's/\//-/' \
| /usr/bin/uniq \
| /usr/bin/xargs /usr/bin/touch
exit 0
;;
-p|--push)
/usr/bin/printf "Pushing new music 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 artist in $(/usr/bin/find "${MUSIC_DIR}" -mindepth 1 -maxdepth 1 -type d); do
artist=$(/usr/bin/basename "$artist")
for album in $(/usr/bin/find "${MUSIC_DIR}/${artist}" -mindepth 1 -maxdepth 1 -type d); do
album=$(/usr/bin/basename "$album")
name="${artist}-${album}"
if [ -f "$CACHE_DIR/${name}" ]; then
#/usr/bin/printf "Skipping %s...\n" "$name"
skipped=$((skipped+1))
else
#/usr/bin/printf "Pushing %s...\n" "$name"
cd "${MUSIC_DIR}" \
&& /usr/local/bin/mktar-batch --compress=gzip --checksum=sha1 --encrypt=gpg --passphrase "$passphrase" --name ${TEMP_DIR}/${name}.tar.gz.gpg ${artist}/${album} \
&& ~/.local/bin/b2 upload-file dominic-media ${TEMP_DIR}/${name}.tar.gz.gpg music/${artist}/${album}.tar.gz.gpg >/dev/null \
&& ~/.local/bin/b2 upload-file dominic-media ${TEMP_DIR}/${name}.sha1 music/${artist}/${album}.sha1 >/dev/null \
&& /usr/bin/touch "${CACHE_DIR}/${name}"
/usr/bin/rm --force ${TEMP_DIR}/${name}.tar.gz.gpg
/usr/bin/rm --force ${TEMP_DIR}/${name}.sha1
fi
done
done
if [[ $skipped -ne 0 ]]; then
/usr/bin/printf "Skipped %s albums\n" "$skipped"
fi
exit 0
;;
*)
/usr/bin/printf "Unknown option '%s'\n" "$1"
exit 1
;;
esac
done