Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-26-2012, 06:43 AM
PM User |
#1
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
Insert back up database in a folder
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:
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
01-26-2012, 06:52 AM
PM User |
#2
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
if it was me, i'd specify full path to where i want the file saved
PHP Code:
$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
Last edited by jmj001; 01-26-2012 at 06:56 AM ..
Reason: added stuff
Users who have thanked jmj001 for this post:
01-26-2012, 07:00 AM
PM User |
#3
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
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
01-26-2012, 07:07 AM
PM User |
#4
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
please paste your updated code here
also put the following in the file and run it and paste the result here
it will show the full path to the script you are running
Users who have thanked jmj001 for this post:
01-26-2012, 07:15 AM
PM User |
#5
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
Here is the revise code:
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
01-26-2012, 08:18 AM
PM User |
#6
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
you needed to change the path-to bit to the actual path to your folder...
PHP Code:
$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
Last edited by jmj001; 01-26-2012 at 08:27 AM ..
Reason: had forward slashes instead of backslashes for your windows install
Users who have thanked jmj001 for this post:
01-26-2012, 08:20 AM
PM User |
#7
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
Actually from my source here is the actual code:
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
01-26-2012, 08:24 AM
PM User |
#8
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
jmj001
you needed to change the path-to bit to the actual path to your folder...
PHP Code:
$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
01-26-2012, 08:30 AM
PM User |
#9
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
yeh sorry, i updated it a second ago, ur on a windows server... try it with the backslashes \ now...
Users who have thanked jmj001 for this post:
01-26-2012, 08:39 AM
PM User |
#10
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
I got an error:
Code:
$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
01-26-2012, 08:44 AM
PM User |
#11
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
yeh, that's not my changes... ur still using your old code
01-26-2012, 08:46 AM
PM User |
#12
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
actually it's probably right with the / slashes... i guess the server will manage the difference...
PHP Code:
$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
01-26-2012, 08:47 AM
PM User |
#13
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
I try this:
Code:
$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
01-26-2012, 08:50 AM
PM User |
#14
Regular Coder
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
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
Last edited by jmj001; 01-26-2012 at 08:50 AM ..
Reason: spelling
01-26-2012, 08:54 AM
PM User |
#15
Regular Coder
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
this is the revise code:
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
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:46 AM .
Advertisement
Log in to turn off these ads.