RESTful knowledge exchange
Post Reply
jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Download file from VM to local host

Post by jw_ic »

Is there a way to download a file from the vm restore point, to your local host where you are running the REST commands?

Trying to do

https://emserver:9398/api/catalog/vms/v ... on=restore

in the request body I have json:

{
"FileRestoreSpec": {
"ForDirectDownload": "",
"_xmlns": "http://www.veeam.com/ent/v1.0",
"_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
}
}

Then I get back:

<?xml version="1.0" encoding="utf-8"?>
<Task xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Href="https://emserver:9398/api/tasks/task-5" Type="Task" xmlns="http://www.veeam.com/ent/v1.0">
<Links>
<Link Href="https://emserver:9398/api/tasks/task-5" Rel="Delete" />
<Link Href="https://emserver:9398/api/restoreSessio ... mat=Entity" Name="Restore job session" Type="RestoreSession" Rel="Related" />
</Links>
<TaskId>task-5</TaskId>
<State>Finished</State>
<Operation>RestoreFile</Operation>
<Result Success="true">
<Message>Restore operations started successfully.</Message>
</Result>
</Task>


then, if I try:
https://emserver:9398/api/catalog/vms/v ... n=download

I get:

{"FirstChanceExceptionMessage":null,"StackTrace":null,"Message":"{\"FirstChanceExceptionMessage\":null,\"StackTrace\":null,\"Message\":\"Resource does not exist\",\"StatusCode\":404,\"Status\":\"NotFound\"}","StatusCode":404,"Status":"NotFound"}

chris.arceneaux
Veeam Software
Posts: 367
Liked: 201 times
Joined: Jun 24, 2019 1:39 pm
Full Name: Chris Arceneaux
Location: Kentucky, USA
Contact:

Re: Download file from VM to local host

Post by chris.arceneaux »

Hi James,

Per our documentation, the API call you referenced doesn't allow direct download. Below is another API call with this ability:

(POST) /vmRestorePoints/{ID}/mounts/{ID}/{filepath}?action=restore

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Sure, yes trying this.

POST:

Code: Select all

https://emserver:9398/api/vmRestorePoints/6eebf77d-5c23-4eaa-997e-09634555fb21/mounts/1/C:/tech/scripts/wssa.bat?action=restore
And getting

Code: Select all

{
    "FirstChanceExceptionMessage": null,
    "StackTrace": null,
    "Message": "The method or operation is not implemented.",
    "StatusCode": 500,
    "Status": "InternalServerError"
}
Headers including:

[{"key":"X-RestSvcSessionId","value":"MDJlZGE5MjQtZDNlMy00MWM5LWI0ZTUtN2YwYjM3OTdhOWQw","description":"","type":"text","enabled":true}]
[{"key":"Accept","value":"application/json","description":"","type":"text","enabled":true}]
[{"key":"Content-Type","value":"application/json","description":"","type":"text","enabled":true}]

Body: ( Raw / json )

Code: Select all

{
   "FileRestoreSpec": {
      "ForDirectDownload": "",
      "_xmlns": "http://www.veeam.com/ent/v1.0",
      "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
      "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
   }
}
fyi; Using Postman to test this out.

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

In the REST API log I see this:

Code: Select all

