[slbackup-commit] CVS update: slbackup-php/src index.php

finnarne-guest at alioth.debian.org finnarne-guest at alioth.debian.org
Wed Apr 25 08:15:47 UTC 2007


  User: finnarne-guest
  Date: 07/04/25 08:15:47

  Modified:    src      index.php
  Log:
  Now it works to restore a snapshot of a file
  
  Revision  Changes    Path
  1.33      +163 -12   slbackup-php/src/index.php
  
  Index: index.php
  ===================================================================
  RCS file: /cvsroot/slbackup/slbackup-php/src/index.php,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- index.php	20 Apr 2007 06:24:48 -0000	1.32
  +++ index.php	25 Apr 2007 08:15:47 -0000	1.33
  @@ -146,6 +146,123 @@
       } else unset ($out) ; 
       return $out ; 
   }
  +function fetchsnapshot ($passwd, $clientdir, $location, $file) {
  +global $backuphost, $backupuser, $ssh_options, $ssh_askpass ;
  +
  +    $desc[0] = array ("pipe", "r") ; 
  +    $desc[1] = array ("pipe", "w") ; 
  +    $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  +
  +    $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  +		  'DISPLAY' => ':nowhere') ; 
  +    $cmd = sprintf ("ssh %s %s@%s 'mktemp'", 
  +   		     $ssh_options, $backupuser, 
  +		     $backuphost) ;
  +
  +    $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
  +    if (is_resource ($proc)) {
  +	fprintf ($pipes[0], "%s\n", $passwd) ; 
  +	fclose ($pipes[0]) ; 
  +	$tempfile = stream_get_contents($pipes[1]) ; 
  +	fclose ($pipes[1]) ; 
  +	proc_close ($proc) ; 
  +    } else unset ($tempfile) ; 
  +    if (empty($tempfile))
  +        return ; 
  +    $cmd = sprintf ("ssh %s %s@%s 'rdiff-backup --force %s/rdiff-backup-data/increments%s/%s %s'", 
  +   		     $ssh_options, $backupuser, 
  +		     $backuphost, $clientdir, $location, $file, $tempfile ) ; 
  +
  +    $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
  +    if (is_resource ($proc)) {
  +	fprintf ($pipes[0], "%s\n", $passwd) ; 
  +	fclose ($pipes[0]) ; 
  +	fclose ($pipes[1]) ; 
  +	proc_close ($proc) ; 
  +    } 
  +    $cmd = sprintf ("ssh %s %s@%s 'cat %s && rm %s'", 
  +   		     $ssh_options, $backupuser, 
  +		     $backuphost, $tempfile, $tempfile) ; 
  +    $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
  +    if (is_resource ($proc)) {
  +	fprintf ($pipes[0], "%s\n", $passwd) ; 
  +	fclose ($pipes[0]) ; 
  +	$out = stream_get_contents($pipes[1]) ; 
  +	fclose ($pipes[1]) ; 
  +	proc_close ($proc) ; 
  +    } else unset ($out) ; 
  +    return $out ; 
  +}
  +
  +function listfile ($passwd, $clientdir, $location, $file) {
  +global $backuphost, $backupuser, $ssh_options, $ssh_askpass ;
  +
  +    $out = array() ; 
  +    $parent = dirname ($file) ;
  +    $base = basename ($file) ; 
  +    if (empty($file)) 
  +        return $out ; 
  +    if ($parent == ".") $parent="" ; 
  +    $out[] = array ('type' => 'parent', 
  +		    'name' => "Parent directory", 
  +		    'sub' => trim ($parent)) ; 
  +
  +    $desc[0] = array ("pipe", "r") ; 
  +    $desc[1] = array ("pipe", "w") ; 
  +    $desc[2] = array ("file", "/tmp/error.log", "a") ; 
  +
  +    $env = array ('SSH_ASKPASS' => $ssh_askpass, 
  +		  'DISPLAY' => ':nowhere') ; 
  +    $cmd = sprintf ("ssh %s %s@%s 'find %s%s/%s -maxdepth 1 -mindepth 1 -type f -name %s'", 
  +   		     $ssh_options, $backupuser, 
  +		     $backuphost, $clientdir, $location, $parent, $base ) ; 
  +    $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
  +    if (is_resource ($proc)) {
  +	fprintf ($pipes[0], "%s\n", $passwd) ; 
  +	fclose ($pipes[0]) ; 
  +	while ($line = fgets ($pipes[1], 1024)) {
  +	    $out[] = array ('type' => 'currentfile', 
  +	                    'name' => basename (trim($line)), 
  +	                    'sub' => substr (trim ($line), strlen ($clientdir) + strlen ($location) + 1)) ; 
  +
  +	    }
  +	fclose ($pipes[1]) ; 
  +	proc_close ($proc) ; 
  +    }
  +    $baselen= strlen ($base) ; 
  +    $cmd = sprintf ("ssh %s %s@%s 'find %s/rdiff-backup-data/increments%s/%s -maxdepth 1 -mindepth 1 -type f -name \"%s*.gz\"'", 
  +   		     $ssh_options, $backupuser, 
  +		     $backuphost, $clientdir, $location, $parent, $base ) ; 
  +
  +    $proc = proc_open ($cmd, $desc, $pipes, '/tmp', $env) ; 
  +    if (is_resource ($proc)) {
  +	fprintf ($pipes[0], "%s\n", $passwd) ; 
  +	fclose ($pipes[0]) ; 
  +	while ($line = fgets ($pipes[1], 1024)) {
  +	    $gzbase = basename (trim($line)) ; 
  +	    $type = substr ($gzbase, $baselen + 27) ; 
  +	    $ts = substr($gzbase, $baselen + 1, 25) ;
  +	    $revision = $gzbase ; 
  +	    while (($pos = strpos ($revision, '+')) !== false)
  +	        $revision = substr ($revision, 0,$pos) . '%2B' . substr ($revision, $pos + 1 ) ;
  +	    switch ($type) {
  +	        case "snapshot.gz":
  +	        case "diff.gz":
  +	        default:
  +		    $out[] = array ('type' => $type, 
  +				    'ts' => $ts, 
  +				    'name' => $base, 
  +				    'revision' => $revision,
  +				    'parent' => sprintf ("%s/%s", $sub, $parent)) ; 
  +		    break ;
  +	    }
  +        }
  +	fclose ($pipes[1]) ; 
  +	proc_close ($proc) ; 
  +    }
  +
  +    return $out ; 
  +}
   
   function listloc ($passwd, $clientdir, $location, $sub = "") {
   global $backuphost, $backupuser, $ssh_options, $ssh_askpass ;
  @@ -514,6 +631,9 @@
           case "client":
               $client=htmlspecialchars(trim($value)) ; 
               break ;
  +        case "revision":
  +            $revision=htmlspecialchars(trim($value)) ; 
  +            break ;
           default:
               $newconf[$key] = htmlspecialchars(trim($value)) ; 
               break ;
  @@ -560,6 +680,7 @@
       sscanf ($array[0], "%d", $config["minutes"]) ; 
       sscanf ($array[1], "%d", $config["hours"]) ; 
   }
  +unset ($revisions) ; 
   
   switch ($submit) {
       case "scheduler": 
  @@ -669,15 +790,41 @@
       case "restorefile": 
           if (empty($client) || empty ($newconf["location"]))
               break ; 
  -         $out = fetchfile ($passwd, 
  -			   sprintf ("%s/%s", $config["server_destdir"], $client), 
  -			   $newconf["location"], $newconf["file"]) ; 
  -
  -        header ('Content-Type: binary/raw') ; 
  -        header ('Content-Length: ' . strlen ($out)) ; 
  -        header (sprintf ("Content-Disposition: attachment; filename=\"%s\"", basename ($newconf["file"]))) ; 
  -        echo $out ; 
  -	exit  ; 
  +         switch ($revision) {
  +             case "current": 
  +		 $out = fetchfile ($passwd, 
  +				   sprintf ("%s/%s", 
  +				            $config["server_destdir"], 
  +				            $client), 
  +				   $newconf["location"], $newconf["file"]) ; 
  +		header ('Content-Type: binary/raw') ; 
  +		header ('Content-Length: ' . strlen ($out)) ; 
  +		header (sprintf ("Content-Disposition: attachment; filename=\"%s\"", basename ($newconf["file"]))) ; 
  +		echo $out ; 
  +		exit  ; 
  +            default:
  +                if (empty($revision))
  +		    $revisions = listfile ($passwd,
  +					   sprintf ("%s/%s", 
  +						    $config["server_destdir"], 
  +						    $client), 
  +					   $newconf["location"], $newconf["sub"]) ; 
  +                else {
  +		 $out = fetchsnapshot ($passwd, 
  +				   sprintf ("%s/%s", 
  +				            $config["server_destdir"], 
  +				            $client), 
  +				   $newconf["location"], sprintf ("%s/%s", $newconf["parent"], $revision)) ; 
  +		header ('Content-Type: binary/raw') ; 
  +		header ('Content-Length: ' . strlen ($out)) ; 
  +		header (sprintf ("Content-Disposition: attachment; filename=\"%s\"", $newconf["name"])) ; 
  +		echo $out ; 
  +		exit  ; 
  +//		    debug (array ('client' => $client, 'serverdir' => $config['server_destdir'], 'revision' => $revision , 'newconf' => $newconf)) ; 
  +               }
  +               break ;
  +        }
  +        break ; 
       case "restorelocation": 
           if (empty($client) || empty ($newconf["location"]))
               break ; 
  @@ -713,10 +860,14 @@
   }
   
   switch ($submit) {
  +    case "restorefile":
  +        if (empty($revisions))
  +            $revisions=array() ; 
  +        $smarty->assign ('revisions', $revisions) ; 
       case "restorelocation":
  -    $smarty->assign ('loc', $loc) ; 
  -    $smarty->assign ('location', $newconf["location"]) ; 
  -    $smarty->assign ('sub', $newconf["sub"]) ; 
  +	$smarty->assign ('loc', $loc) ; 
  +	$smarty->assign ('location', $newconf["location"]) ; 
  +	$smarty->assign ('sub', $newconf["sub"]) ; 
       case "restoreclient":
       case "restore":
   	$clients = array_keys($config["clients"]) ; 
  
  
  



More information about the slbackup-commit mailing list