Do a MySQL backup from a PHP script

In today's FOSSwire programming special, I'm going to be showing you how to build a script in PHP that will allow you to back up your MySQL database remotely, using just a web browser.

Why might you need such a script? Well, remote backup through a web browser could be very useful, as you will be able to do it from any machine with a web browser, where you might not have the ability to or want to log in via SSH.

First of all, I need to point out an important point. This example will have no authentication in it. That means anyone that can find this script can perform a backup of your MySQL databases should they know a valid password, or could try knocking at the door to see if they can get in. I strongly advise you restrict access to the script we're about to write using some form of authentication, be it through .htpasswd or similar, or by building a custom login system on top of this.

A few more notes:

  • This script is basic. It's not designed to be super-duper, and it's probably best as a starting point for your own script. Take it with a pinch of salt.
  • It needs a directory writable to the web server to store its files. This should most definitely be outside of the web root - i.e. impossible for anyone to download your raw database dumps in their browser.
  • Any errors that happen in the mysqldump process will get thrown right back onto the browser. Making that cleaner and better is outside the scope of this script.
  • It's ugly, but making it pretty isn't my job, or my expertise. ;)
  • It is designed for servers where MySQL runs on localhost (i.e. the same box as the web server). It can be easily adapted though, just by changing the mysqldump command string.
  • The mysqldump program must be in the PATH, or else you must supply the full path to it in $command.

And that is it. Properly in place, that script is a very simple, down and dirty interface to remotely back up your MySQL databases from a web browser.

Avatar for peter Peter Upfold - http://peter.upfold.org.uk/

Peter Upfold is a technology enthusiast from the UK. Peter’s interest in Linux stems back to 2003, when curiosity got the better of him and he began using SUSE 9.0. Now he runs Linux Mint 9 on the desktop, runs a CentOS-based web server from home for his personal website and dabbles in all sorts of technology things across the Windows, Mac and open source worlds.

Home » Articles »

Discussion: Do a MySQL backup from a PHP script

  1. # Posted on 24 January 2008 at 01:33 PM

    [...] Backup MySQL via PHP Filed under: Technology — 0ddn1x @ 2008-01-24 20:33:45 +0000 http://fosswire.com/2008/01/21/do-a-mysql-backup-from-a-php-script/ [...]



  2. # Posted on 08 February 2008 at 08:47 AM

    [...] bumped into PHP scripts like this before, but haven’t bookmarked them until now. Tags: MySQL, [...]



  3. Jason (guest)

    # Posted on 17 March 2009 at 08:05 PM

    Hey, this script looks great - but I think the page has some issues with displaying the code - it would be nice if you could link to a text file with all the code in it, or it clean it up.



  4. # Posted on 17 March 2009 at 10:58 PM

    Jason said:

    Hey, this script looks great - but I think the page has some issues with displaying the code - it would be nice if you could link to a text file with all the code in it, or it clean it up.

    Good catch; fixed it up, sorry about that.



  5. Lil (guest)

    # Posted on 20 May 2010 at 09:06 PM

    Works great!



  6. Ed (guest)

    # Posted on 14 June 2010 at 10:42 AM

    In PHP5 on my local system, I can't make this script work! I run it, and I get the following errors: "sh: mysqldump: command not found Notice: Undefined index: bzip in /Applications/XAMPP/xamppfiles/htdocs/xampp/backup.php on line 94"



  7. norm (guest)

    # Posted on 06 July 2010 at 05:56 AM

    ED,

    error appears to be stating that the local server you are using, XAMPP, does not have a required package installed, i.e. bzip. if you install it you should be fine.



  8. AndyFletcherp (guest)

    # Posted on 12 April 2011 at 10:16 AM

    Hi - I am definitely delighted to discove this. Good job!



  9. Kevin (guest)

    # Posted on 18 June 2011 at 09:19 PM

    I use this script found on github, works great. I placed the script out of web accessible path and setup a cron to run the code..

    https://github.com/ryanharkins/mysql-php-backup/tree/



Home » Articles » Do a MySQL backup from a PHP script