Jump to content

Database backup to file


calabiyau

Recommended Posts

I have written a database back up script that writes DROP TABLE IF EXIST and CREATE TABLE and a bunch of INSERT INTO commands.  This is all written to a file with |*| as a delimiter between queries and then on restore the file is split into an array and each individual query is run.  I realize I could just back up with php myadmin but in this particular situation, that is not appropriate for what I am trying to do.  I have some questions about this method.  It works really well in testing with just a few tables and entries to back up but what are some of the problems I might run into with a larger database?  I thought I read somewhere that there was a limit on the length of time that a php script may execute.  Is there any way to increase this time with ini set?  Any other problems anyone can see?  The code is below.

 

BACK UP

 

function dtb_backup($database)
{
define ('lnbr',"\n");
$query = '';
$tables = @mysql_list_tables($database);

while ($row = @mysql_fetch_row($tables))

	{
	$table_list[] = $row[0];
	echo "<p>".$row[0]."<br/>";
	}

for ($i=0; $i < @count($table_list); $i++)

	{
	$results = mysql_query('DESCRIBE '.$table_list[$i]);
	$query .= lnbr.'DROP TABLE IF EXISTS '.$table_list[$i].' |*| '.lnbr;
	$query .= lnbr . 'CREATE TABLE '.$table_list[$i]. ' ('. lnbr;

	$tmp = '';

		while($row= @mysql_fetch_assoc($results))

			{
			$query .= $row['Field']." ". $row['Type']." ";


				if ($row['Null'] != 'YES') 
				{ $query .= ' NOT NULL'; }            

				if ($row['Default'] != '') 
				{ $query .= ' DEFAULT \'' . $row['Default'] . '\''; }            

				if ($row['Extra']) 
				{ $query .= ' ' . strtoupper($row['Extra']); }            

				if ($row['Key'] == 'PRI') 
				{ $tmp = 'primary key(' . $row['Field'] . ')'; }             



				$query .= " , ";
			}

	$query .= $tmp . lnbr . ')' . str_repeat(lnbr, 2);


	$results = mysql_query('SELECT * FROM ' . $table_list[$i]);          

		while ($row = @mysql_fetch_assoc($results)) 
			{  
				$query .= ' |*| ';           
				$query .= 'INSERT INTO '.$table_list[$i] .' (';             
				$data = Array();             

				while (list($key, $value) = @each($row)) 

					{ $data['keys'][] = $key; 
					$data['values'][] = strip_pipes(quote_smart($value));

					}             

				$query .= join($data['keys'], ', ') . ')' . lnbr . 'VALUES (\'' . join($data['values'], '\', \'') . '\');' . lnbr;          

			}          

			$query .= str_repeat(lnbr, 2);   
			$query .= ' |*| '.lnbr;    
			}       

	          return $query;
       



 

 

RESTORE

 

$cachepage = "backup.sql";

$dtb_file = file_get_contents($cachepage);

$dtb_split = explode ('|*|',$dtb_file);
$dtb_count = count($dtb_split);

for ($i=0; $i<$dtb_count; $i++)

{
include ('../connections.php');
if (mysql_query($dtb_split[$i],$connect))

	{
	echo "Successful change to ".$dtb_split[$i];
	}
}	

Link to comment
https://forums.phpfreaks.com/topic/39053-database-backup-to-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.