~dricottone/secret-utils

ref: 9585733cade94d25ea50cc308317fd9dca2fd3fe secret-utils/backup-legal -rwxr-xr-x 2.4 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
#!/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