forked from TobyMaxham/backblaze-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb2upload
121 lines (90 loc) · 2.78 KB
/
b2upload
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
#!/usr/bin/env bash
# ##################################################
# Backblaze Cloud Backup
#
version="0.0.0" # Sets version variable
#
function mainScript() {
############## Begin Script Here ###################
####################################################
if [ -s ~/.B2 ]
then
source ~/.B2
else
echo "The configuration file ~/.B2 does not exist"
safeExit
fi
echo -n
FILE_NAME=$(file --mime-type -b $1)
REMOTE_PATH=$2
AUTH_REL=$(curl -s https://api.backblaze.com/b2api/v1/b2_authorize_account -u "$B2_ACCOUNT_ID:$B2_API_KEY")
AUTH_TOKEN=$( echo "$AUTH_REL" | grep -Po '(?<="authorizationToken": ")[^"]*')
API_URL=$( echo "$AUTH_REL" | grep -Po '(?<="apiUrl": ")[^"]*')
UPLOAD_REL=$(curl -s \
-H 'Authorization: '"$AUTH_TOKEN"'' \
-d '{"bucketId": "'$B2_BUCKET_ID'"}' \
$API_URL/b2api/v1/b2_get_upload_url)
UPLOAD_AUTH=$( echo "$UPLOAD_REL" | grep -Po '(?<="authorizationToken": ")[^"]*' )
UPLOAD_URL=$( echo "$UPLOAD_REL" | grep -Po '(?<="uploadUrl": ")[^"]*' )
FILE_TO_UPLOAD=$1
MIME_TYPE=$(file --mime-type -b $1)
SHA1_OF_FILE=$(openssl dgst -sha1 $FILE_TO_UPLOAD | awk '{print $2;}')
curl -s \
-H "Authorization: $UPLOAD_AUTH" \
-H "X-Bz-File-Name: $REMOTE_PATH" \
-H "Content-Type: $MIME_TYPE" \
-H "X-Bz-Content-Sha1: $SHA1_OF_FILE" \
-H "X-Bz-Info-Author: unknown" \
--data-binary "@$FILE_TO_UPLOAD" \
--progress-bar \
$UPLOAD_URL > /dev/null
echo -e "Upload complete! "
####################################################
############### End Script Here ####################
}
usage() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is a script template. Edit this description to print help to users.
${bold}Options:${reset}
-h, --help Display this help and exit
-v, --version Output version information and exit
"
}
function safeExit() {
trap - INT TERM EXIT
exit
}
# Read the options and set stuff
while [[ $1 = -?* ]]; do
case $1 in
-h|--help) usage >&2; safeExit ;;
-v|--version) echo "$(basename $0) ${version}"; safeExit ;;
# -u|--username) shift; username=${1} ;;
# -p|--password) shift; echo "Enter Pass: "; stty -echo; read PASS; stty echo;
# echo ;;
# -v|--verbose) verbose=true ;;
# -l|--log) printLog=true ;;
# -q|--quiet) quiet=true ;;
# -s|--strict) strict=true;;
# -d|--debug) debug=true;;
# --force) force=true ;;
# --endopts) shift; break ;;
*) die "invalid option: '$1'." ;;
esac
shift
done
if [ "$1" == '' ]
then
echo "Please specify a local file!"
safeExit
fi
if [ "$2" == '' ]
then
REMOTE_PATH=$(dirname $1 | sed -r 's/^.{1}//' )/$(basename $1)
else
REMOTE_PATH=$2
fi
# Run your script
mainScript $1 $REMOTE_PATH $3 $4 $5
# Exit cleanly
safeExit