Dropbox Database Sync for MySQL & MAMP
Using Dropbox for web development is an easy way to share files across multiple local servers. This tutorial shows you how to setup a Dropbox database sync so you can trigger your MySQL database sync in the push of a button.
Keeping computers in a local testing environment in sync can be a nightmare. While using GIT is certainly preferential, it may not be an option for newbies of those without the needed experience or software. In those cases, creating a file and Dropbox database sync is an ingenious option. As long as you have MAMP and Dropbox on your computer, you'll be able to share your files and databases across any computer you'd like to run a local testing environment on.
We will start off assuming your testing environment is already established. If you haven't gotten to that step yet, please refer to our Setting up MAMP. MAMP (much like LAMP or XAMP) is simply an acronym for Apache, MySQL, PHP and it's important to keep that in mind while we push forward. What we're looking to address in this tutorial is your MySQL database. More specifically - We will be creating a SQL database on Dropbox then tricking MAMP into using that (instead of the default /db folder).
Dropbox is an amazing tool, even if you're not a dev nerd. You can get started by setting up a free account at http://www.dropbox.com, but put simply - Dropbox is just online "cloud" storage that is easily accessible through multiple devices. Once your account is properly setup and assuming you already have MAMP installed, we can move forward to create your local Dropbox database sync. This is what we'll be able using to ensure that databases are properly disseminated to all the computers that will be sharing the local testing environment.
Creating your Dropbox Database
Open the Terminal application that's located in your "Applications/Utilities" folder. Make sure you're MAMP server is off, then we'll be running a few commands to achieve the following tasks for files you'll be using on your local server.
- 1) Setting your local directory to Dropbox
- 2) Moving the current MAMP database to Dropbox
- 3) Removing the old MAMP database folder
- 4) Creating a symbolic link to the shared Dropbox database
Creating an Alias for Your MySQL Database Sync
Each of the above tasks are achieved with the corresponding Terminal commands below. Keep in mind that if your file structure is different, you will need to adjust the folders to suit your individual needs. As with everything in Terminal, be deliberate and take your time. Also note that the name
"MAMP_DATABASE" is arbitrary. If you prefer a different name feel free, just make sure you use the exact same name in both lines (2nd & 4th) and adhere to the normal naming rules (alphanumeric and underscores).
cd ~/Dropbox/ cp -r /Applications/MAMP/db MAMP_DATABASE rm -rf /Applications/MAMP/db ln -s ~/Dropbox/MAMP_DATABASE/db /Applications/MAMP/db
Once you've got everything setup, clear your browser cache, and start your MAMP application. If you get an error I'd suggest clicking on your PHP admin tab, since most errors will result in a missed connection there. As long as everything is up and running, give one last sync, and then we'll get next computer connected.
Initialize Your Dropbox Sync
Make sure your MAMP server is off (you obviously don't want to sync your database while it's on) then start your Dropbox application. Give a quick look to your file structure to ensure the above Terminal commands were entered properly. Just look at your
Applications/MAMP folder and make sure it matches up. You should see the small folder icon with the arrow underneath (see image) where your db folder used to be. In your Dropbox folder you should see your new database folder, which in this example would be named
MAMP_DATABASE. Since your SQL database is in your Dropbox folder, you just sync Dropbox, and you're good to go.
Adding Computers to your Dropbox Database Sync Environment
The Terminal commands we used earlier are useful in this situation, but we certainly don't want to overwrite the existing Dropbox database - We just want to use it. While these commands look similar, that is the crucial distinction between them. In this case the tasks we're trying to achieve are…
- 1) Setting your directory to Dropbox
- 2) Removing old MAMP database folder
- 3) Creating symbolic link to Dropbox folder
Each of those tasks will be achieved with the corresponding Terminal commands below.
cd ~/Dropbox/ rm -rf /Applications/MAMP/db ln -s ~/Dropbox/MAMP_DATABASE/db /Applications/MAMP/db
Enjoy Your Coordinated Local Development Setup
That's it - Not as tricky as you thought right? If you do happen to run into any issues (and if you're a newbie, you might) just make sure you take a look at your error logs and the structure of your folders. The only issue's I've seen in using this setup is the normal "conflicting copy" errors that you occasionally get with Dropbox (see image). As long as you only sync one computer at a time, this shouldn't be a problem. If you do see those errors, just make sure you delete the appropriate "conflicting" files, and you're good. If anything looks really wonky, just double check your file structure cause it may be an input error with those terminal commands.
Now throw the first person you see a high-five, because you have an AWESOME tool for your testing environment. No more importing, exporting, and ending up with spare database backups everywhere. As previously mentioned, moving to a GIT workflow (like we covered in this post > Optimizing Development Workflows) is a much better approach, but this works for beginners or if you need something quick.
If you have any questions or ideas on how to flush out this concept, please feel free to shoot us a message and we'll fold it in. If you found this tutorial helpful, please share with any of the buttons below or give us a follow on your favorite social network. As always - Thanks for stopping by and happy coding!!