forked from broadinstitute/imaging-backup-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3md5
executable file
·303 lines (260 loc) · 6.56 KB
/
s3md5
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
298
299
300
301
302
303
#!/bin/bash
# s3md5 Copyright (C) 2013
# Antonio Espinosa <aespinosa at teachnova dot com>
#
# This file is part of s3md5 by Teachnova (www.teachnova.com)
#
# s3md5 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.
#
# s3md5 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 s3md5. If not, see <http://www.gnu.org/licenses/>.
VERSION='1.2'
DEBUG=0
##################################################################
# s3md5 paths
root_path() {
SOURCE="${BASH_SOURCE[0]}"
DIR="$( dirname "$SOURCE" )"
while [ -h "$SOURCE" ]
do
SOURCE="$( readlink "$SOURCE" )"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo "$DIR"
}
is_integer() {
local min="$2"
local max="$3"
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
return 1
fi
if [ -n "$2" ] && [ $2 -gt $1 ]; then return 1; fi
if [ -n "$3" ] && [ $3 -lt $1 ]; then return 1; fi
return 0
}
license_show() {
cat << LICENSE
s3md5 v$VERSION
Copyright (C) 2013 Antonio Espinosa <aespinosa@teachnova.com> - TeachNova
This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it GPLv3 license conditions. Read LICENSE.md for more details.
LICENSE
}
short_help() {
option=$1
if [[ -n "$option" ]]; then
cat <<EOL
ERROR : Invalid argument: '$option'
EOL
fi
cat << HELP
Usage :
# Get the S3 etag of a file
$APP <chunk size in MB> <file> [-v|--verbose]
# Verify the S3 etag of a file
$APP -e|--etag <etag> <chunk size in MB> <file> [-v|--verbose]
# Short help
$APP -h
# Extended help
$APP --help
Example :
# Use 15 MB chunk size (as default in s3cmd .s3cfg config file)
# and get the etag of "myFile"
$APP 15 myFile
# Given the S3 etag, verify if it matches "myFile"
$APP --etag e3ad981c2963eb533e3e58175cf1c966-8 64 myFile
HELP
}
extended_help() {
license_show
cat << HELP
Calculates the Etag/S3 MD5 Sum of a file, using the same algorithm that S3 uses on multipart uploaded files.
Specially usefull on files bigger than 5GB uploaded using multipart S3 API.
HELP
short_help
}
ARGS=()
function main() {
if [ $# -gt 0 ]; then
case "$1" in
-h)
short_help
exit 1
;;
--help)
extended_help
exit 1
;;
-e|--etag)
shift
ETAG=$1
shift
main "$@"
;;
-H|--show-filename)
shift
SHOW_FILENAME=1
main "$@"
;;
-v|--verbose)
shift
DEBUG=1
main "$@"
;;
-*)
short_help $1
exit 1
;;
*)
ARGS+=("$1")
shift
main "$@"
;;
esac
else
run "${ARGS[@]}"
fi
}
cleanup() {
$RM_BIN "$ERROR_FILE"
$RM_BIN "$SUM_FILE"
$RM_BIN "$BIN_FILE"
}
if [ "$(uname)" == "Darwin" ]; then
IS_OSX=1
else
IS_OSX=0
fi
ROOT_PATH=`root_path`
APP=`basename $0`
ECHO_BIN=$(which echo)
RM_BIN="$(which rm) -f"
DD_BIN=$(which dd)
if [ "$IS_OSX" -eq 0 ]; then
MD5_BIN=$(which md5sum)
else
MD5_BIN=$(which md5)
fi
CUT_BIN=$(which cut)
GREP_BIN=$(which grep)
CAT_BIN=$(which cat)
AWK_BIN=$(which awk)
XXD_BIN=$(which xxd)
get_chunks_from_etag() {
$ECHO_BIN "$1" | cut -d'-' -f 2
}
get_filesize_in_bytes() {
du -m "$1" | cut -f 1
}
get_chunk_size() {
local filesize=$1
local chunks=$2
local min_chunk_size=`$ECHO_BIN "$filesize / $chunks" | bc`
local max_chunk_size=`$ECHO_BIN "$filesize / ( $chunks - 1 )" | bc`
$ECHO_BIN "With $chunks chunks, the possible chunk sizes are between $min_chunk_size MB and $max_chunk_size MB"
}
function run() {
# Check parameters
if [ $# -lt 2 ]; then short_help; exit 1; fi
SIZE="$1"
FILE="$2"
local FILESIZE=`get_filesize_in_bytes "$FILE"`
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN "File size: $FILESIZE MB"
fi
if [ -n "$ETAG" ]; then
if [ $DEBUG -eq 1 ]; then
local CHUNKS=`get_chunks_from_etag $ETAG`
get_chunk_size $FILESIZE $CHUNKS
fi
fi
}
main "$@"
if [ ! -f "$FILE" ]; then
$ECHO_BIN "ERROR : File '$FILE' not found"
exit 2
fi
if ! is_integer "$SIZE"; then
$ECHO_BIN "ERROR : Chunk size is not an integer number"
exit 2
fi
ERROR_FILE="/tmp/s3md5-error-$$.out"
SUM_FILE="/tmp/s3md5-md5sumlist-$$.out"
BIN_FILE="/tmp/s3md5-md5bin-$$.out"
trap "{ cleanup; }" EXIT
$ECHO_BIN -n > "$SUM_FILE"
$ECHO_BIN -n > "$BIN_FILE"
n=0
i=0
while true; do
sum=`$DD_BIN bs=1024k count=$SIZE skip=$i if="$FILE" 2> "$ERROR_FILE" | $MD5_BIN | $CUT_BIN -d' ' -f1`
if $GREP_BIN -q "^0 bytes " "$ERROR_FILE"; then
# break this loop if 0 bytes read
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN "END"
fi
break
fi
if $GREP_BIN -q "cannot skip" "$ERROR_FILE"; then
# End of file, break this loop
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN "END"
fi
break
else
if [ $DEBUG -eq 1 ]; then
part=$((n+1))
to=$((i+SIZE))
$ECHO_BIN -n "SUM for part $part ($i to $to MB) ... "
fi
# Chunk read
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN "OK - $sum"
fi
i=$((i+SIZE))
n=$((n+1))
# Add its md5 sum to sum list file
$ECHO_BIN "$sum" >> "$SUM_FILE"
fi
done
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN "Converting all md5sums to binary"
fi
# Convert all md5 sums (in hex text notation) to bin and concatenate them into a file
$CAT_BIN "$SUM_FILE" | $AWK_BIN '{print $1}' | while read MD5; do $ECHO_BIN $MD5 | $XXD_BIN -r -p >> "$BIN_FILE"; done
# Calculate MD5 sum of this binary file
if [ "$IS_OSX" -eq 0 ]; then
s3sum=`$MD5_BIN "$BIN_FILE" | $CUT_BIN -d' ' -f1`
else
s3sum=`$MD5_BIN "$BIN_FILE" | $CUT_BIN -d' ' -f4`
fi
if [ $DEBUG -eq 1 ]; then
$ECHO_BIN -n "Etag/S3 MD5 Sum : "
fi
filename_in_output=""
[[ -n ${SHOW_FILENAME} ]] && filename_in_output=" ${FILE}"
if [ -n "$ETAG" ]; then
if [ $DEBUG -eq 1 ]; then
# Return Etag/S3 MD5 Sum
$ECHO_BIN "${s3sum}-${n}${filename_in_output}"
fi
if [ "${s3sum}-${n}" == "$ETAG" ]; then
$ECHO_BIN "TRUE"
exit 0
else
$ECHO_BIN "FALSE"
exit 1
fi
else
# Return Etag/S3 MD5 Sum
$ECHO_BIN "${s3sum}-${n}${filename_in_output}"
fi