-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.sh
executable file
·93 lines (86 loc) · 2.06 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
#!/usr/bin/env bash
. constants.sh
VAULT=glacier_valut
START_TIME=23
END_TIME=8
prefix_length=${#PREFIX}
function fail {
echo $1 >&2
exit 1
}
function retry {
local n=1
local max=5
local delay=15
while true; do
"$@" && break || {
if [[ $n -lt $max ]]; then
((n++))
echo "Command failed. Attempt $n/$max:"
sleep $delay;
else
fail "The command has failed after $n attempts."
fi
}
done
}
decode () {
ret=0
place=1
num_digits=${#1}
for (( index=$(($num_digits - 1)); index>=0; index-- )); do
char=${1:$index:1}
char_val=$(printf "%d\n" \'$char)
ret=$(($ret + ($char_val - 97) * $place))
place=$(($place * 26 ))
done
echo $ret
}
upload () {
file_num=$(decode ${1:$prefix_length})
file="$3/$1"
echo "file num: $file_num"
start=$(( $SPLIT_SIZE * $file_num ))
end=$(( $start + $(bytes "$file") - 1 ))
retry aws glacier upload-multipart-part --account-id - --vault-name $VAULT --upload-id $2 --range "bytes $start-$end/$4" --body "$file" --checksum $(cat "$file" | treehash)
}
initiate () {
if [ -f "$1/.id" ]; then
cat "$1/.id"
else
ID=$(aws glacier initiate-multipart-upload --account-id - --part-size $SPLIT_SIZE --vault-name $VAULT --archive-description "backup for $2" | awk '{print $2}')
echo $ID > "$1/.id"
echo $ID
fi
}
cleanup () {
aws glacier complete-multipart-upload --account-id - --vault-name $VAULT --upload-id "$1" --archive-size "$2" --checksum "$3"
rm "$DIR/.id"
rm "$DIR/.size"
rm "$DIR/.hash"
rmdir "$DIR"
}
check_time () {
now=$(date "+%H")
if [ $now -gt $END_TIME -a $now -le $START_TIME ]; then
exit 1
fi
}
for I in $(ls "$MY_DIR"); do
DIR="$MY_DIR/$I"
if [ -d "$DIR" ]; then
ID=$(initiate $DIR $I)
HASH=$(cat "$DIR/.hash")
SIZE=$(cat "$DIR/.size")
if [ "$(ls "$DIR")" ]; then
for FILE in $(ls "$DIR"); do
check_time
upload "$FILE" "$ID" "$DIR" $SIZE
rm "$DIR/$FILE"
done
fi
if [ $HASH ]; then
cleanup "$ID" "$SIZE" "$HASH" "$DIR"
fi
fi
done