Re: using php to backup mysql database?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Apr 4, 2008 at 11:27 AM, paragasu <paragasu@xxxxxxxxx> 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!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux