-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgz.sh
executable file
·130 lines (116 loc) · 2.99 KB
/
tgz.sh
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
#!/bin/zsh
# /bin/bash #if on server ;)
# Michael J. Foster
# github.com/mjfos2r
function usage() {
echo "Usage: $0 [-h] [-v | -d] -i <input> [-o <output>]"
echo " "
echo "This is a simple helper script to easily create tarballs and their checksum "
echo " "
echo "Options: "
echo " -h Print this help message and exit cleanly."
echo " -v Run tar with verbose output to STDOUT."
echo " -d Create dialog box to monitor progress."
echo " -i <input> The directory or file to compress"
echo " -o <output> Name of tarball (Optional: Defaults to input basename)"
echo " "
}
function with_dialog() {
local input=$1
local output=$2
#local size=$3
if ! (tar -cf - "$input" 2>/dev/null \
| pv -n \
| pigz -6 -p $(nproc) > "$output") 2>&1 \
| dialog --gauge 'Progress' 7 50; then
echo "Error during compression" >&2
rm -f "$output" # Clean up partial file
return 1
fi
return 0
}
function without_dialog() {
local input=$1
local output=$2
local opts=$3
echo "Compressing $input to $output"
if ! tar "$opts" - "$input" | pigz -6 -p $(nproc) > "$output"; then
echo "Error during compression" >&2
rm -f "$output" # Clean up partial file
return 1
fi
return 0
}
# setup vars
INPUT_OBJ=""
TARBALL=""
SIZE=0
VERBOSE=false
DIALOG=false
OPTS="-cf"
# parse args
while getopts "hi:o:v:d" opt; do
case $opt in
h)
usage
exit 0
;;
i)
INPUT_OBJ="$OPTARG"
;;
o)
TARBALL="${OPTARG}.tar.gz"
;;
v)
VERBOSE=true
;;
d)
DIALOG=true
;;
\?)
echo "ERROR: Invalid option -$OPTARG" >&2
exit 2
;;
:)
echo "ERROR: Option -$OPTARG requires an argument" >&2
exit 2
;;
esac
done
# check args
if [[ -z $INPUT_OBJ ]]; then
echo "ERROR: You must specify an input (-i). check your command and try again." >&2
exit 2
fi
# input's gotta exist
if [[ ! -f "$INPUT_OBJ" && ! -d "$INPUT_OBJ" ]]; then
echo "ERROR: $INPUT_OBJ is neither a file nor directory" >&2
exit 2
fi
# If no output_name, use basename.
if [[ -z "$TARBALL" ]]; then
TARBALL="$(basename "$INPUT_OBJ").tar.gz"
fi
if $VERBOSE; then
echo "Running Tar with full verbosity!"
OPTS="-cvf"
fi
if $DIALOG; then
OPTS="-cf"
SIZE=$(du -sb $INPUT_OBJ | awk '{print $1}')
fi
if $DIALOG; then
with_dialog "$INPUT_OBJ" "$TARBALL" "$SIZE"
else
without_dialog "$INPUT_OBJ" "$TARBALL" "$OPTS"
fi
# Only create checksum if compression succeeded
if [ -f "$TARBALL" ]; then
echo "Getting checksum!"
md5sum "$TARBALL" > "${TARBALL}.md5"
echo "Done!"
exit 0
else
echo "ERROR: Compression failed" >&2
exit 1
fi