Alan T. Miller

PHP web developer among other things...

about this picture

Alan Miller's blog for a Brave New World!

Blog PHP Life and Development MySQL dumps with the PHP CLI (Command Line Interface)

MySQL dumps with the PHP CLI (Command Line Interface)

PHP application to automate running mysqldump with PHP's CLI (Command Line Interface)
Created: January 22, 2010

I am sure many UNIX purists out there might scream that I could use a straight shell script to accomplish the same thing. I agree, of course I could. But I am a PHP developer and I tend to like to do things in PHP. To each his own I say. With that said, the application described here is probably best suited for small to medium sized databases running on moderate hardware. Anyhow, the application consists of two classes which are both included in the source code and in this article.

It should probably be said that this application was written for a UNIX / Linux (LAMP) environment. It won't work for you if you are not running a version of UNIX or Linux. Of course you could modify the script to suit your needs but it currently relies on common unix utilities so if you are looking to dump your databases on a Windows system, with slight modifications, this application can work for you too.

Code

You can download or view the source code by clicking the appropriate link below. You will find all three classes mentioned above in the file named 'class.xml.sitemap.generator.php'

View
Download

Usage

As with most PHP scripts, you must include the file which contains the classes, in this case, you first instantiate the class dump_databases and pass it a few configuration variables for your system as shown below:

Being that this script was designed to be ran on the command line you will notice the script interpreter cal at the top of this script. This is what allows you to execute the script as you might any other shell script. Be sure you don't forget to include it in your own script. Of course you can always execute the script manually with the 'php' command.

Next, you need to add the database information for those database you plan to dump. This is accomplished by instantiating the 'database' class while passing the constructor three arguments: the name of the database, the database username and password as shown below. You can add as many databases as you like (or as many as your system can handle I should say). Once you have added the databases you want to dump, the only thing left is to call the process() method which will take care of the rest.

Now save your file, make sure your file is executable, then simply run the script as shown below:

Not withstanding any permission issues and assuming you entered your database information correctly, you should see output similar to the above, you should also receive an email at the address specified for the email administrator and you will find your database dump files zipped up in the database dump directory you specified when creating the initial object.

Full source code displayed below:

Comments

There are currently no comments on this article. You could be the first!

Leave Comment