[06.01.2021 10:38:27] <62> Error    Failed to start file restore from mount point. vmRestorePointIdStr: 6eebf77d-5c23-4eaa-997e-09634555fb21, mountIdStr: 1, path: C:/tech/scripts/wssa.bat. RequestUri: [URI template match was not found].
[06.01.2021 10:38:27] <62> Error    The method or operation is not implemented. (System.NotImplementedException)
[06.01.2021 10:38:27] <62> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestApiFlrBrowserScope`2.StartFileRestoreFromMountPoint(String vmPointIdStr, String mountIdStr, String displayPath, FileRestoreSpecType restoreSpec)
[06.01.2021 10:38:27] <62> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestRestoreMountPointControllerStub.<StartFileRestoreFromMountPoint>d__8.MoveNext()
[06.01.2021 10:49:45] <59> Info     [GET] request to [https://veeamem:9398/api/vmRestorePoints/6eebf77d-5c23-4eaa-997e-09634555fb21/mounts/1/C:/tech/scripts?action=listFiles&amp;pageSize=10&amp;page=1] deserialized. Message: [null]; Parameters: [].
[06.01.2021 10:49:45] <59> Info     [02eda924d3e341c9b4e57f0b3797a9d0] Retrieving mount point file items list. vmPointIdStr: 6eebf77d-5c23-4eaa-997e-09634555fb21, mountIdStr: 1, path: C:/tech/scripts, pageSize: , pageNum: 
[06.01.2021 10:49:54] <38> Info     [POST] request to [https://veeamem:9398/api/vmRestorePoints/6eebf77d-5c23-4eaa-997e-09634555fb21/mounts/1/C:/tech/scripts/wssa.bat?action=restore] deserialized. Message: [{
[06.01.2021 10:49:54] <38> Info        "FileRestoreSpec": {
[06.01.2021 10:49:54] <38> Info           "ForDirectDownload": "",
[06.01.2021 10:49:54] <38> Info           "_xmlns": "http://www.veeam.com/ent/v1.0",
[06.01.2021 10:49:54] <38> Info           "_xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
[06.01.2021 10:49:54] <38> Info           "_xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
[06.01.2021 10:49:54] <38> Info        }
[06.01.2021 10:49:54] <38> Info     }]; Parameters: [].
[06.01.2021 10:49:54] <38> Info     [02eda924d3e341c9b4e57f0b3797a9d0] Starting file restore from mount point. vmPointIdStr: 6eebf77d-5c23-4eaa-997e-09634555fb21, mountIdStr: 1, path: C:/tech/scripts/wssa.bat
[06.01.2021 10:49:54] <38> Error    Failed to start file restore from mount point. vmRestorePointIdStr: 6eebf77d-5c23-4eaa-997e-09634555fb21, mountIdStr: 1, path: C:/tech/scripts/wssa.bat. RequestUri: [URI template match was not found].
[06.01.2021 10:49:54] <38> Error    The method or operation is not implemented. (System.NotImplementedException)
[06.01.2021 10:49:54] <38> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestApiFlrBrowserScope`2.StartFileRestoreFromMountPoint(String vmPointIdStr, String mountIdStr, String displayPath, FileRestoreSpecType restoreSpec)
[06.01.2021 10:49:54] <38> Error       at Veeam.Backup.Enterprise.RestAPIService.CRestRestoreMountPointControllerStub.<StartFileRestoreFromMountPoint>d__8.MoveNext()

oleg.feoktistov
Veeam Software
Posts: 847
Liked: 300 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Download file from VM to local host

Post by oleg.feoktistov »

Hi James,

EM REST API interprets converted self-closing xml tags as a json object, and all the rest is redundant. For your request body just use this:

Code: Select all

{
      "ForDirectDownload": {}
}
Thanks,
Oleg

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Was able to get father with the suggestion of using

Code: Select all

{
      "ForDirectDownload": {}
}

So thank you Oleg!

I am using Postman and it put the file in the Body for the results? Is there a way to save it as a file vs display it? and can you specify where it downloads to on the local file system?

Thanks

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

fyi; I do have a case open: Case #04576926

oleg.feoktistov
Veeam Software
Posts: 847
Liked: 300 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Download file from VM to local host

Post by oleg.feoktistov »

Yes, postman displays file contents in response body by default. The easiest way to save files from postman UI is to expand "Send" button and choose "Send and Download" instead. Upon request completion, Postman should prompt you to save the file to your filesystem.
If we are talking about full automation, code would depend on the language and libraries you use, but the algorithm is all the same:
  • Create request to the download endpoint.
  • Create writable stream on your file system. Use full path (e.g. "/home/admin/Downloads/file.log").
  • Send request and pipe response to a writable stream.
  • Close writable stream and check the file on your fs.
