-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.sh
169 lines (151 loc) · 4.38 KB
/
upload.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
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
#!/bin/bash
#
# This script takes a path to a file and uploads it to Amazon
# Glacier. It does this in several steps:
#
# 1. Split the file up into 1MiB chunks.
# 2. Initiate a multipart upload.
# 3. Upload each part individually.
# 4. Calculate the file's tree hash and finish the upload.
#
# See: http://amzn.to/1RjTwYk
#
# Author: Damien Radtke <damienradtke at gmail dot com>
# License: WTFPL
# 1 MiB in bytes; the tree hash algorithm requires chunks of this
# size.
CHUNK_SIZE=1048576
if [[ -z "${1}" ]]; then
echo "No file provided."
echo "Usage: upload.sh <file> <description> <vault name>"
exit 1
fi
if [[ -z "${2}" ]]; then
echo "No description provided."
exit 1
fi
if [[ -z "${2}" ]]; then
echo "No vault name provided."
exit 1
fi
VAULT_NAME="${3}"
ARCHIVE="`[[ $1 = /* ]] && echo \"$1\" || echo \"$PWD/${1#./}\"`"
ARCHIVE_SIZE=`stat -f%z ${ARCHIVE}`
TEMP=`mktemp -d`
cd "${TEMP}"
# Clean up at exit.
function cleanup {
echo "Cleaning up."
cd ~-
rm -rf "${TEMP}"
}
trap cleanup EXIT
echo "Initiating multipart upload..."
echo "Vault name: $3"
echo "File: $1"
echo "Description: $2"
# Split the archive into chunks.
split -a 5 -b ${CHUNK_SIZE} "${ARCHIVE}" chunk
NUM_CHUNKS=`ls chunk* | wc -l`
echo "Chunks created: $NUM_CHUNKS"
# Initiate upload.
UPLOAD_ID=$(aws glacier initiate-multipart-upload \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--archive-description="${2}" \
--part-size=${CHUNK_SIZE} \
--query=uploadId | sed 's/"//g')
RETVAL=$?
if [[ ${RETVAL} -ne 0 ]]; then
echo "initiate-multipart-upload failed with status code: ${RETVAL}"
exit 1
fi
echo "Upload ID: ${UPLOAD_ID}"
# Abort the upload if forced to exit.
function abort_upload {
echo "Aborting upload."
aws glacier abort-multipart-upload \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--upload-id="${UPLOAD_ID}"
}
trap abort_upload SIGINT SIGTERM
# Loop through the chunks.
INDEX=0
for CHUNK in chunk*; do
# Calculate the byte range for this chunk.
START=$((INDEX*CHUNK_SIZE))
END=$((((INDEX+1)*CHUNK_SIZE)-1))
END=$((END>(ARCHIVE_SIZE-1)?ARCHIVE_SIZE-1:END))
# Increment the index.
INDEX=$((INDEX+1))
while true; do
echo "Uploading chunk ${INDEX} / ${NUM_CHUNKS}..."
aws glacier upload-multipart-part \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--upload-id="${UPLOAD_ID}" \
--body="${CHUNK}" \
--range="bytes ${START}-${END}/*" \
>/dev/null
RETVAL=$?
if [[ ${RETVAL} -eq 0 ]]; then
# Upload succeeded, on to the next one.
break
elif [[ ${RETVAL} -eq 130 ]]; then
# Received a SIGINT.
exit 1
elif [[ ${RETVAL} -eq 255 ]]; then
# Most likely a timeout, just let it try again.
echo "Chunk ${INDEX} ran into an error, retrying..."
sleep 1
else
echo "upload-multipart-part failed with status code: ${RETVAL}"
echo "Aborting upload."
aws glacier abort-multipart-upload \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--upload-id="${UPLOAD_ID}"
exit 1
fi
done
openssl dgst -sha256 -binary ${CHUNK} > "hash${CHUNK:5}"
done
# Calculate tree hash.
# ("And now for the tricky bit.")
echo "Calculating tree hash..."
while true; do
COUNT=`ls hash* | wc -l`
if [[ ${COUNT} -le 2 ]]; then
TREE_HASH=$(cat hash* | openssl dgst -sha256 | awk '{print $2}')
break
fi
ls hash* | xargs -n 2 | while read PAIR; do
PAIRARRAY=(${PAIR})
if [[ ${#PAIRARRAY[@]} -eq 1 ]]; then
break
fi
cat ${PAIR} | openssl dgst -sha256 -binary > temphash
rm ${PAIR}
mv temphash "${PAIRARRAY[0]}"
done
done
echo "Finalizing..."
aws glacier complete-multipart-upload \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--upload-id="${UPLOAD_ID}" \
--checksum="${TREE_HASH}" \
--archive-size=${ARCHIVE_SIZE}
RETVAL=$?
if [[ ${RETVAL} -ne 0 ]]; then
echo "complete-multipart-upload failed with status code: ${RETVAL}"
echo "Aborting upload ${UPLOAD_ID}"
aws glacier abort-multipart-upload \
--account-id=- \
--vault-name="${VAULT_NAME}" \
--upload-id="${UPLOAD_ID}"
exit 1
fi
echo "Done."
exit 0