Page 1 of 1

PHP Backup script fails

PostPosted: 25. September 2010 21:30
by rdgreenlaw
Hi,

I have the following PHP code which works as expected on a Linux system with Apache, PHP and MySql installed, but fails on Windows XAMPP.
Code: Select all

<?php

// turn on error reporting
error_reporting(-1);
ini_set('display_errors', true);

// Parameters for BINGO database
$username="user";
$password="password";
$hostname="localhost";
$dbname="bingodata";

// Backup file location
$tmpdir="/home/rdg/backupdb/";

// backup file name will be /home/rdg/backupdb/mybackup.sql

$dbh = mysql_connect($hostname, $username, $password)
        or die("Connection to MSQL failed!");

$selected = mysql_select_db($dbname, $dbh)
        or die("Could not access $dbname database:" . mysql_error() );

// Connection to database is established at this point;

    $prefix = "db_";
    $sqlFile = $tmpDir."mybackup.sql";
    $creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
 
        echo "<html><title>Backup Bingo Database</title><body>";
    echo "<center>";
    echo "Bingo Reporting System<p>";
    echo "Backup Bingo Database<p>";
    echo exec($creatBackup)."<p>";
    echo "Backup created "<p>";
?>
</body></html>


This works fine on my Linux system, but XAMPP simply creates a zero length file c:/rdg/home/backupdb/mybackup.sql

Any help will be greatly appreciated.

Re: PHP Backup script fails

PostPosted: 26. September 2010 14:30
by JonB
are you getting PHP errors? -

is c:/rdg/home/backupdb/mybackup.sql inside your DocumentRoot?

you may also need an absolute path to mysqldump.exe

:?:

Re: PHP Backup script fails

PostPosted: 29. September 2010 03:16
by rdgreenlaw
JonB wrote:are you getting PHP errors? -

is c:/rdg/home/backupdb/mybackup.sql inside your DocumentRoot?

you may also need an absolute path to mysqldump.exe

:?:


Thanks for your help, JonB!

I was not receiving any PHP errors, and though the file being created is not in the DocumentRoot, it appeared to be a valid location since the file was being created when the script executed the mysqldump command. This left your last suggestion. I put an absolute path to mysqldump in the script as
Code: Select all
   $creatBackup = "c:/xampp/xampplite/mysql/bin/mysqldump.exe -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;

This resolved my problem and the backup script is now working as intended.

Roger