Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-26-2012, 06:43 AM   PM User | #1
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 06:52 AM   PM User | #2
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
newphpcoder (01-26-2012)
Old 01-26-2012, 07:00 AM   PM User | #3
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 07:07 AM   PM User | #4
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
please paste your updated code here

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

PHP Code:
echo __FILE__
it will show the full path to the script you are running
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
newphpcoder (01-26-2012)
Old 01-26-2012, 07:15 AM   PM User | #5
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 08:18 AM   PM User | #6
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
newphpcoder (01-26-2012)
Old 01-26-2012, 08:20 AM   PM User | #7
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 08:24 AM   PM User | #8
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Quote:
Originally Posted by jmj001 View Post
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 08:30 AM   PM User | #9
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
yeh sorry, i updated it a second ago, ur on a windows server... try it with the backslashes \ now...
jmj001 is offline   Reply With Quote
Users who have thanked jmj001 for this post:
newphpcoder (01-26-2012)
Old 01-26-2012, 08:39 AM   PM User | #10
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 08:44 AM   PM User | #11
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
yeh, that's not my changes... ur still using your old code
jmj001 is offline   Reply With Quote
Old 01-26-2012, 08:46 AM   PM User | #12
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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
jmj001 is offline   Reply With Quote
Old 01-26-2012, 08:47 AM   PM User | #13
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Old 01-26-2012, 08:50 AM   PM User | #14
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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
jmj001 is offline   Reply With Quote
Old 01-26-2012, 08:54 AM   PM User | #15
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
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
newphpcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:46 AM.


Advertisement
Log in to turn off these ads.