...

View Full Version : Insert back up database in a folder



newphpcoder
01-26-2012, 06:43 AM
Hi..

I've seen code for back up database but when I run the code the database was backup outside the folder. I want to put the back up database inside the folder

here is the code:


<?php
include 'config.php';
backup_tables('localhost','root','','payroll');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}

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

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

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\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
$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd().$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

// $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}


$smarty->display('header_cat.tpl');
$smarty->display('backup.tpl');
$smarty->display('footer.tpl');
?>


Thank you in advance

jmj001
01-26-2012, 06:52 AM
if it was me, i'd specify full path to where i want the file saved


$myfoldername = $_SERVER['DOCUMENT_ROOT']."/path-to/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

I wouldn't use getcwd()

oh and make sure the path to the folder is writable

newphpcoder
01-26-2012, 07:00 AM
I try your suggestion, but still it was save outside the folder.

For your reference i used Xampp, htdocs, and my php file was in the system_php folder and i want to put db backup in backup_DBPayroll where is in htdocs folder also.

Thank you

jmj001
01-26-2012, 07:07 AM
please paste your updated code here

also put the following in the file and run it and paste the result here


echo __FILE__;

it will show the full path to the script you are running

newphpcoder
01-26-2012, 07:15 AM
Here is the revise code:


<?php
include 'config.php';
backup_tables('localhost','root','','payroll');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}

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

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

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\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
// $myfoldername = "backup_DBPayroll";//your folders name
//$handle = fopen(getcwd().$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
$myfoldername = $_SERVER['DOCUMENT_ROOT']."/path-to/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

echo __FILE__;


// $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}

$smarty->display('header_cat.tpl');
$smarty->display('backup.tpl');
$smarty->display('footer.tpl');
?>



the output from echo __FILE__;
C:\xampp\htdocs\system_php\backup_db.php


Thank you

jmj001
01-26-2012, 08:18 AM
you needed to change the path-to bit to the actual path to your folder...


$myfoldername = $_SERVER['DOCUMENT_ROOT']."\backup_DBPayroll\";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

replace the changes with these listed above and it should work...

this is assuming that the folder backup_DBPayroll does already exist and is writable

newphpcoder
01-26-2012, 08:20 AM
Actually from my source here is the actual code:




$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd()./.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

but i got an error: Parse error: syntax error, unexpected '/' in C:\xampp\htdocs\payroll\backup_db.php on line 58

so I remove /



thank you

newphpcoder
01-26-2012, 08:24 AM
you needed to change the path-to bit to the actual path to your folder...


$myfoldername = $_SERVER['DOCUMENT_ROOT']."/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

replace the changes with these listed above and it should work...

this is assuming that the folder backup_DBPayroll does already exist and is writable

I try this, still no db backup outside or inside the folder.

Thank you

jmj001
01-26-2012, 08:30 AM
yeh sorry, i updated it a second ago, ur on a windows server... try it with the backslashes \ now...

newphpcoder
01-26-2012, 08:39 AM
I got an error:


$myfoldername = "backup_DBPayroll";//your folders name
$handle = fopen(getcwd().\.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampp\htdocs\payroll\backup_db.php on line 58

Parse error: syntax error, unexpected '.' in C:\xampp\htdocs\payroll\backup_db.php on line 58

jmj001
01-26-2012, 08:44 AM
yeh, that's not my changes... ur still using your old code

jmj001
01-26-2012, 08:46 AM
actually it's probably right with the / slashes... i guess the server will manage the difference...


$myfoldername = $_SERVER['DOCUMENT_ROOT']."/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

replace your lines with these

newphpcoder
01-26-2012, 08:47 AM
I try this:


$myfoldername = $_SERVER['DOCUMENT_ROOT']."/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.\.'.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');


but still i got this error:


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\xampp\htdocs\payroll\backup_db.php on line 60

Parse error: syntax error, unexpected '.' in C:\xampp\htdocs\payroll\backup_db.php on line 60


thank you

jmj001
01-26-2012, 08:50 AM
but that's not my changes... you've added .\. which is invalid syntax...

just paste my changes directly into your code and it should work

newphpcoder
01-26-2012, 08:54 AM
this is the revise code:


<?php
include 'config.php';
backup_tables('localhost','root','','payroll');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);

//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}

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

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

for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\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
// $myfoldername = "backup_DBPayroll";//your folders name
// $handle = fopen(getcwd().\.$myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
$myfoldername = $_SERVER['DOCUMENT_ROOT']."/backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

// $myfoldername = $_SERVER['DOCUMENT_ROOT']."/path-to/backup_DBPayroll/";//your folders name
// $handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');


// $handle = fopen('db-backup-'.date('m-d-Y').'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}


$smarty->display('header_cat.tpl');
$smarty->display('backup.tpl');
$smarty->display('footer.tpl');
?>



No error but no db back up save

Thank you

jmj001
01-26-2012, 09:03 AM
okie dokie... put the following code in a file....


<?php
$thisDir = getcwd();
echo $thisDir.'<br>';
echo is_writable($thisDir) ? $thisDir.' IS writable' : $thisDir.' IS NOT writable';
echo '<br>';
echo $_SERVER['DOCUMENT_ROOT'];
echo '<br>';
?>

name it test.php

save it in the backup_DBPayroll folder and open it in a browser and paste the results here ...

newphpcoder
01-26-2012, 09:07 AM
okie dokie... put the following code in a file....


<?php
$thisDir = getcwd();
echo $thisDir.'<br>';
echo is_writable($thisDir) ? $thisDir.' IS writable' : $thisDir.' IS NOT writable';
echo '<br>';
echo $_SERVER['DOCUMENT_ROOT'];
echo '<br>';
?>

name it test.php

save it in the backup_DBPayroll folder and open it in a browser and paste the results here ...

here is the output

C:\xampp\htdocs\backup_DBPayroll
C:\xampp\htdocs\backup_DBPayroll IS writable
C:\Documents and Settings\rhoda.barrera\Application Data\NuSphere\PhpED\projects

jmj001
01-26-2012, 09:15 AM
ok... server[docroot] prob won't work...


$myfoldername = "../backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

try again :)

jmj001
01-26-2012, 09:17 AM
btw if you look in C:\Documents and Settings\rhoda.barrera\Application Data\NuSphere\PhpED\projects you may find the backups...

not sure though, i have zero experience with php on windows

newphpcoder
01-26-2012, 09:21 AM
ok... server[docroot] prob won't work...


$myfoldername = "../backup_DBPayroll/";//your folders name
$handle = fopen($myfoldername.'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');

try again :)

Thanks...it works now..

BUt what if I only need to back up one table and in an excel file?

Is it possible?

Thank you

jmj001
01-26-2012, 09:25 AM
yeh but wouldn't be a backup just a csv dump

newphpcoder
01-26-2012, 09:32 AM
How???

Thank you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum