﻿<?PHP

    //====== custom parameters ==========================
    
    $backupName = "Backup job name";
    $login = "login";
    $password = "password";
    
    //=========================================

	require_once('Common.php');
	require_once('util\http.php');
	
	use XmlSerialization\XmlSerializer;	
    use \com\veeam\www\ent\v1_0 as restApi;
    
    
	$http = new HttpContext('127.0.0.1', 9399);
	$restApiInteraction = new RestApiInteraction($http);
	
	// log in using basic authentification
	$session = $restApiInteraction->LogInByBasicAuthentication($login, $password);
	if ($session->SessionId == null) { echo 'Login failed'; exit; }

	echo 'Logged in successfully, sessionId : ' . $session->SessionId . "\r\n";
    
    echo "Getting info about the following backup job: \"" . $backupName . "\"\r\n";
    
    $queryResult = $restApiInteraction -> GetObject(
                    "/api/query?type=Backup&format=Entities&filter=Name==\"" . urlencode($backupName) . "\"",
                    ClassHelper::CombineWithNamespace("QueryResultType")
                    );
    if (!isset($queryResult) || !isset($queryResult->Entities) 
        || !isset($queryResult->Entities->Backups) || !isset($queryResult->Entities->Backups->Backup)) { echo "Cannot find backup job with the following name: '" . $backupName . "'";exit; }
        
    $backup = $queryResult -> Entities -> Backups -> Backup[0];
    
    echo "Getting restore points for the backup \r\n";
    $restorePointsLink = LinkHelper::FindLink($backup->Links->Link, LinkRelations::Down);
    $restorePoints = $restApiInteraction -> GetObject(
                    $restorePointsLink->Href . "?format=Entity",
                    ClassHelper::CombineWithNamespace("RestorePointEntityListType")
                    );
    if (!isset($restorePoints)) { echo "Unable to retrieve restore points for backup \"" . $backupName . "\""; exit; }
    if (count($restorePoints->RestorePoint) == 0) { echo "No restore points exist for backup " . $backupName; exit;}
    
    
    foreach($restorePoints->RestorePoint as $restorePoint)
        if (!isset($latestRp) || $restorePoint->BackupDateUTC > $latestRp->BackupDateUTC)
            $latestRp = $restorePoint;
    
    echo "Getting VMs from the latest restore point\r\n";
    $vmRestorePointsLink = LinkHelper::FindLink($latestRp->Links->Link, LinkRelations::Down);
    $vmRestorePoints = $restApiInteraction -> GetObject(
                    $vmRestorePointsLink->Href . "?format=Entity",
                    ClassHelper::CombineWithNamespace("VmRestorePointEntityListType")
                    );
    if (!isset($vmRestorePoints)) { echo "Unable to retrieve VM restore points for backup \"" . $backupName . "\""; exit;}
    if (count($vmRestorePoints->VmRestorePoint) == 0) { echo "No VM restore points exist in backup \"" . $backupName . "\""; exit;}
    
    $vmRestorePoint = $vmRestorePoints->VmRestorePoint[0];
     
    echo "Starting to restore VM to original location\r\n";
    $restoreVmLink = LinkHelper::FindLink($vmRestorePoint->Links->Link, LinkRelations::Restore);
    $restoreTask = $restApiInteraction->PostObject(
                                    $restoreVmLink->Href, 
                                    null,
                                    ClassHelper::CombineWithNamespace("TaskType")
                                    );
    
    echo "Waiting for task result\r\n";
    if (strcmp($restoreTask->State, 'Running') == 0) {
		$restoreTask = $restApiInteraction->WaitForTaskResult($restoreTask->Href, 1);
	}
    
    // check if clone task is succeeded
	if (!$restoreTask->Result->Success) { echo "Restoration failed\r\n\r\n"; var_dump($restoreTask); exit; }
	
	echo "VM was restored successfully\n";
    echo "Removing tasks\n";
    $restApiInteraction->DeleteTask($restoreTask);
    echo "Logout";
    $restApiInteraction->Logout($session);
?>