Last Updated: February 25, 2016
·
2.193K
· djuki

Run Dropbox on shutdown with bash script - Ubuntu

You want to sync your documents, images or even your project source code between your office and home computer and like every lazy developer you want to do it automatically,

Tool we need for this job is DropBox. Dropbox will keep all our files in the cloud, so if your PC dies your files will be safe.

I don't want that dropbox runs all the time, bitching my hard drive and internet. I want to run DropBox at the end of a day, when i leave my office.

First lets make sure you have installed Dropbox deamon. In terminal Type: dropbox status. If Dropbox is not started output will be Dropbox isn't running!. If Dropbox running but there is nothing to be synced at the moment output will be Up to date. If Dropbox is not installed install dropbox via official dropbox repository.

Install Dropbox

Add Dropbox’s repository key

sudo apt-key adv --keyserver pgp.mit.edu --recv-keys 5044912E

Add Dropbox’s repository

sudo add-apt-repository "deb http://linux.dropbox.com/ubuntu $(lsb_release -sc) main"

update and install Dropbox

sudo apt-get update && sudo apt-get install nautilus-dropbox

Create shutdown shell script

Create schell with your favorite editor

sudo nano /etc/init.d./dropbox-shutdown

#!/bin/bash


DBUSER=yourusername


STATUS=$(sudo -u $DBUSER dropbox status)

if [ "$STATUS" == "Dropbox isn't running!" ]; then
    START=$(sudo -u $DBUSER dropbox start)
fi

COUNT_DONE=1
while true
do
    STATUS=$(sudo -u $DBUSER dropbox status)
    if [ "$STATUS" == "Up to date" ]; then
            COUNT_DONE=`expr $COUNT_DONE + 1`
            if [ $COUNT_DONE -gt 10 ]
            then
                    STOP=$(sudo -u $DBUSER dropbox stop)
                    break;
            fi
    fi
done
exit 0

Before save change DBUSER with your ubuntu user name. DBUSER=yourusername. This script will run dropbox, wait until dropbox sync all files and then exit.

Now we need to create symbolic link.

cd /etc/rc0.d
ln -s ../init.d/dropbox-shutdown K01DropboxShutDown

This symbolic link will be called when ubuntu shutdown system. You can change priority by changing 01 number in link name.

You need to create this shell script and symbolic link on every machine which use/change your files at the DropBox. When every machine is shut down it will send changes to DropBox.

Now lazy dev is happy.

Also we can run this script before we start to work. I do not recommended calling the same script on the ubuntu system start-up, because start-up will be slow, and if script fails you can be in big problem. So my choice is to run script manually when I need to. That is not every day, just sometimes when I work at home.

We can call from terminall:

bash /etc/init.d/dropbox-shutdown

Too long, so lets make a alias:

sudo nano ~/.bash_aliases

Add this line at the end of this file, or create this file and add more aliases to speed up your work.

alias dsync="bash /etc/rc0.d/K01DropboxShutDown"

Now from terminal we can simply call

dsync

Conclusion

Do not do anything that your computer can do for you. Automate everything.

Now all I have to do is to call dsync command at the office pc when I change files from second location (home pc).