
<?php
// restore a mySql database, that was backed up with backupMysqlDatabase

require("phpVars.php");

 header('Content-Type: text/plain; charset=iso-8859-1');

  $sessName='';
  $backupFile='';
  $adate='';

  foreach ($_REQUEST as $vname=>$oof) {
     $avar=trim(strtoupper($vname)) ;
     if ($avar=='SESSION') $sessName=trim($oof);
     if ($avar=='FILE' ) $backupFile=trim($oof) ;
     if ($avar=='DATE' ) $adate=trim($oof) ;
  }

if ($sessName=='') {
  print "- Error. No SESSION= in call to $myScriptName " ;
  exit ;
}

// use default location for a dated backup
if ($adate!='') {
   $backupFile=$backup_dir . $sessName . '_' .  $adate . ".sql";
}

if ($backupFile=='') {
  print "- Error. No FILE= or DATE= in call to $myScriptName " ;
  exit ;
}



$aexists=file_exists($backupFile);
if (!$aexists) {
     print "-Sorry. Unable to find existing backup file: $backupFile ";
     exit ;
}

$file=fopen($backupFile,'r');
if (!$file) return '-Error: problem opening dbBackupfile: ' . $backupFile ;
$query=fread($file,filesize($backupFile));
if (!$query) return '-Error: unable to read from dbBackupfile: ' . $backupFile ;
fclose($file);

$link=mysqli_connect('localhost','dsurveyAdmin','admin');
if (!$link) {
   print ' - Error. Could not connect to mysqli (with username=`dsurveyAdmin` and password=`admin`): ' . mysqli_error();
   exit ;
}
$mdata=$hdrMysql . $sessName ;

// remove old database, then create one (with no data)
mysqli_query($link,"DROP DATABASE IF EXISTS " . $mdata)  ;
$qq=mysqli_query($link,"CREATE DATABASE  " . $mdata)  ;
if (!$qq) {
  print "Error creating mysql database: " . mysql_error();
  return 1 ;
}
$qq=mysqli_select_db($link,$mdata);

// now, restore from backup
$foo=restoreDbBackup($link,$query);
if (substr($foo,0,1)=='-') {
    print "-Error in restore dbBackup of session  $sessName from  $backupFile : $foo ";
} else {
   print 'ok  restore dbBackup of session ' .  $sessName . ' from ' .  $backupFile  . ' : # of  sql commands= ' .  $foo  . ')';
}

exit;


?>


