Hello,
Â
I'm trying to create a script to for each over a folder, and upload to the API but I cannot figure out where I'm going wrong.
Â
This is the script I have so far, any help would be appreciated 🙂
Â
Spoiler
Â
#!/bin/bash
UPLOAD_PATH="Backups"
if [[ -f '/root/dropbox.cfg' ]]; then
echo 'It appears you have set an API Key for Dropbox already.'
echo 'Using your configured API Key to proceed...'
source /root/dropbox.cfg
else
echo 'No existing API Key for Dropbox was found...'
read -p 'Please provide an API Key to proceed ' api_key
if [[ -z "$api_key" ]]; then
echo "No key was provided. Exiting..."
exit
fi
echo "DROPBOX_API_KEY=\"$api_key\"" >> /root/dropbox.cfg
DROPBOX_API_KEY=$api_key
fi
read -p 'Please provide the path to the folder you wish to upload ' dest_path
if [[ -z "$dest_path" ]]; then
echo "No path was provided. Exiting..."
exit
else
# Check if upload session exists
if [[ -f "$dest_path/uploadsession.cfg" ]]; then
echo "File exists using this upload session."
source "$dest_path/uploadsession.cfg"
# If it doesn't exist... create a session to be used
else
cd $dest_path
touch uploadsession.cfg
FIRSTFILE="$dest_path/uploadsession.cfg"
curl -X POST https://content.dropboxapi.com/2/files/upload_session/start \
--header "Authorization: Bearer $DROPBOX_API_KEY" \
--header "Dropbox-API-Arg: {\"close\":false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @${FIRSTFILE} >> /root/output.txt
result=$(grep -oP '(?<="session_id": ")[^"]*' /root/output.txt)
SESSION_ID=""
IFS=':'
for x in $result
do
if [[ $x != "pid_upload_session" ]]; then
SESSION_ID=$x
fi
done
unset IFS
echo "SESSION_ID=\"$SESSION_ID\"" >> "$dest_path/uploadsession.cfg"
echo "Session ID = $SESSION_ID"
fi
# For each through each directory and folder, append to session
for file in $dest_path/* $dest_path/**/*; do
echo ${SESSION_ID};
echo $file;
curl -X POST https://content.dropboxapi.com/2/files/upload_session/append_v2 \
--header "Authorization: Bearer $DROPBOX_API_KEY" \
--header "Dropbox-API-Arg: {\"close\":false,\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}" \
--header "Content-Type: application/octet-stream" \
--data-binary @${file} >> /root/output.txt
done;
# Finish Session
curl -X POST https://api.dropboxapi.com/2/files/upload_session/finish_batch_v2 \
--header "Authorization: Bearer $DROPBOX_API_KEY" \
--header "Content-Type: application/json" \
--data "{\"entries\":[{\"commit\":{\"autorename\":true,\"mode\":\"add\",\"mute\":false,\"path\":\"/${UPLOAD_PATH}\",\"strict_conflict\":false},\"cursor\":{\"offset\":0,\"session_id\":\"${SESSION_ID}\"}}]}"
fi<br />Â