PHP script for backup of mysql database

sandykadam

202.***.***.***
1,130 days ago

PHP script for backup of mysql database

Hi friends,

Following is script by which you can take database dump by PHP script. Class is ready to make the full backup of a mysql database, and can compress the dump in gzip format!

PHP code:


<?php
//if t=1 dumps the data, otherwise the structure
$data=$_GET['t'];
require("class_mysqldump.php");

//Instantiate the class: host name, user name, and password
$dump = new MySQLDump("localhost", "root", "");

//If you want to compress the output uncomment the follow line
//$dump = new MySQLDump("localhost", "root", "", False);

if ($data=="1") {
$dump->dumpDatabaseData("dbname", $filename, 100);
//If you don't want binary fields saved in hexadecimal
//format uncomment the follow line
//$dump->dumpDatabaseData("nomedb", $filename, 100, False);
}
else {
//dump the structure
$dump->dumpDatabaseStructure("nomedb", $filename);
}

//send file to standard output
header ('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$file=fopen($filename,"r");
fpassthru($file);
fclose($file);

//delete temporary files
unlink($filename);
?>

swajain

122.***.***.***
1,014 days ago
gzwrite(): supplied argument is not a valid stream resource

swajain

122.***.***.***
1,009 days ago

to take mysql data base backup from php script

//file name backupnew.php

$database="dbname";
define("DB_NAME", $database);

global $result;
$result="";

$backupFile = $database.date("_m_d_Y").'.sql'; //backup file name
$result .= "# MySQL Data Backup of ".$database."\n";
$result .= "# This was generated on " . date("m/d/Y") . "\n\n"; //just an information

$dbhandle= mysql_connect(dbsource,dbusername,dbpass) or die ("Could not connect");
$result="";
$result1="";

$tables = mysql_list_tables($database);
for($i = 0; $i < mysql_num_rows($tables); $i++)
{
$result1="";
$table = mysql_tablename ($tables, $i);
$result .= "# Start of $table \n";
//for schema
$result .="DROP TABLE IF EXISTS `$table`;\n";
$result .= "CREATE TABLE `$table` ( \n";
$result_fld = mysql_query( "SHOW FIELDS FROM ".$table, $dbhandle );

while($row1 = mysql_fetch_row($result_fld) ) {

$result.= "`".$row1[0]."`"." ".$row1[1]." " ;
if($row1[2]=="NO")
$result.=" NOT NULL ";
if(($row1[4]!=""))
$result.=" default `".$row1[4]."` ";
if($row1[5]!="")
$result.=" ".$row1[5]." ";
if($row1[3]=="PRI")
$result1.=" PRIMARY KEY (`".$row1[0]."`),\n";
if($row1[3]=="MUL")
$result1.=" KEY ".$row1[0]."("."`".$row1[0]."`"."),\n";
$result.=",\n";
}
$result .= $result1;
$result .= "\n);\n\n";

//for schema

$query = mysql_query("select * from $table");
$num_fields = mysql_num_fields($query);
$numrow = mysql_num_rows($query);

while( $row = mysql_fetch_array($query, MYSQL_NUM))
{
$result .= "INSERT INTO ".$table." VALUES(";
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = str_replace("\n","\\n",$row[$j]);
$row[$j] = str_replace("\r","",$row[$j]);
if (isset($row[$j]))
$result .= "\"$row[$j]\"";
else
$result .= "\"\"";
if ($j<($num_fields-1))
$result .= ", ";
}
$result .= ");\n";
}

if ($i+1 != mysql_num_rows($tables))
$result .= "\n";
}
if((isset($_GET['action'])) && ($_GET['action'] == "save"))
{ ob_clean();
ob_start();
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=$backupFile");
echo $result;
ob_end_flush();
//echo "Backup Taken Successfully!!!";
exit;
}

<table border=1 align="center" cellpadding="5" cellspacing="0" bordercolor="#C6D78C">
<form name='MyForm' method='post' action='backupnew.php?action=save'>
<tr bgcolor="#FFFFFF">
<td colspan = 2 align="center" ><input type='submit' name='submit' value='Submit'class = "butt1"></td>
</tr>
</form>
</table>

dayathiyam

122.***.***.***
772 days ago
hello i am very thankful for you all for giving me guide to php code and i started coding from last one month.
thanks.....
daya

dayathiyam

122.***.***.***
772 days ago
i need a help and very fast. and question is how to access php code within html?
anyone plese help me

Bill

84.***.***.***
772 days ago

dayathiyam;641:

i need a help and very fast. and question is how to access php code within html?
anyone plese help me

Change the file extension from .html to .php, or make Apache run .html files by the PHP interpreter by adding this to a .htaccess file:

Code:

AddType application/x-httpd-php .php .html