Game Developer

Automatic backup to Dropbox on Raspberry Pi

Category : Tutorials · by Mar 4th, 2014

Recently, I have been working a lot on Raspberry Pi server and as it had more functionality and useful data, problem of automatic backup became quite critical. As I am an active user of SkyDrive (OneDrive) I wanted to do a backup there. But after quick search it appears quite hard to do (if not impossible), so I decided to use Dropbox instead.

The first tutorial I found was this one from Dave Hope. Quite a nice script, doesn’t use any dependencies, but unfortunately I haven’t been able to run it (probably because of my lack of experience with Linux in general). So I decided to use Andrew Fabrizi’s Dropbox Uploader and write my own script.

So there are three steps we have to do:

  1. Download and setup Dropbox uploader script
  2. Write script to archive files we want to backup and upload it to Dropbox
  3. Add it as a scheduled task

1. Ok, so first ssh to your Raspberry Pi and download Dropbox uploader script to your home directory.

sudo wget -P ~

The only dependency this script is using is Curl which is installed on Raspbian by default, but just to be 100% sure run this

sudo apt-get update
sudo apt-get install curl

Then make it executable and run the script

sudo chmod +x ~/
bash ~/

Follow on screen tutorial to get the App key, App Secret and Permission type from your Dropbox account and type them into script. Then finally copy link the script gives you into browser, click Allow button and your Dropbox uploader script should work fine ! You can check it by trying to lauch command like this

~/ upload ~/ /

Script should successfully upload itself into Dropbox.

2. Next step is to write script that would archive the files/folders you want to backup into a single file and than upload it to Dropbox. My script currently looks like this:

#!/usr/bin/env bash
BACKUP_SRC="/home /var/www /etc /root /tmp/databases.sql"
BACKUP_DST="/var/backups/$(date +"%Y")"
DROPBOX_DST="/Backup/$(date +"%Y")"
MYSQL_PASS="your MySQL pass"

NOW=$(date +"%Y.%m.%d")

echo -e "Backing up MySQL databases"
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "/tmp/databases.sql"

echo -e "Archiving folders"
sudo mkdir -p $BACKUP_DST
sudo tar -zcf "$DESTFILE" --exclude="$BACKUP_EXСLUDE" $BACKUP_SRC

echo -e "Uploading archive to DropBox"
~/ upload "$DESTFILE" "$DROPBOX_DST/$NOW.tgz"

echo -e "Finished"

Create text file and copy/paste this script

nano ~/

Now it is important to change some variables so this script better suits you.
First of all, if you don’t have MySQL database or don’t want to backup it, you have to remove /tmp/databases.sql from BACKUP_SRC and comment out this line by putting # in front of it.

mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "/tmp/databases.sql"

If you have WordPress website on your Pi, than all posts/comments/etc are stored in MySQL databases, so you better to backup it.

BACKUP_SRC contain all folders you want to backup. I have put folders where by default apache websites and most OS settings are stored.

BACKUP_EXCLUDE contains folders which are inside BACKUP_SRC folders but you wont to back it up. For example I want to backup my home folder /home/pi but don’t want to backup /home/pi/Downloads folder which could be quite big.

BACKUP_DST contains folder where backup would be stored locally. If you don’t want store all backups locally, than add this line into the end of the script.


Note that I am creating folder with the number of the year. This is to help me manage all backups in the future. If you want you can also add month folder like this.

BACKUP_DST="/var/backups/$(date +"%Y")/$(date +"%m")"

DROPBOX_DST is similar to BACKUP_DST and tells where backup would be stored in DropBox.

MYSQL_SERVER, MYSQL_USER and MYSQL_PASS are needed for MySQL database backup.

NOW is the file name for backup archive we want to create. It is good idea to use current year, month and date for it.

Finally rest of the script is straightforward. It would create sql script with all databases, archive all folders and files and finally send it to dropbox.

After you edit variables, make script executable and try to run it.

sudo chmod +x ~/
bash ~/

3. If everything works fine the last thing we have to do is to setup scheduled task to run the script. We would do so using crontab. Open crontab editor…

crontab -e

…and add this line into it.

0 5 * * 1 ~/

This would run our script every Monday 5am. I don’t feel it is necessary for me to explain crontab format, as there is already a lot of good tutorials for this, for example this one from The Geek Stuff.

Well, I hope it works and you have your scheduled script running regularly to backup your precious data.


(13) comments

Robert Shaw
5 years ago · Reply


Thanks for the script. Very handy!

One correction on the backup script shown in the page.

sudo tar -zcf “$DESTFILE” –exclude=”$BACKUP_EXLUDE” $BACKUP_SRC

should be

sudo tar -zcf “$DESTFILE” –exclude=”$BACKUP_EXCLUDE” $BACKUP_SRC

If you don’t correct this, then the $BACKUP_SRC will be used as the –exclude= option and there will be no files backed up.


Rich McClellan
5 years ago · Reply

Everything goes swimmingly until I get to running the script.
Then, I get

bash ~/
/home/pi/ line 5: syntax error near unexpected token `newline’
/home/pi/ line 5: `’

Any ideas?

    Alex Belezjaks
    5 years ago · Reply

    This looks strange. script should not have syntax error at line 5. Check your file looks exactly like this one here

    Maybe it got corrupt when downloaded?

    Also, it might be mismatch of windows and linux newline characters. Have you copied this script from windows machine though copy-paste/ftp?

    4 years ago · Reply

    I was getting the same error message as you.

    pi@raspberrypi:~ $ ./
    ./ line 5: syntax error near unexpected token `newline’
    ./ line 5: `’

    I opened the file (sudo nano ./ and saw that using Alex’s command above had not mirrored it properly.

    In order to get it correctly I used the command:

    git clone

    This cloned the entire project to a folder called Dropbox-Uploader. In there, the file was correctly copied.

      4 years ago · Reply

      This helped! Make sure you run the script in the newly created directory! 🙂

      bash ~/Dropbos-Uploader/

5 years ago · Reply

You can easily run original Dropbox client using Exagear, here is simple instruction how to fo that:

    Alex Belezjaks
    5 years ago · Reply

    That’s a nice program! Can it be fully operational in terminal mode?

5 years ago · Reply

Hi.. Thanks for the detailed steps. Was able to get this working fine.
May I please know how to get the uploaded files back into the raspberry pi folder.?

    Alex Belezjaks
    5 years ago · Reply

    You mean how to sync Dropbox files to RaspberryPi? You will need to write new script for that, dropbox uploader script can upload/download/move/delete files with the right commands. Check manual for all available commands here

    Or you mean how you can access all uploaded files on RaspberryPi locally? In this case they are stored in BACKUP_DST folder, /var/backups/ by default.

5 years ago · Reply

Thanks for the reply. I was having problem while downloading the file from dropbox

was using this command and got error

./ download abc.tar /
> Error writing file /abc.tar: permission denied

Now I referred to the info in
and used correct command

./ download /abc.tar

Thank you very much.

4 years ago · Reply

Hi Alex Thanks for a great script. I got it to work and it does back up the folder that im pointing on but unfortunately there isn´t any files in the tgz-file in dropbox. When I unpack the file it has the correct folder-names but I cannot see any files in the folder. The folder that im trying to backup has another ownership than root. Could that be the problem?

    Alex Belezjaks
    4 years ago · Reply

    Yes, permission is probably a problem. I guess you can either try to change permissions or run script with sudo?

Leave a Reply

Your email address will not be published.