~dricottone/secret-utils

secret-utils/backup-music -rwxr-xr-x 2.5 KiB
9585733cDominic Ricottone Initial commit 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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