Jump to content

Restore DB from a remote website?


darubillah

Recommended Posts

hi,

 

I am trying to restore  a mysql db from a remote location

 

below is the function it is working fine when i am passing local location, but gives error on passing remote location

 

function restore($path) {
  $f = fopen('restore.sql' , 'w+');
  if(!$f) {
   echo "Error While Restoring Database";
   return;
  }
  $zip = new ZipArchive();
  if ($zip->open($path) === TRUE) {
   #Get the backup content
   $sql = $zip->getFromName('adobe4u_new-sqldump.sql');
   #Close the Zip File
   $zip->close();
   #Prepare the sql file
   fwrite($f , $sql);
   fclose($f);

   #Now restore from the .sql file
   //$command = "mysql --user=root --password=password --database=adobe4u < restore.sql";
   //exec($command);

// Name of the file
$filename = 'restore.sql';



// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
	continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
	// Perform the query
	mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
	// Reset temp variable to empty
	$templine = '';
}
}
// Restoring END

   #Delete temporary files without any warning
   @unlink('restore.sql');
   echo '<div class="success">Successfully Updated!</div>';
  }
  else {
   echo '<div class="error">Some thing went wrong! Updation Failed</div>';
  }
} 

 

Please can anyone help me in it, so That if i pass a remote location of database it will extract it locally

 

Link to comment
Share on other sites

Problem solved

 

what we have to do is to download the file from remote server to local

 

<?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);


//File Location to restore the downloaded DB
$filename = 'backup/backup.zip';

//Checking if database already exists then delete it first
if (file_exists($filename)) {
    @unlink($filename);
}



// folder to save downloaded files to. must end with slash
$destination_folder = 'backup/';

//File URL to download zip database
$url = "http://example.com/backup.zip";

$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
  $newf = fopen ($newfname, "wb");

  if ($newf)
  while(!feof($file)) {
    fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
  }
}

if ($file) {
  fclose($file);
}

if ($newf) {
  fclose($newf);
}

//Call above restore function to restore database
restore($filename)
?>

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.