Rubrik : API Call for Downloading a File from a Snapshot



Background

You have a bunch of snapshots in your Rubrik appliance and you would like to download a file from one of them. I have a particular use case, where I need to attach a file to a report email from a backup to prove this has been successfully completed. This is how our DBAs can confirm that Rubrik is doing the job right.



Php Comes to Rescue!

So, we already know that Rubrik is allowing direct download of files from a generated URL who is valid for 24 hours. This is how on-demand restore works. Now, we would like to automate this step and have the URL generated and returned. What I'm looking for, roughly, is a function like this : rkGetFileURLfromSnap($clusterConnect,$snapshotID,$fileName).

This can be achieve with 2 different API calls.

1) /api/internal/fileset/snapshot/{$snapshotID}/download_files
2) /api/v1/fileset/request/{id}

First, you are generating the URL for a specific file in a specific snapshot. Then, this is see as a job and once the job is completed, you are getting the URL.


function rkGetFileURLfromSnap($clusterConnect,$snapshotID,$fileName)
{
// Step 1 - Generate URL

$API="/api/internal/fileset/snapshot/".$snapshotID."/download_files";
$config_params="
{
  \"sourceDirs\": [
\"".$fileName."\"
  ]
}";

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$config_params);
curl_setopt($curl, CURLOPT_USERPWD, $clusterConnect["username"].":".$clusterConnect["password"]);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: '.strlen($config_params),'Accept: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$clusterConnect["ip"].$API);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = json_decode(curl_exec($curl));
$info=curl_getinfo($curl,CURLINFO_HTTP_CODE);
curl_close($curl);

$id=$result->id;

// Step 2 - Get generated URL from above id Generating URL takes few second, need to loop until SUCCEEDED

$API="/api/v1/fileset/request/".urlencode($id);

$Stat="RUNNING";

while ($Stat!="SUCCEEDED")
{
sleep(1);
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $clusterConnect["username"].":".$clusterConnect["password"]);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$clusterConnect["ip"].$API);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = json_decode(curl_exec($curl));
curl_close($curl);
if($result->status=="SUCCEEDED") $Stat="SUCCEEDED";
}

return($result->links[1]->href);

}


There is a "while" statement at the end since we need to wait for the URL to be generated. I have added a 1 sec sleep to avoid overloading the cluster too much.

Constraints : you have to be absolutely sure of the path of the file otherwise, you may run into issues.

This function can be found in my GitHub repository, part of the Rubrik php framework that I have written.

Sample output

print(rkGetFileURLfromSnap($clusterConnect,"5e81e235-5394-40a3-995f-3d369be3fe47","/mypath/myfile.txt"));

download_dir/HUC1tzIC55SQa7ul39V6

Based on the above output, you can construct the URL with https://<rubrik_ip_or_hostname>/download_dir/HUC1tzIC55SQa7ul39V6

The URL generated by Rubrik is valid for 24 hours. It might be safe to capture the time when the URL is created to add it into an array and keep it for reference if needed.

$myFile=array(
                 "URL" => rkGetFileURLfromSnap($clusterConnect,"5e81e235-5394-40a3-995f-3d369be3fe47","/mypath/myfile.txt"),
       "time"=>date(now);

Then when you are using the $myFile["URL"] later, you may also check that $myFile["time"] is not exceeded the 24 hours window. If you, you cannot use the URL anymore and need to generate a new one.

Comments

What's hot ?

Wallbox : Get The Most Of It (with API)

PCBWay : CNC Machining

Nutanix : CVM stuck into Phoenix