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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.