-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress_cp
More file actions
executable file
·177 lines (156 loc) · 5.77 KB
/
compress_cp
File metadata and controls
executable file
·177 lines (156 loc) · 5.77 KB
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
#!/usr/bin/env bash
#
# Description: Copy a directory or file to a specified location,
# compressing it in the process.
#
# Homepage: https://github.com/krathalan/miscellaneous-scripts
#
# Copyright (C) 2020-2024 Hunter Peavey
#
# 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/>.
# -----------------------------------------
# -------------- Guidelines ---------------
# -----------------------------------------
# This script follows the Google Shell Style Guide:
# https://google.github.io/styleguide/shell.xml
# This script uses shellcheck: https://www.shellcheck.net/
# See https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
# -----------------------------------------
# ----------- Program variables -----------
# -----------------------------------------
# Colors
GREEN=$(tput bold && tput setaf 2)
RED=$(tput bold && tput setaf 1)
BLUE=$(tput sgr0 && tput setaf 4)
NC=$(tput sgr0) # No color/turn off all tput attributes
readonly GREEN RED BLUE NC
# -----------------------------------------
# --------------- Functions ---------------
# -----------------------------------------
# Used to set default commands to others/enable other functionality
check_command() {
if [[ -n "$(command -v "$1")" ]]; then
return 0
else
return 1
fi
}
#######################################
# Prints passed error message before premature exit.
# Prints everything to >&2 (STDERR).
# Globals:
# RED, NC
# SCRIPT_NAME
# Arguments:
# $1: error message to print
# Returns:
# none
#######################################
exit_script_on_failure()
{
printf "%sError%s: %s\n" "${RED}" "${NC}" "$1" >&2
exit 1
}
# -----------------------------------------
# ---------------- Script -----------------
# -----------------------------------------
if [[ $# -lt 2 ]]; then
exit_script_on_failure "Please specify (1) a directory to compress and (2) the target directory."
fi
# Get full paths
directoryToCompress="$1"
targetDirectory="$2"
directoryToCompress="$(readlink -f "${directoryToCompress}")"
targetDirectory="$(readlink -f "${targetDirectory}")"
printf "\nSpecify a compression program and level (e.g. \'pzstd -19\'),\nor leave blank to choose a preset compression program next.\n\n"
read -r -p " > " response
compression_program="${response}"
base_program="${compression_program%% *}"
if [[ -n "${base_program}" ]]; then
case "${base_program}" in
lz4|xz)
compression_file_ext="${base_program}"
;;
pigz|gzip)
compression_file_ext="gz"
;;
zstd|pzstd)
compression_file_ext="zst"
;;
*)
exit_script_on_failure "Unknown compression program, can't guess file extension. Submit an issue on Github please!"
esac
fi
if [[ -z "${response}" ]]; then
printf "\nWhat kind of compression? Will default to multithreaded compression\nif available. Leave blank to choose (p)zstd -10 (default).\n"
printf "\n [1] zstd -10 (max -19) (default)"
printf "\n [2] lz4 -9 (max -12)"
printf "\n [3] xz -T 0 -6 (max -9)"
printf "\n [4] gzip -6 (max -9)\n\n"
read -r -p " > " response
case "${response}" in
2)
compression_program="lz4 -9"
compression_file_ext="lz4"
;;
3)
compression_program="xz -T 0 -6"
compression_file_ext="xz"
;;
4)
if check_command "pigz"; then
compression_program="pigz -6"
else
compression_program="gzip -6"
fi
compression_file_ext="gz"
;;
*)
# Default
if check_command "pzstd"; then
compression_program="pzstd -10"
else
compression_program="zstd -10"
fi
compression_file_ext="zst"
;;
esac
fi
# Target output file name
targetOutput="${directoryToCompress##*/}.tar.${compression_file_ext}"
if [[ "${GPG_KEY_ID:-}" == "" ]]; then
response=n
else
printf "\nSign and encrypt with key %s? " "${GPG_KEY_ID}"
read -r -p "[y/N] " response
fi
start_time="$(date +%s)"
case "${response}" in
[yY][eE][sS]|[yY])
targetOutput="${targetOutput}.gpg"
printf "\nRunning command:\n%s\n\n" "tar -I \"${compression_program}\" -cf - \"${directoryToCompress}\" | gpg2 -z 0 --default-key ${GPG_KEY_ID} --recipient ${GPG_KEY_ID} --sign --encrypt > \"${targetDirectory}/${targetOutput}\""
tar -I "${compression_program}" -cf - "${directoryToCompress}" | gpg2 -z 0 --default-key "${GPG_KEY_ID}" --recipient "${GPG_KEY_ID}" --sign --encrypt > "${targetDirectory}/${targetOutput}"
;;
*)
printf "\nRunning command:\n%s\n\n" "tar -I \"${compression_program}\" -cf \"${targetDirectory}/${targetOutput}\" \"${directoryToCompress}\""
tar -I "${compression_program}" -cf "${targetDirectory}/${targetOutput}" "${directoryToCompress}"
;;
esac
end_time="$(date +%s)"
printf "\nOutput compressed archive %s%s/%s%s%s" "${BLUE}" "${targetDirectory}" "${GREEN}" "${targetOutput}" "${NC}"
# Get the size of the directoryToCompress and the targetOutput file and divide them to get a compression ratio,
# rounded to the nearest hundredth, e.g. 0.91
printf "\nCompression ratio: %.2f" "$(echo "$(du -sc "${targetDirectory}/${targetOutput}" | tail -n1 | awk '{printf $1}') / $(du -sc "${directoryToCompress}" | tail -n1 | awk '{printf $1}')" | bc -l)"
printf "\nTime to compress: %s seconds\n" "$(( end_time - start_time ))"