<?php
//   Backup a dSurvey session

require("phpVars.php");

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

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

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

//$dstamp=date("d_m_Y_H_i") ;
$dstamp=date('U') ;
$ares="Backup of $sessName at $dstamp \n ";

$sbackup_dir=$backup_dir . $sessName .'/' ;
if (!file_exists($sbackup_dir)) {
   if (!mkdir($sbackup_dir)) {
      print "-Sorry. Unable to create directory: $sbackup_dir";
      return 0 ;
   }
}
$ares.="Backing up session files to $sbackup_dir \n " ;

// 1: copy mysql databast to backup direcotry

$backupFile=$sbackup_dir .  'mysql_' . $dstamp . ".sql";
$aexists=file_exists($backupFile);
$aexists=file_exists($backupFile);
if ($aexists) {
  $astat=unlink($backupFile);
  if (!$astat) {
     print "-Error. Unable to delete existing dbBackup file: $backupFile ";
     exit ;
  }
}
$link=connectDsurveyDatabase($sessName);
$mdata=$currentDatabase ;

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

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


 // Now, copy session descirptor file
 $oldfile=$sessions_dir . $sessName . '.xml' ;
 $newfile=$sbackup_dir .   'descriptor_' . $dstamp . '$' . $sessName . ".xml";
 if (!copy($oldfile,$newfile)) {
     $ares .= "Unable to write to dbBackup file: $backupFile \n ";
    $ierr++ ;
 } else {
     $ares .= "Descriptor file saved \n ";
 }

// now do all files in sessions\sessname
 $sDir=$sessions_dir .  $sessName . '/' ;
 $dir=opendir($sDir);
 while (!($filename= readdir($dir)) === FALSE) {
          $from1=$sDir . $filename ;
          if (is_dir($from1)) continue ;
//         $goon=explode('.',$filename);
         $newfile=$sbackup_dir . 'session_' . $dstamp . '$' . $filename ;
//         if (count($goon)>1) {$newfile.= $goon[1] ; } ;
         if (copy($from1,$newfile)) {
             $ares.= "Backing up $filename \n ";
         } else {
               $ares .= "Unable to copy to $newfile " ;
               $ierr++ ;
         }
  }

  // now do all files in pinfo\sessname
 $sDir=$pinfo_dir .  $sessName . '/' ;
 $dir=opendir($sDir);
 while (!($filename= readdir($dir)) === FALSE) {
          $from1=$sDir . $filename ;
          if (is_dir($from1)) continue ;
//         $goon=explode('.',$filename);
         $newfile=$sbackup_dir . 'pinfo_' . $dstamp . '$' . $filename ;
//         if (count($goon)>1) {$newfile.= $goon[1] ; } ;
         if (copy($from1,$newfile)) {
             $ares.= "Backing up $filename \n ";
         } else {
               $ares .= "Unable to copy to $newfile " ;
               $ierr++  ;
         }
  }


if ($ierr==0) {
   $ares="ok session files saved. \n " . $ares ;
} else {
   $ares="-Error: problem saving session files \n " . $ares ;
}
print $ares ;
exit;


?>




