1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Backup MySQL PHP script

Discussion in 'PHP' started by koolsamule, Sep 25, 2009.

  1. #1
    Hi guys,

    I have a simple script that backup the contents of a database and place the file on the server.
    The problem is that this script works fine on my Apache server, but not on the IIS server (HTTP 500 error).
    <?php
    $backupFile = "../DBJobs_" . date("Y-m-d");
    $command = "mysqldump --opt -h localhost -u (user) -p(password) dbjobs > $backupFile.sql";
    system($command);
    
    if ($command) {
    $mess = "<p>Backup file created!</p>";
    }
    else {
      $mess = "<p>There was a problem with the backup routine.</p>";
    }
    ?>
    PHP:
    Do I have to change the $command variable or the system function?

    Any help would be very appreciated.
     
    koolsamule, Sep 25, 2009 IP
  2. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is the output folder set to write permission?
     
    orionoreo, Sep 26, 2009 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    Don't think windows will support your command. But I could be wrong
     
    stephan2307, Sep 26, 2009 IP
  4. newgenservices

    newgenservices Well-Known Member

    Messages:
    862
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    105
    Digital Goods:
    1
    #4
    Please try to execute the command manually from Command Prompt before running it on PHP and see if it throws any error.
     
    newgenservices, Sep 26, 2009 IP
  5. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi Chaps,

    Thanks for your help, I have the correct credentials and have tested the mysqldump function on the server manually and it writes to the directory no problem.

    Is there anything I can test to see if the system function is working correctly, e.g. replace the $command variable with something simple?

    Cheers
     
    koolsamule, Sep 28, 2009 IP
  6. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    as the others have suggested try running the command in prompt... not sure if mysqldump is working for you
     
    orionoreo, Sep 28, 2009 IP
  7. venetsian

    venetsian Well-Known Member

    Messages:
    1,105
    Likes Received:
    61
    Best Answers:
    0
    Trophy Points:
    168
    #7
    Hi, I'm not 100% sure, but isn't mysqldump a UNIX mysql command? If it was for Windows would it be like mysqldump.exe ?

    This command should work fine in UNIX environment, but as I said above, I'm not sure if will ever work on windows server.

    Cheers,
    Venetsian.
     
    venetsian, Sep 28, 2009 IP
  8. noumenon

    noumenon Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hi koolsamule,

    greetings from france!

    the problem is that you can't be sure that the server you're hosting allows system-commands to be executed!

    if your matter is just the backup of a database, i programed the following script and it works fine for me (as a cronjob as well)!

    all you have to do is to complete the variables (written in capital-letters) within the "if" at the very end!

    the result of the script is a gziped file named depending on the timestamp the script is executed!
    it works on any server (linux, windows, etc) and with any DB.

    hoping it helps!

    <?php
    function check_db($host,$database, $username, $password) {
    	$result = false;
    	$link = @mysql_connect($host, $username, $password);
    	if ($link) {
    		$db_selected = mysql_select_db($database, $link);
    		$result = true;
    	} else gzwrite($fh,"no connection possible to: " . mysql_error());
    	return array($result,$mysqlInfo);
    }
    function export_DB($mysql_database) {
    	$sql="show tables;";
    	$result= @mysql_query($sql);
    	if ($result) {
    		while ($row=mysql_fetch_row($result)) {
    			tableStructure($row[0]);
    			tableData($row[0]);
    		}
    		mysql_free_result($result);
    	}	
    }
    function tableStructure($table) {
    	global $fh;
    	$data= "not OK";
    	$sql="SHOW CREATE TABLE `$table`; ";
    	$result=mysql_query($sql);
    	if ($result) {
    		if ($row=mysql_fetch_assoc($result)) {
    			$data=$row['Create Table'].";\n\n";
    			gzwrite($fh,$data); 	  
    		}
    		mysql_free_result($result);
    	}
    }
    function tableData($table) {
    	global $fh;
    	$sql="select * from `$table`;";
    	$result=mysql_query($sql);
    	if ($result) {
    		$num_rows= mysql_num_rows($result);
    		$num_fields= mysql_num_fields($result);
    		if ($num_rows > 0) {
    			$field_type=array();
    			$i=0;
    			while ($i<$num_fields) {
    				$meta= mysql_fetch_field($result, $i);
    				array_push($field_type, $meta->type);
    				$i++;
    			}
    			$index=0;
    			while ($row=mysql_fetch_row($result)) {
    				gzwrite($fh, "INSERT INTO `$table` VALUES ");
    				gzwrite($fh, "(");
    				for ($i=0; $i<$num_fields; $i++) {
    					if (is_null($row[$i])) gzwrite($fh, "null");
    					else {
    						switch ($field_type[$i]) {
    							case 'int':
    								gzwrite($fh, $row[$i]);
    								break;
    							case 'string':
    							case 'blob' :
    							default:
    								gzwrite($fh, "'".mysql_real_escape_string($row[$i])."'");							  
    						}
    					}
    					if ($i<$num_fields-1) gzwrite($fh, ",");
    				}
    				gzwrite($fh, ");\n");	      
    				$index++;
    			}
    		}
    	}
    	mysql_free_result($result);
    	gzwrite($fh, "\n");
    }
    if ($_SERVER['HTTP_HOST']=="localhost") $host = "localhost";
    else $host = "PLACE YOUR HOSTNAME HERE!";
    $filename = date('Ymd').".sql.gz";
    $fh = gzopen($filename,'w');
    if ($fh) {
    	$res = check_db($host, 'DB_NAME_HERE', 'USERNAME_HERE', 'PASSWORD_HERE');
    	if ($res[0]) export_DB($database);
        else gzwrite($fh,"ERROR exporting main-DB ");
    	gzclose($fh);
    } else gzwrite($fh,"internal file error");
    ?>
    
    PHP:
     
    noumenon, Sep 29, 2009 IP
  9. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for the reply, I'm using an IIS server and neither the exec() nor the system() functions work.
    I've tried this:
    <?php
    $backupFile = "DBJobs_" . date("Y-m-d");
    $command = "mysqldump.exe --opt -h localhost -uUser -pAdmin dbjobs > backupFile.sql";
    $result = var_dump($command);
    if ($result !== false) {
      echo "<p>Backup file created!</p>";
    }
    else {
      echo "<p>There was a problem</p>";
    }
    ?>
    Code (text):
    When run through the browser, returns:
    But the backupFile isn't created on the server. What else can I try? This is the last bit of code I need to finish this project!
     
    koolsamule, Sep 29, 2009 IP
  10. noumenon

    noumenon Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    rehi,

    i just gave you the solution (see post above)!

    all you habe to do is to copy/paste the code! save it under the name you like!
    upload it on your server, and run the script! it runs on ALL servers, with ANY DB!
     
    noumenon, Sep 29, 2009 IP
  11. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi,
    Thanks for that.

    I can confirm that the database is backed up, however I still get the HTTP 500 error (although the .sql.gz file is created and populated)

    How you have any ideas why this is and can it be solved?

    Also, I can't seem to restore from the backup file. . . .it creates tables and enters the data, but doesn't tell mysql to drop if already exists . . .can this be changed?

    Cheers
     
    Last edited: Sep 29, 2009
    koolsamule, Sep 29, 2009 IP
  12. noumenon

    noumenon Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    rehi,

    HTTP500-error? via php? that's odd! that's an internal server-error! are you sure you get this error from php and not from FTP?

    however, i'm glad to see that the script helped!
    and YES SURE adding the DROP TABLE-line can be added! it's just that i don't find it appropriate! if i ever have to restore a table, i want to be sure that i'm not doing mistakes, that's why i left this line out.
    now if you want to know if I can add it, the answer is still "yes", but why should i?

    consider the script as yours! do whatever you like with it! :)
     
    noumenon, Sep 29, 2009 IP
  13. renownedmedia

    renownedmedia Well-Known Member

    Messages:
    65
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    100
    #13
    instead of just using "backupFile.sql", try using an absolute path.
     
    renownedmedia, Sep 29, 2009 IP
  14. satishbhawra40

    satishbhawra40 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks for your help, I have the correct credentials and have tested the mysqldump function on the server manually and it writes to the directory no problem.
     
    satishbhawra40, Sep 29, 2009 IP