I like to automate things! There’s this great feeling you get when you set things up to run without manual intervention. It just makes you feel “powerful”. I have scripts and code pieces to backup stuff like Microsoft SQL Server Databases (see my post on Backing up SQL Server to Dropbox), MySQL Databases, entire cloud machines, entire websites, and/or selected folder(s) within a directory.
Dropbox is a file hosting service that offers cloud storage, file synchronization, and client software. In brief, Dropbox allows users to create a special folder on each of their computers, which Dropbox then synchronizes so that it appears to be the same folder (with the same contents) regardless of the computer it is viewed on.
Dropbox is currently the best free cloud storage service. It is extremely easy-to-use and even allows for data syncing to devices like smartphones and tablets.
Let us explore how the power and flexibility of Dropbox can be enjoyed for website administrators. This tutorial will show you how to:
- Install and configure Dropbox on your server. I will demonstrate this using Ubuntu Server 12.04
- Set your website folder (usually /var/www) as the synced folder on the server. The same principles will apply if your website lives on a different folder.
- Simplify your server using Dropbox selective sync.
Install and configure Dropbox on Ubuntu Server 12.04
Step 1: Go into your local Dropbox folder and create a folder called “Server” or whatever you prefer. But if it’s different you’ll need to remember that later.
Step 2: Get a command line session with your server via SSH. Then use this command to check if you are on 32 or 64 bit.
uname -m
If it says x86_64 then download the 32-bit version from the official website:
wget -O dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86"
If it says i686 then download the 64-bit version from the official website:
wget -O dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86_64"
Step 3: Extract the archive just downloaded with the command below:
tar -zxvf dropbox.tar.gz
Step 4: Run the Dropbox client daemon with this command:
~/.dropbox-dist/dropboxd
Since your server has not been linked to any Dropbox account yet, the daemon will keep displaying a notification message every few seconds.
The message will be something like this:
Copy and paste the link in the message to a web browser using any computer. This will start the process of linking your Ubuntu Server to your Dropbox account. You will be asked to authenticate with your username and password. If the authentication is successful, you will see the message “Client successfully linked, Welcome!”. And the notification message containing the authorization link will stop printing. The daemon will also automatically create a Dropbox folder in the home directory of the current logged in user. Press CTRL+C to terminate the Dropbox deamon process.
Step 5: Create a script for service management functions like starting and stopping Dropbox. We will name the script “dropbox”. Use the following commands:
sudo touch /etc/init.d/dropbox sudo nano /etc/init.d/dropbox
Copy the following text into the script file just created:
#!/bin/sh # dropbox service # Replace with linux users you want to run Dropbox clients for DROPBOX_USERS="user1 user2" DAEMON=.dropbox-dist/dropbox start() { echo "Starting dropbox..." for dbuser in $DROPBOX_USERS; do HOMEDIR=`getent passwd $dbuser | cut -d: -f6` if [ -x $HOMEDIR/$DAEMON ]; then HOME="$HOMEDIR" start-stop-daemon -b -o -c $dbuser -S -u $dbuser -x $HOMEDIR/$DAEMON fi done } stop() { echo "Stopping dropbox..." for dbuser in $DROPBOX_USERS; do HOMEDIR=`getent passwd $dbuser | cut -d: -f6` if [ -x $HOMEDIR/$DAEMON ]; then start-stop-daemon -o -c $dbuser -K -u $dbuser -x $HOMEDIR/$DAEMON fi done } status() { for dbuser in $DROPBOX_USERS; do dbpid=`pgrep -u $dbuser dropbox` if [ -z $dbpid ] ; then echo "dropboxd for USER $dbuser: not running." else echo "dropboxd for USER $dbuser: running (pid $dbpid)" fi done } case "$1" in start) start ;; stop) stop ;; restart|reload|force-reload) stop start ;; status) status ;; *) echo "Usage: /etc/init.d/dropbox {start|stop|reload|force-reload|restart|status}" exit 1 esac exit 0
You could also grab the above script from github here.
Now edit the script and replace “user1 user2” with an Ubuntu Server username (not your Dropbox account username).
Make the script executable and enable Dropbox to start up automatically on boot using the following 2 commands:
sudo chmod +x /etc/init.d/dropbox sudo update-rc.d dropbox defaults
Now, you can easily control the Dropbox client like any other Ubuntu service (if Dropbox hasn’t started, start it):
sudo service dropbox start|stop|reload|force-reload|restart|status
How to Check The Dropbox Status with a Python Script
Step 6: Download the dropbox.py script and make the file excutable using the following 2 commands:
wget -O ~/.dropbox/dropbox.py "http://www.dropbox.com/download?dl=packages/dropbox.py" chmod +x ~/.dropbox/dropbox.py
Now you can easily check the status of the Dropbox client with following command:
~/.dropbox/dropbox.py status
Statuses could be “Idle” or “Uploading” etc.
To get more help for dropbox.py use the following command:
~/.dropbox/dropbox.py help
Set Your Website Folder as a Synced Folder in Dropbox
Step 7: In Step 1 above, you created a folder called Server within your Dropbox folder. Let’s assume that you want this folder to act as your www root folder. Edit /etc/apache2/sites-available/default and replace /var/www/ with /home/
If you prefer to add /var/www/ to your Dropbox, and keep it synced, create a symlink inside ~/Dropbox/ to point to /var/www using this command:
cd ~/Dropbox ln -s /var/www www
You might want to chown the www folder so as to add files to it without using root.
Simplify Your Server Using Dropbox Selective Sync
Right now Dropbox is syncing all the folders in your Dropbox account to your Ubuntu Server. For me that’s not good so let’s pare it down using selective sync.
Step 8: You will need to call the ‘exclude add’ command for each top level folder you don’t want to sync. If you only just installed Dropbox, I’d suggest starting by excluding your biggest folder to save Dropbox and your server some work. If you want to sync only the “Server” folder we created earlier, proceed like this (assuming the dropbox.py script was stored in your home directory… I however, recommend keeping scripts like this in a “utils” folder):
cd ~/Dropbox ~/dropbox.py ls -> Photos Projects Public Server Work ~/dropbox.py exclude add Projects ~/dropbox.py exclude add Photos ~/dropbox.py exclude add Public ~/dropbox.py exclude add Work ~/dropbox.py ls -> Server
There you go… the only synced folder is now the “Server” folder.
Excellent post!
When I visit https://raw.github.com//gist/2347727/108fc8af551cb4fdf7cdd08b891a45f405d283dc/dropbox I get a 404
WIll this also work for centos 7?
Hi Stanley,
Since the github link seems to have died, just copy and paste the script I provided. That was actually the reason I posted the code on here (in case the link dies or something).
As for CentOS 7, I believe the code will work as well. However, I haven’t tried yet.
Thanks.
Note you have the definitions switched on top: “If it says x86_64 then download the 32-bit version…” Feel free to correct and delete my comment.