måndag 4 januari 2016

Using Dropbox from command line in Fedora

Dropbox is a file hosting service operated by Dropbox Inc. that offers easy cloud storage on multiple platforms. Note that Dropbox is proprietary software, but offers a free account with up to 2 GB of storage [1]. Personally I've used Dropbox to sync files between PCs and also phones.

In Fedora linux, you can install Dropbox from the RPM Fusion repository [2]. This is basically an installer and command line client script for the binary Dropbox daemon (application) that then has to be separately installed by each user. There is also a plugin for nautilus, the file manager: nautilus-dropbox.

To install, simply issue on the command line as root (or sudo):
dnf install dropbox nautilus-dropbox
You could also download the program from Dropbox home page and install it manually [3]. I prefer using the Fedora package, then it will be nicely integrated also in Gnome on a desktop PC. The following instruction is how to install and configure on a headless PC, using only the command line (well almost, you need a web browser at some point).

To install the actual daemon (application), issue the following command as user:
dropbox start -i
Let the program download the daemon and install it.

If you already started the daemon, ensure it is stopped with:
dropbox stop
Now run the dameon manually from command line to initialize it and connect to your Dropbox account:
You should see the following output:
 This client is not linked to any account... Please visit https://www.dropbox.com/cli_link?host_id=7d44a548aa48f285f3da0x63564d03c2 to link this machine.
You need to copy this link and paste it into a browser and then login to your Dropbox account. How this is done is up to you. Either copy the link from an SSH window, type it manually on a separate device that has a browser or use link (lynx) on the headless PC. After this step, Dropbox will create a ~/Dropbox folder and start synchronizing.
(I don't know why this step isn't implemented in the /usr/bin/dropbox script. It makes no sense that it supports installing the daemon, but not connecting it to your account and initializing, even though the daemon can do it, half ways at least).

Kill the previous command with Ctrl-c. Whenever you need, you can start Dropbox with
dropbox start
Type 'dropbox help' to get more information using the command line [4].

In Fedora, Dropbox will now automatically start when you login to a desktop session, but not on the command line, e.g. remotely via SSH. To make it start also in bash, add the following to your .bash_profile:
# start Dropbox
if [ "$SHLVL" = 1 ]; then
    sessions=$(who | grep -c "$USER")
    if [ $sessions -eq "1" ]; then
       [ -x /usr/bin/dropbox ] && /usr/bin/dropbox start
Here is the explanation for the above script. It will only run when the SHLVL variable is 1, which means it will prevent from starting several times in nested bash threads (starting bash from within bash). It will also only start if this is the first and only session where the user is logged in. Then it checks for existence of the Dropbox start script and starts it. If for some reason the checks don't work and Dropbox is already started, it is no big deal. Dropbox will print a warning that it is already running.

To stop Dropbox when logging out from the last session, add the following to your .bash_logout:
# stop Dropbox
if [ "$SHLVL" = 1 ]; then
    sessions=$(who | grep -c "$USER")
    if [ $sessions -eq "1" ]; then
        [ -x /usr/bin/dropbox ] && /usr/bin/dropbox stop
Similarly to the start script, it will not stop Dropbox from a nested bash session and only when it is run by the last user session.

The scripts won't run when starting an xterm within a desktop session (only .bashrc is executed when starting a terminal within a desktop session, not .bash_profile nor .bash_logout).

[1] https://en.wikipedia.org/wiki/Dropbox_%28service%29
[2] http://rpmfusion.org/
[3] http://www.dropboxwiki.com/tips-and-tricks/install-dropbox-in-an-entirely-text-based-linux-environment
[4] http://www.dropboxwiki.com/tips-and-tricks/using-the-official-dropbox-command-line-interface-cli