-
Notifications
You must be signed in to change notification settings - Fork 2
/
archive
executable file
·297 lines (263 loc) · 7.2 KB
/
archive
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
295
296
297
#!/bin/bash
# SPDX-FileCopyrightText: 2017 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command sed && exit 3
#
# Functions
#
# Returns a string containing all the files and directories passed in the
# command line. Paths prefixed with '~/' are converted to '${HOME}'. Symlinks
# are expanded and included as if they were in the current working directory.
function make_pathstring() {
local pathstring=""
local dir
local link
local item
local target
for item in "${@}"; do
if [ -e "${item}" ]; then
dir="$(pwd)"
target="${item}"
if [ "${item:0:2}" = "\~/" ]; then
target="${HOME}/${item:2}"
dir="$(dirname "${target}")"
target="$(basename "${target}")"
fi
if [ -L "${item}" ]; then
link="$(readlink -f "${item}")"
dir="$(dirname "${link}")"
target="$(basename "${link}")"
fi
if [[ "${item}" =~ ^/ ]]; then
dir="$(dirname "${item}")"
target="$(basename "${item}")"
fi
pathstring="${pathstring} -C ${dir@Q} ${target@Q}"
else
show_error "ERROR: ${item@Q} does not exist. Exiting."
exit 2
fi
done
echo "${pathstring}"
}
# Generate a name for the archive. For single directories, the name will be
# <dirname>_<timestamp>. For multiple files and directories, the name will be
# the basename of the directory from which the script is called. For single
# files, the name sill be <basename>_<timestamp>.
function make_archivename() {
local archive
local dir
local file
if [ ${#} -eq 1 ]; then
if [ -d "${1}" ]; then
dir=${1}
if [ "${dir:0:2}" = "\~/" ]; then
dir="${HOME}/${dir:2}"
fi
archive="$(basename "${dir}" | slugify)_$(date +%Y%m%d-%H%M%S)"
elif [ -f "${1}" ]; then
file=${1}
if [ "${file:0:2}" = "\~/" ]; then
file="${HOME}/${file:2}"
fi
archive="$(echo "${file%.*}" | slugify)_$(date +%Y%m%d-%H%M%S)"
fi
elif [ ${#} -gt 1 ]; then
archive="$(basename "$(pwd)" | slugify)_$(date +%Y%m%d-%H%M%S)"
fi
echo "${archive}"
}
# Take the (1) archive name and the (2) pathstring and compress into a tarball
# to be found in ${ARCHIVEDIR} (see globals). Uses pigz (parallelized gzip
# compression) if available.
function compress_archive() {
local exclude_dir
local exclude_str="--exclude='*sw[a-p]' --exclude='*~'"
for exclude_dir in "${EXCLUDEDIRS[@]}"; do
exclude_str="${exclude_str} --exclude=${exclude_dir@Q}"
done
mkdir -p "${ARCHIVEDIR}"
case "${Z}" in
7z)
if check_command 7z tar; then
show_warning "WARNING: 7z does not support uid/gid metadata, so using tar first."
eval "tar cf - ${exclude_str} ${2}" |
7z a -si "${ARCHIVEDIR}/${1}.tar.7z"
fi
;;
gz)
! check_command tar && return
if command -v pigz > /dev/null 2>&1; then
eval "tar cf - ${exclude_str} ${2}" |
pigz > "${ARCHIVEDIR}/${1}.tar.gz"
else
eval "tar czf ${ARCHIVEDIR}/${1}.tar.gz ${exclude_str} ${2}"
fi
;;
xz)
! check_command tar && return
if command -v pixz > /dev/null 2>&1; then
eval "tar cf - ${exclude_str} ${2}" |
pixz > "${ARCHIVEDIR}/${1}.tar.xz"
else
eval "tar czf ${ARCHIVEDIR}/${1}.tar.xz ${exclude_str} ${2}"
fi
;;
zip)
if check_command zip; then
local tmp=()
local delete=()
local item
local name
for item in "${ITEMS[@]}"; do
if [[ "${item}" =~ ^/ ]]; then
name="$(basename "${item}")"
ln -s "${item}" "${name}"
tmp+=("${name}")
delete+=("${name}")
else
tmp+=("${item}")
fi
done
eval "zip -q -r ${ARCHIVEDIR}/${1}.zip ${tmp[*]} ${exclude_str}"
if [ ${#delete[@]} -gt 0 ]; then
rm "${delete[@]}"
fi
fi
;;
zstd)
if check_command pzstd tar; then
eval "tar cf - ${exclude_str} ${2}" |
pzstd > "${ARCHIVEDIR}/${1}.tar.zst"
fi
;;
*)
show_error "?"
exit 3
;;
esac
}
# Check that the archive destination directory is valid. The order of
# preference is:
# - 1. the directory specified by the -d flag
# - 2. the archivedir global variable
# - 3. ${HOME}/Desktop
function check_archive_dir() {
if ! [[ -v ARCHIVEDIR ]]; then
ARCHIVEDIR="${archivedir:-${HOME}/Desktop}"
fi
if ! [ -d "${ARCHIVEDIR}" ]; then
show_error "ERROR: Destination ${ARCHIVEDIR@Q} does not exist. Exiting."
exit 3
fi
}
# Convert an input string to a lowercase, slugified version.
function slugify() {
tr "[:upper:]" "[:lower:]" | sed -e "s/^\.//g" -e "s/\s/_/g"
}
function print_usage() {
show_header "Usage: archive"
cat << EOF
-d|--dir (optional) destination directory
-z|--compress (optional) compression algorithm (xz, zstd, gz, or zip)
Example: archive <item1> <path/to/item2> ...
The output file name is derived from the common root directory for the
compressed contents and the timestamp of when command was run.
EOF
}
#
# Main
#
if [ ${#} == 0 ]; then
show_error "Incorrect # of parameters."
print_usage
exit 1
fi
Z=zstd
OPTIONS=d:e:z:h
LONGOPTIONS=dir:exclude_dir:compress:,help
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-d | --dir)
ARCHIVEDIR="${2}"
shift 2
;;
-e | --exclude_dir)
EXCLUDEDIRS+=("${2}")
shift 2
;;
-z | --compress)
case "${2,,}" in
7z | p7zip)
Z=7z
;;
zst | zstd | pzstd)
Z=zstd
;;
gz | pigz)
Z=gz
;;
xz | pixz)
Z=xz
;;
zip)
Z=zip
;;
*)
show_error "ERROR: Compression ${2@Q} not supported. Exiting."
exit 3
;;
esac
shift 2
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "ERROR: Invalid flag ${1@Q}. Exiting."
exit 3
;;
esac
done
check_archive_dir
if [ "${1}" = "--" ]; then
ITEMS=("${@:2}")
else
ITEMS=("${@}")
fi
PATHSTRING="$(make_pathstring "${ITEMS[@]}")"
ARCHIVE="$(make_archivename "${ITEMS[@]}")"
if [ -z "${PATHSTRING}" ]; then
show_error "Path string is empty. Exiting."
exit 1
fi
if [ -z "${ARCHIVE}" ]; then
show_error "Archive name is empty. Exiting."
exit 1
fi
compress_archive "${ARCHIVE}" "${PATHSTRING}"
sync