-
Notifications
You must be signed in to change notification settings - Fork 2
/
gdrive.bash
307 lines (290 loc) · 11.4 KB
/
gdrive.bash
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
304
305
306
307
#!/bin/bash
BUTGG_DIR="${HOME}/.butgg"
BUTGG_CONF="${HOME}/.butgg/butgg.conf"
GDRIVE_API="https://www.googleapis.com/drive/v3"
DF_PARENTS_ID="root"
FIRST_OPTION=$1
SECOND_OPTION=$2
THIRD_OPTION=$3
if [ ! -d "${BUTGG_DIR}" ]
then
mkdir ${BUTGG_DIR}
fi
loop_dir(){
IFS=$'\n'
for i in $(ls -1 "$1")
do
REAL_PATH=`realpath "$1/$i"`
DIR_DEPTH=`echo ${REAL_PATH} | grep -o "/" | wc -l`
DIR_DEPTH2=`expr ${DIR_DEPTH} - 1`
if [ -d "${REAL_PATH}" ]
then
echo "Creating directory $i..."
_mkdir "$i" ${!PR_ID_NAME} >/dev/null
PARENTS_ID=${FOLDER_ID}
PR_ID_NAME="PARENTS_ID${DIR_DEPTH}"
eval "$PR_ID_NAME"="$PARENTS_ID"
loop_dir ${REAL_PATH}
else
PR_ID_NAME="PARENTS_ID${DIR_DEPTH2}"
echo "Uploading file $i..."
curl -# -X POST -L \
-H "Authorization: Bearer ${GG_TOKEN}" \
-F "metadata={name: \"$i\", parents: [\"${!PR_ID_NAME}\"]};type=application/json;charset=UTF-8" \
-F "file=@${REAL_PATH}" \
https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart >/dev/null
fi
done
}
create_token(){
if [ -f "${BUTGG_DIR}/butgg.conf" ]
then
GG_CLIENT_ID=`cat ${BUTGG_DIR}/butgg.conf | grep CLIENT_ID | cut -d"=" -f2`
GG_CLIENT_SECRET=`cat ${BUTGG_DIR}/butgg.conf | grep CLIENT_SECRET | cut -d"=" -f2`
if [[ ${GG_CLIENT_ID} == "" ]] || [[ ${GG_CLIENT_SECRET} == "" ]]
then
echo "Read more: https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step"
read -p " Your Google API client_id: " GG_CLIENT_ID
read -p " Your Google API client_secret: " GG_CLIENT_SECRET
sed -i "/^GG_CLIENT_ID/d" ${BUTGG_DIR}/butgg.conf
sed -i "/^GG_CLIENT_SECRET/d" ${BUTGG_DIR}/butgg.conf
echo "GG_CLIENT_ID=${GG_CLIENT_ID}" >> ${BUTGG_DIR}/butgg.conf
echo "GG_CLIENT_SECRET=${GG_CLIENT_SECRET}" >> ${BUTGG_DIR}/butgg.conf
fi
else
echo "Read more: https://github.com/mbrother2/backuptogoogle/wiki/Create-own-Google-credential-step-by-step"
read -p " Your Google API client_id: " GG_CLIENT_ID
read -p " Your Google API client_secret: " GG_CLIENT_SECRET
echo "GG_CLIENT_ID=${GG_CLIENT_ID}" >> ${BUTGG_DIR}/butgg.conf
echo "GG_CLIENT_SECRET=${GG_CLIENT_SECRET}" >> ${BUTGG_DIR}/butgg.conf
fi
echo ""
echo "Authentication needed"
echo "Go to the following url in your browser:"
echo "https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=${GG_CLIENT_ID}&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state"
echo ""
read -p "Enter verification code: " GG_VERIFY_CODE
echo "Generating token file..."
curl -s -X POST \
--data "code=${GG_VERIFY_CODE}&client_id=${GG_CLIENT_ID}&client_secret=${GG_CLIENT_SECRET}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&access_type=offline" \
https://accounts.google.com/o/oauth2/token \
-o ${BUTGG_DIR}/token.json
TOKEN_START=`date +%s`
TOKEN_END=`expr $TOKEN_START + 3600`
TIME_END=`date -d @${TOKEN_END} --rfc-3339=seconds`
sed -i "s/.*\"expires_in\":.*/\ \ \"expires_in\": \"${TIME_END}\",/" ${BUTGG_DIR}/token.json
}
get_token(){
if [ ! -f "${BUTGG_DIR}/token.json" ]
then
create_token
else
GG_TOKEN=`cat ${BUTGG_DIR}/token.json | grep access_token | cut -d'"' -f4`
if [ "${GG_TOKEN}" == "" ]
then
create_token
GG_TOKEN=`cat ${BUTGG_DIR}/token.json | grep access_token | cut -d'"' -f4`
fi
fi
}
refresh_token(){
get_token
TOKEN_END=`cat ${BUTGG_DIR}/token.json | grep expires_in | cut -d'"' -f4`
TOKEN_END=`date -d "${TOKEN_END}" +%s`
TIME_NOW=`date +%s`
CHECK_TIME=`expr ${TOKEN_END} - ${TIME_NOW} - 5`
if [ ${CHECK_TIME} -lt 0 ]
then
TOKEN_END=`expr ${TIME_NOW} + 3600`
TIME_END=`date -d @${TOKEN_END} --rfc-3339=seconds`
GG_REFRESH_TOKEN=`cat ${BUTGG_DIR}/token.json | grep refresh_token | cut -d'"' -f4`
GG_CLIENT_ID=`cat ${BUTGG_DIR}/butgg.conf | grep CLIENT_ID | cut -d"=" -f2`
GG_CLIENT_SECRET=`cat ${BUTGG_DIR}/butgg.conf | grep CLIENT_SECRET | cut -d"=" -f2`
curl -s -X POST https://accounts.google.com/o/oauth2/token \
--data "client_id=${GG_CLIENT_ID}&client_secret=${GG_CLIENT_SECRET}&refresh_token=${GG_REFRESH_TOKEN}&grant_type=refresh_token" -o ${BUTGG_DIR}/token.json
sed -i "/.*\"scope\":.*/i \ \ \"refresh_token\": \"${GG_REFRESH_TOKEN}\"," ${BUTGG_DIR}/token.json
sed -i "s/.*\"expires_in\":.*/\ \ \"expires_in\": \"${TIME_END}\",/" ${BUTGG_DIR}/token.json
fi
}
check_info(){
CHECK_INFO=`_info name "$1"`
if [ $? -ne 0 ]
then
echo "${CHECK_INFO}"
exit 1
fi
}
_about(){
GD_EMAIL=`curl -s "${GDRIVE_API}/about?fields=user" -H "Authorization: Bearer ${GG_TOKEN}" -H "Accept: application/json" --compressed | grep emailAddress | cut -d'"' -f4`
GD_LIMIT=`curl -s "${GDRIVE_API}/about?fields=storageQuota" -H "Authorization: Bearer ${GG_TOKEN}" -H "Accept: application/json" --compressed | grep "\"limit\"" | cut -d'"' -f4`
GD_USAGE=`curl -s "${GDRIVE_API}/about?fields=storageQuota" -H "Authorization: Bearer ${GG_TOKEN}" -H "Accept: application/json" --compressed | grep "\"usage\"" | cut -d'"' -f4`
if [ ${GD_USAGE} -ge 1099511627776 ]
then
GD_USAGE2=`awk "BEGIN {printf \"%.2f\n\", ${GD_USAGE} / 1099511627776}"`" TB"
elif [ ${GD_USAGE} -ge 1073741824 ]
then
GD_USAGE2=`awk "BEGIN {printf \"%.2f\n\", ${GD_USAGE} / 1073741824}"`" GB"
elif [ ${GD_USAGE} -ge 1048576 ]
then
GD_USAGE2=`awk "BEGIN {printf \"%.2f\n\", ${GD_USAGE} / 1048576}"`" MB"
elif [ ${GD_USAGE} -ge 1024 ]
then
GD_USAGE2=`awk "BEGIN {printf \"%.2f\n\", ${GD_USAGE} / 1024}"`" KB"
else
GD_USAGE2="${GD_USAGE} B"
fi
GD_FREE=`awk "BEGIN {printf \"%.2f\n\", (${GD_LIMIT} - ${GD_USAGE}) / 1073741824}"`
GD_LIMIT=`awk "BEGIN {printf \"%.2f\n\", ${GD_LIMIT} / 1073741824}"`
echo "Email: ${GD_EMAIL}"
echo "Total: ${GD_LIMIT} GB"
echo "Used : ${GD_USAGE2}"
echo "Free : ${GD_FREE} GB"
}
_upload(){
if [ -z "$2" ]
then
PARENTS_ID="${DF_PARENTS_ID}"
else
PARENTS_ID="$2"
fi
check_info "${PARENTS_ID}"
REAL_PATH=`realpath "$1"`
FILE_NAME=`echo "$1" | sed '$s/\/$//' | grep -o '[^/]*$'`
if [ -d "${REAL_PATH}" ]
then
echo "Creating directory ${FILE_NAME}..."
DIR_DEPTH=`echo "${REAL_PATH}" | grep -o "/" | wc -l`
_mkdir "${FILE_NAME}" "${PARENTS_ID}" >/dev/null
PARENTS_ID=${FOLDER_ID}
PR_ID_NAME="PARENTS_ID${DIR_DEPTH}"
eval "$PR_ID_NAME"="$PARENTS_ID"
loop_dir "${REAL_PATH}"
else
echo "Uploading file ${FILE_NAME}..."
curl -# -X POST -L https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart \
-H "Authorization: Bearer ${GG_TOKEN}" \
-F "metadata={name: \"${FILE_NAME}\", parents: [\"${PARENTS_ID}\"]};type=application/json;charset=UTF-8" \
-F "file=@${REAL_PATH}" >/dev/null
fi
}
_list(){
if [ -z "$2" ]
then
echo "Missing file ID"
exit 1
elif [ "$1" == "all" ]
then
MIMETYPE=""
elif [ "$1" == "dir" ]
then
MIMETYPE="+and+mimeType+=+'application/vnd.google-apps.folder'"
elif [ "$1" == "file" ]
then
MIMETYPE="+and+mimeType+!=+'application/vnd.google-apps.folder'"
else
echo "Do not support option $1"
exit 1
fi
check_info "$2"
for i in $(curl -s "${GDRIVE_API}/files?q=\"$2\"+in+parents+and+trashed=false${MIMETYPE}" -H "Authorization: Bearer ${GG_TOKEN}" -H "Accept: application/json" --compressed | grep "\"id\":" | cut -d'"' -f4)
do
NAME=`curl -s "${GDRIVE_API}/files/$i" -H "Authorization: Bearer ${GG_TOKEN}" -H "Accept: application/json" --compressed | grep "\"name\":" | cut -d'"' -f4`
echo "$i ${NAME}"
done
}
_mkdir(){
if [ -z "$2" ]
then
PARENTS_ID="root"
else
PARENTS_ID="$2"
fi
check_info "${PARENTS_ID}"
FOLDER_ID=`curl -s -X POST ${GDRIVE_API}/files -H "Authorization: Bearer ${GG_TOKEN}" -H 'Content-Type: application/json' --data "{name: \"$1\",mimeType: \"application/vnd.google-apps.folder\",parents: [\"${PARENTS_ID}\"]}" --compressed | grep "\"id\":" | cut -d'"' -f4`
if [ -z "{FOLDER_ID}" ]
then
echo "Can not create file $1"
exit 1
else
echo "${FOLDER_ID}"
fi
}
_delete(){
check_info "$1"
DELETE_FILE=`curl -s -X DELETE ${GDRIVE_API}/files/$1 -H "Authorization: Bearer ${GG_TOKEN}" -H 'Content-Type: application/json' --compressed`
if [ -z "${DELETE_FILE}" ]
then
echo "Deleted file ID $1"
else
echo "${DELETE_FILE}"
exit 1
fi
}
_info(){
if [ -z "$2" ]
then
echo "Missing file ID"
exit 1
elif [[ "$1" == "name" ]] || [[ "$1" == "mimeType" ]]
then
FILE_INFO=`curl -s "${GDRIVE_API}/files/$2" -H "Authorization: Bearer ${GG_TOKEN}" | grep "\"$1\":" | cut -d'"' -f4`
elif [ "$1" == "trashed" ]
then
FILE_INFO=`curl -s "${GDRIVE_API}/files/$2?fields=trashed" -H "Authorization: Bearer ${GG_TOKEN}" | grep "\"$1\":" | cut -d' ' -f3`
else
echo "Do not support option $1"
exit 1
fi
if [ "${FILE_INFO}" == "" ]
then
echo "Can not find file ID ${FILE_ID}"
exit 1
else
echo "${FILE_INFO}"
fi
}
_download(){
FILE_TYPE=`_info mimeType "$1"`
if [ ${FILE_TYPE} == "application/vnd.google-apps.folder" ]
then
echo "Do not support download directory"
exit 1
fi
FILE_NAME=`_info name "$1"`
echo "Downloading file $1..."
curl -# "${GDRIVE_API}/files/$1?alt=media" -H "Authorization: Bearer ${GG_TOKEN}" -o "${FILE_NAME}"
}
_help(){
echo "gdrive.bash - Simple upload to Google Drive use curl"
echo ""
echo "Usage: gdrive.bash [options] [command] [file ID]"
echo ""
echo "Options:"
echo " --about show Google account informations"
echo " --upload upload to Google Drive"
echo " --list list directory and file"
echo " all list all directory and file"
echo " dir list directory only"
echo " file list file only"
echo " --mkdir create directory on Google Drive"
echo " --delete delete file or directory on Google Drive"
echo " --info show info of file"
echo " name show name only"
echo " mimeType show mimeType only"
echo " trashed show file in trash or not"
echo " --download download file. Not download directory"
echo " --help show this help message and exit"
}
refresh_token
get_token
case ${FIRST_OPTION} in
--about) _about;;
--upload) _upload "${SECOND_OPTION}" "${THIRD_OPTION}";;
--list) _list "${SECOND_OPTION}" "${THIRD_OPTION}";;
--mkdir) _mkdir "${SECOND_OPTION}" "${THIRD_OPTION}";;
--delete) _delete "${SECOND_OPTION}";;
--info) _info "${SECOND_OPTION}" "${THIRD_OPTION}";;
--download) _download "${SECOND_OPTION}";;
--help) _help;;
*) echo "Not support option ${FIRST_OPTION}"; _help;;
esac