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
- A Dropbox® account, click here to grab one if you don’t have it;
- Cronjobs enabled: you can make it without cron as well, but scheduling the backup won’t work;
- Any database backup tool installed, in this post I will use mysqldump;
- 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;
- 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.
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.
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:
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:
Or if you use 64bit Linux version:
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.
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.
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…
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.
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.
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
.
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.
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.
Add this line to the end of the file:
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.
Rafael Lopes (?)
Tech-lover, also loves photography and curiosity. AWS Cloud Ninja. What I enjoy? Learn from unknown internet blogs like this one.