How to backup mysql using php, and shell script - the easy way -
As I was trying to find an easy way to backup a MySQL database with no ssh access to the server, there should be better ways, but i found this easy one.
You will need a Shell script, a .php script.
Ok, lets make this work
- First create a shell script with this line
mysqldump --opt -u [user_name] -p [database_name] | gzip > [backup_file].dump.gz
and name it, lets say:
the_backup_script.sh
- Now create the .php script that will call the shell script.
It will contain this:
<?php
echo exec('./the_back_script.sh');
?>
and name it, for instance
the_backup_php_script.php
Until this point this should work, just copy the two files using your ftp account in the root directory of your site.
Here comes the security issue, nobody wants that somebody discover this file and can get all you mysql database, so you have some options.
1. Create a complete server based security like shown here: http://www.pagetutor.com/keeper/http_authentication.html
2. Erase the files from your server once used, and upload them each time you may need to do a backup.
Trackback URL for this post:
If you like this article, subscribe to our full rss
Please post your question in our forum and use comments only to leave your comments about the article, thanks.













i found this very helpful.I
i found this very helpful.I am new to database especially in linux. I have a few queries
1.Can you tell me the maximum size of the table it can copy using this method.
2.Is there any other restrictions for this method.
3.How to restore the dumpfiles back to the database.
Thank in advance
with regards
Philip
Phil, first I have to tell
Phil, first I have to tell you that I am no expert with MySQL, but I will try to answer you questions the best I can.
1. I think there is no limit, just the MySQL Limit for a table, which I do not know.
2. I have not found any so far
3. You should create a script to restore, with this line in it.
mysql [database_name] < [backup_file].dump
Please read here.
Guillermo Garron
Post new comment