backup database

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
josecnp1
User
Posts: 73

backup database

Post by josecnp1 »

I've got a function to make a backup of the database:
Warning: mysql_connect(): Access denied for user ''@'localhost' (using password: NO) in C:\Web\administracion\11.php on line 251 -> this is the line: mysql_connect();
the file does correctly generates and saves all data. but I this error.
the code is placed correctly in the file: blankpage.php
how could remove the error on the file it generates, it is only the first line in which it appears, the rest of the file if all data from the backup of the database appears

This is the code:

<?php
backup_tables($conn);

function backup_tables($conn,$tables = '*')
{
mysql_connect();


//$conn = mysql_connect($host,$user,$pass) or die(mysql_error());



mysql_query('SET NAMES utf8');
mysql_query('SET CHARACTER SET utf8');
// mysql_select_db($name,$link);
$return = '' ;


//get all of the tables
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
//if (empty($result)) continue;
}



//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);


$num_fields = mysql_num_fields($result);

$return.= 'DROP TABLE IF EXISTS '.$table.';';

// If you are getting errors related to fetching, uncomment the next row:
//if (empty($result)) continue;

$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";

// If you are getting errors related to fetching, uncomment the next row:
//if (empty($result)) continue;

for ($i = 0; $i < $num_fields; $i++) 
{
  while($row = mysql_fetch_row($result))
  {
    $return.= 'INSERT INTO '.$table.' VALUES(';
    // If you are getting errors related to fetching, uncomment the next row:
	//if (empty($result)) continue;
	$columns = count($row);
	for($j=0; $j<$columns; $j++)
    {
      $row[$j] = addslashes($row[$j]);
      //$row[$j] = ereg_replace("\n","\\n",$row[$j]);
	  $row[$j] = preg_replace("/\r\n/","\\r\\n",$row[$j]);
      if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
     
           if ($j<($num_fields-1)) { $return.= ','; }
        }
        $return.= ");\n";
     }
  }
  $return.="\n\n\n";

}
//save file
$filename = 'CopiaSeg-'. date("d-M-Y-H-i-s") .'.sql';

Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$filename");
echo $return;

}
?>


scs
User
Posts: 694

Post by scs »

Another option is to use mysqldump

Create a bash script and assign cronjob to do backup in specific time.


josecnp1
User
Posts: 73

Post by josecnp1 »

OK, I created this file, but I would like to replace variable: $dbhost, $dbuser, $dbpass, $dbname by using PhpMaker, or is there some alternative method?

<?php include_once "ewcfg10.php" ?>
<?php include_once "ewmysql10.php" ?>
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = 'XXXXXXXXXXXXX';
$dbname = 'aaaaaaaaaaaaa';

$backupFile = 'CopSeg-'.$dbname . date("d-M-Y-H-i-s") . '.sql';

$mime = "application/octet-stream";
header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $backupFile . '"' );

$cmd = 'C:\Web\MySQL\bin\mysqldump --opt -u '.$dbuser.' -p'.$dbpass.' '.$dbname.'';
passthru( $cmd );
?>


Post Reply