# php sql backup script



## amanxman (Mar 28, 2006)

Hey,

I am trying to backup a sql database in PHP, with either of these scripts:


```
<?php
  // Enter Database access details
  $host= 'dbxx.oneandone.co.uk';
  $user= 'dbxxxxxx';
  $pass= 'xxxxxxxx';
  $db=   'dbxxxxxxx';

  // Instructing the system to zip and store the database
  system(sprintf(

    'mysqldump --opt -h%s -u%s -p%s %s | gzip > %s/dumpDB.sql.gz',
    $host,
    $user,
    $pass,
    $db,
    getenv('DOCUMENT_ROOT')
  ));
  echo '+DONE';
?>
```
or


```
<?php

$host = "dbXX.oneandone.co.uk";
$db = "dbXXXXXXXX";
$dbuser = "pXXXXXXX";
$dbpw = "XXXXXXX";

MYSQL_CONNECT($host, $dbuser, $dbpw) or die ( "<H3>Database Server could not be reached</H3>");
MYSQL_SELECT_DB($db) or die ( "<H3>Database not Available</H3>");

$path = getenv('DOCUMENT_ROOT')."/DB_backup";
$result = MYSQL_QUERY("SHOW TABLES");
$numrow = MYSQL_NUM_ROWS($result);

for($i = 0;$i < $numrow;$i++) {
  $table = MYSQL_RESULT($result,$i);
  echo "$table ... ";
  system(sprintf("mysqldump --opt -h $host -u $dbuser -p$dbpw $db $table | gzip > %s/$table.sql.gz",$path));
  echo "DONE\n\n";
}

MYSQL_CLOSE();

?>
```
They connect to the database fine, and complete the task, however no backup file is created on the server.

The first should place it in root, and the second within DB_Backup folder. However, nothing is placed there after running the scripts.

They are scripts provivded by the host, so should work fine; but something's not right... any ideas why the gzip files aren't being saved would be most appreciated.

Many thanks


----------



## MMJ (Oct 15, 2006)

Put an echo before system() to see what output mysqldump is returning, it might be encountering an error.


----------



## amanxman (Mar 28, 2006)

Hey,


```
echo (system(sprintf("mysqldump --opt -h $host -u $dbuser -p$dbpw $db $table | gzip > %s/$table.sql.gz",$path)));
```
or

```
echo (system(sprintf(

    'mysqldump --opt -h%s -u%s -p%s %s | gzip > %s/dumpDB.sql.gz',
    $host,
    $user,
    $pass,
    $db,
    getenv('DOCUMENT_ROOT')
  )));
```
Make no different at all -- nothing new is echoed/outputted at all.

Like the system() is doing nothing

Confused....
Cheers


----------



## amanxman (Mar 28, 2006)

Also trying a cron job:


```
date=`date -I` ; mysqldump -a -u XXXX -pPPPP YYYYYY | gzip > /var/www/vhosts/domain.com/database$(date +%g%m%d_%H%M).sql ; mutt -s "Backup ($date)" -n -a /var/www/vhosts/domain.com/database$(date +%g%m%d_%H%M).sql [email protected] < /dev/null
```
But that doesn't seem to be doing anything at all; I'm not even receiving an error messages through to my email! At least I should receive error messages!

Cheers


----------



## MMJ (Oct 15, 2006)

If nothing is outputted than that is your problem; system function isn't working right.

Post output of following code:


```
vardump(system('ls'));
```


```
vardump(shell_exec('ls'));
```


----------



## amanxman (Mar 28, 2006)

Where do I need to put that code to get output sorry?

Cheers


----------



## MMJ (Oct 15, 2006)

Put it in any PHP file on your web host, open that page with your browser and see what the output is.


----------



## amanxman (Mar 28, 2006)

Hey,

echo (vardump(shell_exec('ls')));
echo (vardump(system('ls')));

or

vardump(shell_exec('ls'));
vardump(system('ls'));

Outputs absolutly nothing; blank page.

Cheers


----------



## MMJ (Oct 15, 2006)

Well then talk to your host, tell them you can't use shell commands.


----------



## amanxman (Mar 28, 2006)

Ah... cheers; will shout at host...

Much appreciated MMJ


----------

