Backup of MySQL database with a PHP script

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:
[list style=”upper-alpha tick”]
[list_item]Got rid of memory_limit – writing to file on every iteration[/list_item]
[list_item]gzip support – you don’t have to download huge uncompressed .sql[/list_item]
[list_item]PHP notice fixed[/list_item]
[list_item]added IF EXISTS to DROP TABLE[/list_item]
[list_item]ereg_replase -> str_replace[/list_item]
[list_item]added set_time_limit(0) to work with large DB’s[/list_item]
[/list]

In order to make a dump:

[list style=”upper-alpha tick”]
[list_item]change mysql login/password/database on this line:

[/list_item]
[list_item]upload the file to your hoster[/list_item]
[list_item]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[/list_item]
[list_item]Navigate to host.com/backup.php – assuming no rewrite urls[/list_item]
[list_item]Download your backup from “backup” folder[/list_item]
[/list]

Here is the script:

  • vijay

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

  • Felipe

    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.

  • Pink

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

Leave a Reply

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