Hope it helps,
Oleg

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Ah ok, didn't notice that on postman.

So when automating, I would be using PowerShell. Would you have a quick example for downloading?

oleg.feoktistov
Veeam Software
Posts: 847
Liked: 300 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Download file from VM to local host

Post by oleg.feoktistov »

Just pipe out the response to Out-FIle cmdlet and pass -FilePath with a precise file name:

Code: Select all

 Invoke-RestMethod -Method Get -Uri $uri -Headers $headers | Out-File -FilePath 'C:\file.log' 
Or use success stream redirection:

Code: Select all

 Invoke-RestMethod -Method Get -Uri $uri -Headers $headers > 'C:\file.log' 
With such approach responses will be redirected to a file only on success.

Thanks!

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Easy enough, thanks again for your help!

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

@oleg.feoktistov

For the powershell to create the json object I did:

Code: Select all

$jsonBase = @{}
$jsonBase.Add("ForDirectDownload","{}")
$json = $jsonBase | ConvertTo-Json
$json
But was getting:

Code: Select all

Invoke-WebRequest : {"FirstChanceExceptionMessage":null,"StackTrace":null,"Message":"restoreSpec.ForDirectDownload : Error converting value \"{}\" to type 'Veeam.Backup.Interaction.RestAPI.Resources.FileRestoreSpecTypeForDirectDownload'. Path 
'ForDirectDownload', line 2, position 30. ","StatusCode":400,"Status":"BadRequest"}
At line:1 char:11
+ $result = Invoke-WebRequest -Uri $uri -method post -Headers $headers  ...
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Thanks

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Oh, maybe I have to do the ?action=restore before I can do ?action=download? Or can I just do ?action=download

jw_ic
Enthusiast
Posts: 32
Liked: never
Joined: Oct 25, 2017 1:26 pm
Full Name: James Wuerflein
Contact:

Re: Download file from VM to local host

Post by jw_ic »

Looks like that was the case, aka need to do the ?action=restore first, then ?action=download second. So with the above Invoke-Rest command I got the file downloaded. The file I tried was a text file, so now need to try a more binary file and make sure that works as well. Wondering now though if there is a way to get how long it will take to download the file, and if the file has finished downloading. But first thing is first is try to get a binary type file.

jhoughes
Veeam Software
Posts: 260
Liked: 105 times
Joined: Apr 20, 2017 4:19 pm
Full Name: Joe Houghes
Location: Austin, TX
Contact:

Re: Download file from VM to local host

Post by jhoughes »

Yes, the restore action must always be processed first.

PowerShell will not return the prompt for you until the download has completed, so you'll know when it's completed. I don't remember of the top of my head if you'll have a progress bar in the PowerShell window or not, as it's been a while since I've done a "stare and compare" recovery.

There won't be any method to determine the time to recover beyond estimating it for your setup, as it would be dependent upon the setup of the session and basic overhead, the size of the file, the transfer speed of the download, along with the file operations on the host running the PowerShell session, and nothing in the API would calculate that automatically.
Husband, Father, Solutions Architect, Geek Extraordinaire | @AustinVMUG & @ATXPowerShell leader | VMware vExpert | Cisco Champion

oleg.feoktistov
Veeam Software
Posts: 847
Liked: 300 times
Joined: Sep 25, 2019 10:32 am
Full Name: Oleg Feoktistov
Contact:

Re: Download file from VM to local host

Post by oleg.feoktistov »

I don't have PS console at hand to duly test it, and I don't normally use powershell to interact with REST API,
but the script shared here looks good to me in terms of setting up a progress bar for your file downloads.
Try one and see if it works. Thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests