Re: using php to backup mysql database?
| From: | Daniel Brown | Date: | Fri, 04 Apr 2008 18:42:21 +0000 |
| Subject: | Re: using php to backup mysql database? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-272540@lists.php.net to get a copy of this message | ||
On Fri, Apr 4, 2008 at 11:27 AM, paragasu <paragasu@gmail.com> wrote:
> anyone know how to backup a mysql database using PHP?
> i want to write one php function to allow admin to download the mysql
> database backup (mysqldump maybe?) ..
> and how to restore the database backup from web based admin?
>
> i am using a shared server and the php scripts executed under the
> www-data user..
If you have shell access and/or can run system() calls from PHP
(and presuming it's a *NIX-like system), you could try something like
this:
<?php
// mysql-backup.php
$database = "your-db-name";
$username = "your-db-username";
$password = "your-db-password";
$stamp = time();
ignore_user_abort(1); // Continue no matter what.
exec('mysqldump -u '.$username.' -p'.$password.' '.$database.'
>
./mysql_'.$stamp.'sql',$ret,$err);
// Handle $ret and $err as you'd like, if you want.
exec('tar -cf mysql_'.$stamp.'.tar mysql_'.$stamp.'.sql',$ret,$err);
// Handle $ret and $err as you'd like, if you want.
exec('gzip -9 mysql_'.$stamp.'.tar',$ret,$err);
// Handle $ret and $err as you'd like, if you want.
echo "<a href=\"mysql_".$stamp.".tar.gz\">Download MySQL
Backup</a>";
?>
Doing any sanity, extension, or even concatenation of commands to
a single line is up to you.
--
</Daniel P. Brown>
Ask me about:
Dedicated servers starting @ $59.99/mo., VPS starting @ $19.99/mo.,
and shared hosting starting @ $2.50/mo.
Unmanaged, managed, and fully-managed!