~dricottone/my-utils

ref: 6223cfd72dae3990fe55996d4af5b5e345830af6 my-utils/archives/archive.bash -rw-r--r-- 6.7 KiB
6223cfd7Dominic Ricottone Adding note about bats to README 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
#!/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 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}'"
      ;;

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

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

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

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

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

  # 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 "$archive_cmd $archive_flags $archive_late_flags ${positional[@]} 2>$verbosity | $encrypt_cmd $encrypt_flags $archive_fn $passphrase_flags 2>$verbosity"
  else
    debug_msg "$archive_cmd $archive_flags $archive_fn $archive_late_flags ${positional[@]} 2>$verbosity"
  fi

  # archive routine
  if [[ "$encrypt" -eq 1 ]]; then
    ( \
      $archive_cmd $archive_flags $archive_late_flags "${positional[@]}" 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 "${positional[@]}" 2>$verbosity \
      || error_msg "could not archive '${archive_fn}'"
  fi
}