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
https://forums.phpfreaks.com/topic/231130-restore-db-from-a-remote-website/
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)
?>

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.