How to automatically backup MySQL database to Dropbox

Ever wanted to backup your MySQL database to Dropbox automatically. Well, there is a nice portable solution with Dropbox Uploader. Dropbox Uploader is a BASH script which can be used to upload, download, delete, list files, move, copy and much more from Dropbox. It’s written in the BASH scripting language and only needs cURL to work. I’ve tested it on my Windows/Cygwin environment at it works like a charm.

Installation

It is a very small script so quick to clone from the repository with git:

git clone https://github.com/andreafabrizi/Dropbox-Uploader.git

or, download with curl. I myself downloaded using curl:

curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh

Once downloaded change the appropriate permissions and run it.

$chmod +x dropbox_uploader.sh
$./dropbox_uploader.sh

The first time you run dropbox_uploader (dropbox_uploader.sh), you’ll be guided through a wizard in order to configure access to your Dropbox account. This configuration will be stored in ~/.dropbox_uploader.

drop-box-upload

Once the initial configuration is done you can upload your MySQL database file as below. The format for the BASH script is:

$ ./dropbox_uploader.sh [PARAMETERS] COMMAND...

An example is given below. ‘file-location’ is the local directory path where the MySQL backup sql file is located. /myfiles/db.sql is the remove Dropbox directory location with the filename.

$ ./dropbox_uploader.sh upload /file-location/db.sql /myfiles/db.sql

You can add the above line to your CRON job to automatically upload your MySQL database file from your preferred location. Note that you will need to mention the full path to the ‘dropbox_uploader.sh’ file in your CRON (e.g. /path/to/dropbox_uploader.sh).

I’ve not mentioned on how to backup your MySQL database as there are many options one could take, like using ‘mysqldump‘ or some other method.

A simple example to dump a complete database is to use the following.

mysqldump -u [uname] -p[pass] db_name > db_backup.sql

Or all the databases. Note that this file can be quite huge.

mysqldump -u [uname] -p[pass] --all-databases > all_db_backup.sql

Note that if the file is bigger than 150Mb the file is uploaded using small chunks (default 4Mb); in this case a . (dot) is printed for every chunk successfully uploaded and a * if an error occurs (the upload is retried for a maximum of three times). Only if the file is smaller than 150Mb, the standard upload API is used, and if the -p option is used the default curl progress bar is displayed during the upload process.

A detailed list of commands is given here with the various options.