
<?php
//   backup mysql database

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 ;
}

if ($adate!='') {
   $backupFile=$backup_dir . $sessName . '_' . $adate . ".sql";
}
if ($backupFile=='') {
   $backupFile=$backup_dir . $sessName . '_' .  date("d_m_Y_H_i") . ".sql";
}

$link=connectDsurveyDatabase($sessName);
$mdata=$currentDatabase ;

$aexists=file_exists($backupFile);
if ($aexists) {
  $astat=unlink($backupFile);
  if (!$astat) {
     print "-Error. Unable to delete existing dbBackup file: $backupFile ";
     exit ;
  }
}


$stuff=makeDbBackup($link,$sessName) ;

$fp = fopen($backupFile, "w");
if (!$fp) {
   print  "-Error: unable to open dbBackup: $backupFile " ;
 } else {
   if (!fwrite($fp,$stuff)) {
       print "-Error. Unable to write to dbBackup file: $backupFile ";
   } else {
        print "ok  dbBackup of session  $sessName to  $backupFile: (bytes= ". strlen($stuff)  . ")";
   }
 }

exit;


?>


