Backup of MySQL database with a PHP script

October 28, 2012 PHP As Is, PHP Recipes

Sometimes you need to make a mysqldump backup without having SSH access or PHPMyAdmin. It can be done with PHP by means of SHOW TABLES/SHOW CREATE TABLE mysql queries. Below is an improved solution proposed here.

Improvements:

  • Got rid of memory_limit – writing to file on every iteration
  • gzip support – you don’t have to download huge uncompressed .sql
  • PHP notice fixed
  • added IF EXISTS to DROP TABLE
  • ereg_replase -> str_replace
  • added set_time_limit(0) to work with large DB’s

In order to make a dump:

  • change mysql login/password/database on this line:

  • upload the file to your hoster
  • Create “backup” folder with write permissions to web server. If you’re not sure what permissions to set, just set 777 – the folder should be deleted after you’re done with backup
  • Navigate to host.com/backup.php – assuming no rewrite urls
  • Download your backup from “backup” folder

Here is the script:

vijay

April 12, 2013 at 5:10 am

Awesome work. I m using this since i have seen this friest time. Nice to see improment using delay statements.
Reply

Felipe

May 24, 2013 at 4:45 pm

Thanks for the script! Works great, I only had to add a "SET FOREIGN_KEY_CHECKS = 0;" to the beginning because my tables has FKs.
Reply

Pink

May 25, 2016 at 5:08 am

I might be beanitg a dead horse, but thank you for posting this!
Reply


Leave a Reply

Your email address will not be published