Tip using PHP’s system/exec command running under MAMP

For a few hours yesterday I was driven mad by not being able to get a simple system command to run at all. Let me quickly detail what it was that threatened to drive me round the bend.

The database powering a high traffic site we developed for a client had become too big to be backed up using the standard PHP method. In my case I was using Codeigniters database backup utility. All this does is put a nice wrapper around the standard approach. As an aside if you haven’t looked into Codeigniter I would recommend you do so. Compared to the other frameworks available it is one of the fastest (Cake coming in the slowest) and easiest to get going with. Escpecially in it’s 2.0 version rev where it dropped it’s support for PHP4 (finally!) it is I think a brilliant choice to build your web sites/applications on. Of course the other good approach being WordPress.

Anyway back to the tip! If (when?) your database becomes too large to backup using PHP you have to use another approach. Luckily MySQL has a backup program built into it. You can access it via the shell. It’s called ‘mysqldump’. It isn’t bothered by the restrictions that bug PHP such as a scripts memory limit or max execution time. Because of this it can backup large databases. Now to be clear the database wasn’t massive. I’m not talking terabytes but merely a few hundred megabytes.

The first and most common command that most tutorials/troubleshooting guides show is something similar to this:

What the above is saying is to run the mysqldump program along with various settings. Make sure to note that preceding the hostname, user and password there is a hyphen along with a h, u or p respectively. Also note that there is no gap.

Now this makes perfect sense and is supposed to work fine. However if you run this you will quickly find out that on a local server running via MAMP (on a MAC) it produces an empty SQL file. Well this was the question that I had to answer and of course I looked at every part of the command apart from mysqldump. I mean how could I get mysqldump wrong excluding the possibility of a spelling mistake. To make things a bit harder the actual system command returns 0 meaning everything OK.

The solution turned out to be changing mysqldump. Of course it was!

Here’s the command that gets it working under MAMP

Supply the full path to the program instead of just using the program name. That’s it folks! It should work after that barring any other errors in your command. To give credit where credit’s due I found the above solution here – http://www.adriantomic.se/development/blog-post-2/

Happy hair pulling everyone.

Leave a Reply

Your email address will not be published. Required fields are marked *