-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropbox_upload
executable file
·128 lines (120 loc) · 4.98 KB
/
dropbox_upload
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
#!/bin/sh
# 02/03/17 - Created by Andrew Puckett
# 03/02/17 - Write to .updated immediately after checking for new files, add support for getopts
# 03/06/17 - Zip auth.log and syslog before uploading
# 02/03/19 - Skip journal logs
# 01/28/20 - Zip all logs before uploading and improve logging
# 04/04/22 - Use Oauth2 because dropbox is deprecating long-term tokens
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "NAME"
echo " $(basename $0) options"
echo
echo "DESCRIPTION"
echo " $(basename $0) is used to upload a directory to Dropbox API."
echo " Start at https://www.dropbox.com/developers/apps and create an app. Add all file and folder permissions."
echo
echo " Use https://www.dropbox.com/oauth2/authorize?client_id=<APP_KEY>&response_type=code and get code."
echo " Then use curl https://api.dropbox.com/oauth2/token \\"
echo " -d code=<AUTHORIZATION_CODE> \\"
echo " -d grant_type=authorization_code \\"
echo " -d redirect_uri=http://localhost \\"
echo " -u <APP_KEY>:<APP_SECRET>"
echo " Copy refresh_token"
echo " Put APP_KEY, APP_SECRET, REFRESH_TOKEN, DBTOKEN=, and EXPIRES=0 in /etc/dropbox"
echo
echo "OPTIONS"
echo " -d"
echo " Directory to upload. Default /var/dropbox"
echo " -o"
echo " Option for Curl without leading -. Default s"
echo " -t"
echo " Prompt for an Authorization Token. Otherwise looks in /etc/dropbox."
echo "AUTHOR"
echo " Andrew Puckett"
echo
exit 1
fi
CURLOPTS=-s
DBLOCATION=/var/dropbox
CURLOUT=/dev/stdout
while getopts d:o:t opt
do
case "$opt" in
d) DBLOCATION=$OPTARG;;
o) CURLOPTS=-$OPTARG;;
t) DBTOKEN=1;;
esac
done
[ "$DBTOKEN" = 1 ] && read -p "Token: " DBTOKEN
[ -n "$DBTOKEN" ] || . /etc/dropbox
[ -z "$DBTOKEN" ] && [ -z "$REFRESH_TOKEN" ] && echo "ERROR: No Access Token Found." >&2 && exit 1
[ "$CURLOPTS" = "-s" ] && CURLOUT=/dev/null
if [ -n "$EXPIRES" ] && [ $EXPIRES -le $(($(date +%s)+120)) ]; then
JSON=$(curl $CURLOPTS https://api.dropbox.com/oauth2/token -d grant_type=refresh_token -d refresh_token=$REFRESH_TOKEN -u $APP_KEY:$APP_SECRET)
ACCESS_TOKEN=$(echo $JSON | jq -r ".access_token")
EXPIRES=$(echo $JSON | jq -r ".expires_in")
if [ -n "$ACCESS_TOKEN" ] && [ -n "$EXPIRES" ]; then
DBTOKEN=$ACCESS_TOKEN
EXPIRES=$(($(date +%s)+$EXPIRES))
sed -i "/^DBTOKEN/ s/.*/DBTOKEN=$DBTOKEN/" /etc/dropbox
sed -i "/^EXPIRES/ s/.*/EXPIRES=$EXPIRES/" /etc/dropbox
else
echo "ERROR: Access Token Refresh failed." >&2 && exit 1
fi
fi
[ -f $DBLOCATION/.updated ] && FINDTESTS="-cnewer $DBLOCATION/.updated"
sleep 15 # we can catch files that were just updated via cron at hh:mm:00
DATE=$(date -R)
FILES=$(find -L $DBLOCATION -type f $FINDTESTS)
touch -d "$DATE" $DBLOCATION/.updated
echo "Uploading $(echo $FILES | wc -w) files to Dropbox"
IFS="$(printf '\n\t')"
for FILE in $FILES; do
REMOVE=0
if [ -n "$(echo "$FILE" | grep -E "$DBLOCATION/log/journal/")" ] || [ ! -f $FILE ]; then
continue
fi
if [ -n "$(echo "$FILE" | grep -E "$DBLOCATION/log/.*log(\.1)?$")" ]; then
gzip -c -9 $FILE > $FILE.gz
FILE=$FILE.gz
REMOVE=1
fi
SIZE=$(du -b $FILE | cut -f1)
DBPATH=$(echo "$FILE" | sed "s@$DBLOCATION@@")
MODTIME=$(date -r "$FILE" +"%Y-%m-%dT%H:%M:%SZ")
if [ $SIZE -lt 150000000 ]; then
curl $CURLOPTS -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer $DBTOKEN" \
--header "Dropbox-API-Arg: {\"path\": \"$DBPATH\",\"mode\": \"overwrite\",\"autorename\": false,\"client_modified\": \"$MODTIME\",\"mute\": true}" \
--header "Content-Type: application/octet-stream" \
--data-binary "@$FILE" > "$CURLOUT"
else
split -b 100m -d "$FILE" "$FILE.part-"
PART="$FILE.part-00"
SESSION=$(curl $CURLOPTS -X POST https://content.dropboxapi.com/2/files/upload_session/start \
--header "Authorization: Bearer $DBTOKEN" \
--header "Dropbox-API-Arg: {\"close\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary "@$PART")
SESSION=$(echo "$SESSION" | awk '{gsub("\"","",$2);sub("}","",$2);} {print $2;}')
OFFSET=$(du -b $PART | cut -f1)
rm "$PART"
for PART in $(find $FILE.part-* | head -n -1); do
curl $CURLOPTS -X POST https://content.dropboxapi.com/2/files/upload_session/append_v2 \
--header "Authorization: Bearer $DBTOKEN" \
--header "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"$SESSION\",\"offset\": $OFFSET},\"close\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary "@$PART" > "$CURLOUT"
OFFSET=$(($OFFSET+$(du -b $PART | cut -f1)))
rm "$PART"
done
PART=$(find $FILE.part-*)
curl $CURLOPTS -X POST https://content.dropboxapi.com/2/files/upload_session/finish \
--header "Authorization: Bearer $DBTOKEN" \
--header "Dropbox-API-Arg: {\"cursor\": {\"session_id\": \"$SESSION\",\"offset\": $OFFSET},\"commit\": {\"path\": \"$DBPATH\",\"mode\": \"overwrite\",\"autorename\": false,\"client_modified\": \"$MODTIME\",\"mute\": true}}" \
--header "Content-Type: application/octet-stream" \
--data-binary "@$PART" > "$CURLOUT"
rm "$PART"
fi
[ $REMOVE -eq 1 ] && rm "$FILE"
done