~dricottone/my-utils

ref: 596d0a2530fc8b5bedf46eb7d9282766a2046ac2 my-utils/archives/archive.bash -rwxr-xr-x 7.4 KiB
596d0a25Dominic Ricottone Help and usage messages 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash

# See mktar-batch for simple usage

archive() {
  local tar_bin=/usr/bin/tar
  local gpg_bin=/usr/bin/gpg
  local age_bin=age

  local encrypt=""
  local encrypt_cmd=""
  local encrypt_flags=""
  local passphrase_flags=""
  local passphrase=""
  local archive_cmd=""
  local archive_flags=""
  local archive_late_flags=""
  local archive_fn=""
  local from_directory=""

  local positional=()

  while [[ $# -gt 0 ]]; do
    case $1 in

    --archive=tar)
      encrypt=0
      encrypt_cmd=""
      encrypt_flags=""
      passphrase_flags=""
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cf"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.age)
      encrypt=1
      encrypt_cmd="${age_bin}"
      encrypt_flags="${encrypt_flags} --encrypt --passphrase --output"
      passphrase_flags="--plaintext"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}c"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.gpg)
      encrypt=1
      encrypt_cmd="${gpg_bin}"
      encrypt_flags="${encrypt_flags} --symmetric --output"
      passphrase_flags="--batch --passphrase"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}c"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.gz)
      encrypt=0
      encrypt_cmd=""
      encrypt_flags=""
      passphrase_flags=""
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}czf"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.gz.age)
      encrypt=1
      encrypt_cmd="${age_bin}"
      encrypt_flags="${encrypt_flags} --encrypt --passphrase --output"
      passphrase_flags="--plaintext"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cz"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.gz.gpg)
      encrypt=1
      encrypt_cmd="${gpg_bin}"
      encrypt_flags="${encrypt_flags} --symmetric --output"
      passphrase_flags="--batch --passphrase"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cz"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.xz)
      encrypt=0
      encrypt_cmd=""
      encrypt_flags=""
      passphrase_flags=""
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cJf"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.xz.age)
      encrypt=1
      encrypt_cmd="${age_bin}"
      encrypt_flags="${encrypt_flags} --encrypt --passphrase --output"
      passphrase_flags="--plaintext"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cJ"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.xz.gpg)
      encrypt=1
      encrypt_cmd="${gpg_bin}"
      encrypt_flags="${encrypt_flags} --symmetric --output"
      passphrase_flags="--batch --passphrase"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cJ"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.zst)
      encrypt=0
      encrypt_cmd=""
      encrypt_flags=""
      passphrase_flags=""
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cf"
      archive_late_flags="--zstd"
      shift
      ;;

    --archive=tar.zst.age)
      encrypt=1
      encrypt_cmd="${age_bin}"
      encrypt_flags="${encrypt_flags} --encrypt --passphrase --output"
      passphrase_flags="--plaintext"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}c"
      archive_late_flags="--zstd"
      shift
      ;;

    --archive=tar.zst.gpg)
      encrypt=1
      encrypt_cmd="${gpg_bin}"
      encrypt_flags="${encrypt_flags} --symmetric --output"
      passphrase_flags="--batch --passphrase"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}c"
      archive_late_flags="--zstd"
      shift
      ;;

    --archive=tar.bz2)
      encrypt=0
      encrypt_cmd=""
      encrypt_flags=""
      passphrase_flags=""
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cjf"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.bz2.age)
      encrypt=1
      encrypt_cmd="${age_bin}"
      encrypt_flags="${encrypt_flags} --encrypt --passphrase --output"
      passphrase_flags="--plaintext"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cj"
      archive_late_flags=""
      shift
      ;;

    --archive=tar.bz2.gpg)
      encrypt=1
      encrypt_cmd="${gpg_bin}"
      encrypt_flags="${encrypt_flags} --symmetric --output"
      passphrase_flags="--batch --passphrase"
      archive_cmd="${tar_bin}"
      archive_flags="${archive_flags}cj"
      archive_late_flags=""
      shift
      ;;

    --archive=*)
      error_msg "unsupported archive type '${1}'"
      ;;

    --directory)
      from_directory="$2"
      shift; shift
      ;;

    --name)
      archive_fn="$(realpath "$2")"
      shift; shift
      ;;

    --passphrase)
      passphrase="$2"
      shift; shift
      ;;

    *)
      positional+=("$(realpath "$1")")
      shift
      ;;
    esac
  done

  # error if no input files given
  if [[ "${#positional[@]}" -lt 1 ]]; then
    error_msg "no input files given"
    usage_msg
  fi

  local targets=()
  for target in "${positional[@]}"; do
    # error if target file does not exist
    if [[ ! -f "$target" ]] && [[ ! -d "$target" ]]; then
      error_msg "no such file '${target}'"
    fi

    # make paths relative to from_directory
    if [[ ! -z "$from_directory" ]]; then
      targets+="$(realpath --relative-base="$from_directory" "$target")"
    else
      targets+="$(realpath --relative-base="$(pwd)" "$target")"
    fi
  done
  debug_msg "Input files have been resolved to relative paths"
  debug_msg "Was:${positional[@]}"
  debug_msg "Is now:${targets[@]}"

  # error if no archive file given
  if [[ -z "$archive_fn" ]]; then
    error_msg "no archive file given"
    usage_msg
  fi

  # error if archive exists and should not be overwritten
  if ! prompt_overwrite "$archive_fn"; then
    error_msg "archive '${archive_fn}' cannot be overwritten"
    usage_msg
  fi

  # error if archive is an input file
  if contains "$archive_fn" "${positional[@]}"; then
    error_msg "archive '${archive_fn}' cannot be an input file"
  fi

  # handle passphrase
  if [[ ! -z "$passphrase_flags" && ! -z "$passphrase" ]]; then
    passphrase_flags="${passphrase_flags} ${passphrase}"
  else
    passphrase_flags=
  fi

  # handle debugging information
  local verbosity=/dev/stderr
  if [[ "$verbose" -ne 1 ]]; then
    verbosity=/dev/null
  elif [[ "$encrypt" -eq 1 ]]; then
    debug_msg "cd ${from_directory:-.} && $archive_cmd $archive_flags $archive_late_flags ${targets[@]} 2>$verbosity | $encrypt_cmd $encrypt_flags $archive_fn $passphrase_flags 2>$verbosity"
  else
    debug_msg "cd ${from_directory:-.} && $archive_cmd $archive_flags $archive_fn $archive_late_flags ${targets[@]} 2>$verbosity"
  fi

  # archive routine
  if [[ ! -z "$from_directory" ]] && ! cd "$from_directory" >/dev/null 2> >(/usr/bin/sed -e "s/.*cd/cd/" >$verbosity); then
    error_msg "could not access directory '${from_directory}'"
    echo "ping"
  fi
  if [[ "$encrypt" -eq 1 ]]; then
    ( \
      $archive_cmd $archive_flags $archive_late_flags "${targets[@]}" 2>$verbosity \
        || error_msg "could not archive '${archive_fn}'" \
    ) \
    | ( \
      $encrypt_cmd $encrypt_flags "$archive_fn" $passphrase_flags 2>$verbosity \
        || error_msg "could not encrypt '${archive_fn}'" \
    )
  else
    $archive_cmd $archive_flags "$archive_fn" $archive_late_flags "${targets[@]}" 2>$verbosity \
      || error_msg "could not archive '${archive_fn}'"
  fi
}