1toN

Backup any MySQL to Dropbox!

Introduction

Featured

Shell

Backup any MySQL to Dropbox!

Posted by Rafael Lopes on .

Very affordable method to backup your MySQL database and sync the dump file directly to your Dropbox® account. Grab some pistachios because this is gonna be fun!

By the way, this procedure allows not only your mysql dumps, but everything you want to backup on your server. I will use a database backup for this example, but it’s up to you decide what content do you want to sync. You will need a SSH shell with at least those requirements working:

Requirements

  1. A Dropbox® account, click here to grab one if you don’t have it;
  2. Cronjobs enabled: you can make it without cron as well, but scheduling the backup won’t work;
  3. Any database backup tool installed, in this post I will use mysqldump;
  4. Any “downloader” app. I will use wget, but can use curl, links, aria, axel, whatever you want to download dropbox, this tool is not really necessary, as you can put the files on the server the way you want. Whatever, I will use wget to download them here;
  5. Ability to chmod+x a file and to make it executable.

Step 1: Install Dropbox on Linux

First of all, create a local bin directory to place Dropbox® binaries.

$ mkdir -p ~/bin   # Create the bin folder if not exists
$ chmod 700 ~/bin  # Set the permissions to you ONLY (rwx), this is highly recommended for security reasons

You can put this new folder to make part of your PATH in your ~/.bash_profile file so as you can use the binaries natively from the shell. Doing so is very simple: just open your ~/.bash_profile with your preferred editor and change the line containing the PATH variable, appending the new folder we just created. If $HOME/bin is already there, no action is needed and you can close the file.

From this:
PATH={whatever is here}
TODO (a.k.a. help-me)

do a sed line that search for this and automatically insert without having to open the file. Feel free to help me going down in the comments section :)


To this:
PATH={whatever is here}:$HOME/bin

Logout and login from shell to apply the new PATH change you just did. Now, download dropbox client directly from Dropbox® site, using wget. If your OS is 32bit:

$ wget -O ~/bin/dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86"

Or if you use 64bit Linux version:

$ wget -O ~/bin/dropbox.tar.gz "http://www.dropbox.com/download/?plat=lnx.x86_64"

This will download a compressed file to our newest ~/bin directory. Go there and extract the file. This will create a hidden folder, that you can see only using -a option on ls.

$ cd ~/bin
$ tar -xzvf dropbox.tar.gz
$ ls -a
.  ..  .dropbox-dist  dropbox.tar.gz

You can delete the .tar.gz file if you want. As Dropbox® binaries remains on a hidden folder, and it’s PITA have to put the dot every time you want to enter on that folder, we will make a symbolic link pointing dropbox and dropboxd to the root of ~/bin. This will make our life easier in the future.

$ ln -s ~/bin/.dropbox-dist/dropbox ~/bin/dropbox
$ ln -s ~/bin/.dropbox-dist/dropboxd ~/bin/dropboxd

If the symlinks and the PATH thing you did on .bash_profile worked, you can invoke the the daemon from the command line; as it’s the first time execution, a message will appear asking you to link your dropbox account to that machine…

$ dropboxd  #this should work!
This client is not linked to any account...
Please visit https://www.dropbox.com/cli_link?host_id=2aa2inaoespertodbcd9 to link this machine.

Copy the link and open in your browser, then login with the Dropbox® account you want to link that server.

Now that everything is linked on, you can kill the daemon with ctrl+c and run again with &, this will make the process going to background.

$ dropboxd &

This procedure will create a folder called Dropbox on your home directory, also called ~/Dropbox, and all the files on the Dropbox® account will be synced to that folder. We would like to create a folder inside there to place our dumps.

$ mkdir -p ~/Dropbox/MySQLdump

Step 2: Making a mysqldump Backup script

Those are different and no related procedures. Part 1 refers to a single Dropbox install and configuration, and this one refers to make a script to dump a database. The fact of saving the files on the ~/Dropbox/MySQLdump folder is just a mere coincidence, we will create a tiny shellscript called mydump, and place it into the ~/bin.

#!/bin/sh
NOW=`date +"%Y%m%d%H%M%S"`
USER=insert your mysql user here
PASS=insert your mysql password here
DB=insert the name of the database here

mkdir -p ~/Dropbox/MySQLdump
cd ~/Dropbox/MySQLdump/
mysqldump -h localhost -u $USER -p$PASS -c --add-drop-table --add-locks --quick --lock-tables $DB | gzip > $NOW.gz

Be careful with this file, as it contains your database password (Hah! We are lucky that ~/bin is set with 700 permissions!)

Also, don’t care if root reads it; because if someone got root, the smallest of your problems will be this file… ha-ha. Anyway, to ensure that no other users will see your mysql password wrote in plaintext, let’s chmod 700 the file.

$ chmod 700 ~/bin/mydump

Nice! Now we have a Shellscript that backups our database and place a compressed timestamped dump file in ~/Dropbox/MySQLdump, the script is placed in our ~/bin; to invoke the script, just type mydump on the terminal. Yeah! You’re doing it right!!


Step 3: Automating the backup task with cron

This is the easiest part, what we have to do is just open the crontab and input a line. To see crontab usage, check this reference, or this one. In our case, we will ask crontab to run our mydump everyday at midnight.

$ crontab -e  #this will open crontab file on your text editor

Add this line to the end of the file:

* 0 * * * mydump

Isn’t it nice and clean? You’re good to go with your automated database backup! :)

Aditional notes and info

  • There are many other methods to make backups using dropbox, you can find them here, here and here, googlin’ also finds tons of them.

  • Although this solution seems to work fine; if you have an important and vital application running on production, I suggest doing a dedicated backup structure. This solution is kinda home-brew (at least in my opinion) and I’m not responsible for any lost you have.

  • This is my first tutorial on this blog, I would appreciate your feedback on the comments. This way I can see if things are going on the right way.

user

Rafael Lopes (?)

Tech-lover, also loves photography and curiosity. AWS Cloud Ninja. What I enjoy? Learn from unknown internet blogs like this one.