~dricottone/secret-utils

ref: 9585733cade94d25ea50cc308317fd9dca2fd3fe secret-utils/backup-files -rwxr-xr-x 2.1 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

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"