Upload file on Linux (CLI) to Dropbox (via bash/sh)?
Asked Answered
M

3

46

I need to save (and overwrite) a file via the cron (hourly) to my dropbox account. The file needs to be stored in a predefined location (which is shared with some other users).

I have seen the possibility to create a Dropbox App, but that create its own dropbox folder.

Also looked at Dropbox Saver but that seems for browsers.

I was thinking (hoping) something super lightweight, a long the lines of CURL, so i don't need to install libraries. Just a simple sh script would be awesome. I only need to PUT the file (overwrite), no need to read (GET) it back.

Was going thru the dropbox developer API documentation, but kind of got lost.

Anybody a good hint?

Manicotti answered 8/2, 2017 at 18:20 Comment(4)
Oh here we go again. Bash is a programming language. If i put it on serverfault they close it since its programming. Please give me a break :/Manicotti
The close vote is not for "not a programming language", but asking for an off-site resource.Kusin
Well thats not how the votes where casted. Anyway, its about the dropbox API and I don't see how thats not programming related... Well, what ever makes your boat float. I will remove the question later today.Manicotti
Yes, that's how they were cast. "Request for off-site resource" is one of the choices within "Off-topic". The question is also very broad: a solution would have to encompass a complete Bash/shell script, and it's also not very clear: "something like curl", "good hint" aren't super specific, to be honest.Kusin
C
61

First, since you need to access an existing shared folder, register a "Dropbox API" app with "Full Dropbox" access:

https://www.dropbox.com/developers/apps/create

Then, get an access token for your account for your app. The easiest way is to use the "Generate" button on your app's page, where you'll be sent after you create the app. It's also accessible via the App Console.

Then, you can upload to a specified path via curl as shown in this example:

This uploads a file from the local path matrices.txt in the current folder to /Homework/math/Matrices.txt in the Dropbox account, and returns the metadata for the uploaded file:

echo "some content here" > matrices.txt

curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer <ACCESS_TOKEN>" \
    --header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\", \"mode\": \"overwrite\", \"strict_conflict\": false}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @matrices.txt

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.

Cathodoluminescence answered 8/2, 2017 at 18:32 Comment(8)
thanks! That means full access. So in any case of a break-in, via that token you can basically access all my files?Manicotti
Yes, you can find more information on the different permissions here: dropbox.com/developers/reference/…Cathodoluminescence
Thanks. I thought so :( The app is working, but it doesn't allow to share the folder. Sigh. And i don't want to expose all my files (via a token) on some server.Manicotti
@Manicotti you can create an app that deals with only one directory so you are not exposing all you data on dboxLat
And make sure not to miss the @ before the filename, as it tells curl to upload the contents of the file and not the filenameEmblazonment
Currently this doesn't seem to work. I'm just trying to put some backups on dropbox... every try fails with obscure error messages... sometimes confilct, somtimes a more generic error :(Dustydusza
This solution worked well for me. However, there is a size limit of 150 MB. That was ok for me, I could just split the file. If that's not enough, you can upload up to 350 GB via an upload session.Oppugnant
When creating a scoped app: You'll have to set the permission files.content.write (and confirming it by hitting submit) first before generating the access token.Kistler
A
20

@Greg's answer also works but seems like a long chore.

I used Dropbox's official command-line interface here: https://github.com/dropbox/dbxcli.

As the date of posting, it is working fine and provides lots of helpful commands for downloading and uploading.

Agni answered 6/10, 2020 at 23:34 Comment(3)
This is the best answer, since it uses the official Dropbox command-line interface. The binaries are available at https://github.com/dropbox/dbxcli/releasesLuting
As of March 2023, the repo says it is not official: github.com/dropbox/dbxcli/commit/…Olympie
They are testing it publicly before making the go SDK (that dbxcli uses) official. But it is made by them (and hosted on their github account)Pentosan
U
1

Another solution I just tried is a bash utility called Dropbox-Uploader. After configuration through the same steps as above (app creation and token generation), you can just do: ./dropbox_uploader.sh upload mylocal_file my_remote_file, which I find pretty convenient.

Unconditional answered 24/5, 2020 at 13:37 Comment(8)
many people say it does not work for now github.com/andreafabrizi/Dropbox-Uploader/issues/498Fidole
yes I see that, when I used it it worked perfectly fine. Some say you need to use another APIUnconditional
I just used it now and it worked perfectly. I am using a token I used back in May that is still working.Unconditional
@YashKant could you copy-paste the error trace, if any, for reference?Unconditional
@ZaccharieRamzi: The error was also quite unhelpful, it said "Some error occured. Please check the log". And I could not find a log file as well [github.com/andreafabrizi/Dropbox-Uploader/issues/509]. What worked was this: github.com/dropbox/dbxcliAgni
Happy to see that at least there exists a backup solution, although it doesn't seem maintained either. For the record, I just used Dropbox-Uploader and it worked just fine. Maybe we could compare our setups.Unconditional
Wow, that's a fine working solution!Dustydusza
For what it's worth, I just tried this script today (February 2023) and it still works absolutely fine! Very happy with this solution 👍Wineshop

© 2022 - 2024 — McMap. All rights reserved.