File Level Restore Script and Getting a Folder Path

PowerShell script exchange

File Level Restore Script and Getting a Folder Path

Veeam Logoby ShawnKPERS » Tue Apr 15, 2014 6:19 pm

I am trying to automate restoring a file from a windows VM and copy it out. I have a working Jerry rigged script I have been using but I was hoping to rehash an old post from six plus months ago:
powershell-f26/start-vbrwindowsfilerestore-and-getting-a-folder-path-t18913.html

In the post I was trying to find a way to get a path to the file that is sitting in the restore session. At the time I was on v6 and tsightler posted some code that was supposed to do it in V7. I wrote around it and eventually upgraded to V7. I am now going back into the script to see if I can update the code with the recommended solution and find I am in the same boat where I don't see the “MountedDevices” nor “FirstDrive” options. Has anyone tried this and gotten it to work under V7?
ShawnKPERS
Enthusiast
 
Posts: 49
Liked: 4 times
Joined: Fri Apr 29, 2011 3:55 pm
Full Name: Shawn Nix

Re: File Level Restore Script and Getting a Folder Path

Veeam Logoby v.Eremin » Wed Apr 16, 2014 9:56 am

For sure it worked at the time when the topic was created, as both Tom and I tested the script in our environment. What exact product version you're using? Also, you can query the $result variable, using Get-Member command, in order to see what particular properties are present.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 974 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: File Level Restore Script and Getting a Folder Path

Veeam Logoby tdewin » Wed Apr 16, 2014 1:52 pm

Was curious so tested in my lab. Maybe check if you have "enough permissions" (run powershell as administrator for example). Also check that you are getting back restore point e.g, run with the start-vbrwindowsfilerestore or check if you see your backup being mounted under c:\veeamflr

Code that is working

Code: Select all
asnp veeampssnapin
$result = Get-VBRRestorePoint | ? { $_.name -eq "exchange" -and $_.getbackup().jobtype -eq "Backup"} | sort Creationtime -descending | select -first 1 | Start-VBRWindowsFileRestore
$mountpoint = $($result.mountsession.mounteddevices | ? { $_.Driveletter -eq "C:" }).MountPoint
ls ("{0}\{1}" -f $mountpoint,"windows")


Image
tdewin
Veeam Software
 
Posts: 1047
Liked: 361 times
Joined: Fri Mar 02, 2012 1:40 pm
Full Name: Timothy Dewin

Re: File Level Restore Script and Getting a Folder Path

Veeam Logoby ShawnKPERS » Wed Apr 16, 2014 4:22 pm 1 person likes this post

Thanks for your help and testing, I think this was a PEBKAC problem. I was going off some of the code from the old post combined with my modified code to test and I was testing on the wrong variable. I was able to get .MountSession.MountedDevices to work once I changed the variable to what I had it labeled.

Just in case anyone is interested here is the code I came up with. We use it to get a the latest copy of our production database and copy it over to our one of our testing SQL servers to refresh our testing environment with fresh data. This script is helpful when restoring multiple files to the same destination.

Code: Select all
# Loads Veeam Powershell Snapin
Add-PSSnapin -Name VeeamPSSnapIn

$vmName = "<Restore VM Name>"
$originalFiles = "M:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\DBFile.mdf", "L:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\DBFile_log.ldf"
$destPath = "\\TestSQLServer\mssql\DATA\"

# Loads the latest restore point for the server
$result = Get-VBRBackup | Get-VBRRestorePoint | sort CreationTime -Descending | where {$_.VMName -eq "$vmName"} | select -First 1
$restore = $result | Start-VBRWindowsFileRestore

# Copies the DB files to KTESTDB
foreach ($file in $originalFiles) {
   
    # Find the FLR mount point for the specific drive letter
    $flrmountpoint = ($restore.MountSession.MountedDevices | ? {$_.DriveLetter -eq (Split-Path $file -Qualifier)}).MountPoint

    # Build the path to the file via the FLR mount point
    $file = $flrmountpoint + (Split-Path $file -NoQualifier)

    #Copy/Restore the files to KTESTDB
    Copy-Item $file $destPath -Force
    }

# Stop the FLR process
Stop-VBRWindowsFileRestore  $restore


I ended up changing tsightler code and making "Start-VBRWindowsFileRestore" it's own variable so I could pull info like $result.CreationTimeString from the $result into an email that would let me know for sure what restore point was used to pull the copied files from. I also think this is what tripped me up when I was testing for .MountSession.MountedDevices code.
ShawnKPERS
Enthusiast
 
Posts: 49
Liked: 4 times
Joined: Fri Apr 29, 2011 3:55 pm
Full Name: Shawn Nix

Re: File Level Restore Script and Getting a Folder Path

Veeam Logoby v.Eremin » Wed Apr 16, 2014 4:39 pm

FYI, sometimes it even happens that widely-used parameter isn't present in particular environment. Such issues are typically related to the absence of the latest Windows Manager Framework. (most commonly, on the default version of powershell on Windows 2008R2). So, it's always recommended to install, at least, Windows Framework 3.0.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 974 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

[MERGED] : Restore Windows Guest Files in Powershell

Veeam Logoby baltechnique » Mon Jun 16, 2014 3:50 pm

Hi,

i needed to restore some Windows Guests Files from a Veeam backup. But i needed to do it by script.

So i wrote one, searching from among lot of scripts that did apply to V6 but not to V7.

Here is what i achieved to do:
Code: Select all
$Job = "myBackupJob"
$VM = "myVirtualMachine"
$Files = "C:\myFolderToRestoreFromBackup\"
$Destination = "C:\WhereToPutFilesOnThisServer\"

$result=Get-VBRBackup | where {$_.jobname -eq $Job} | Get-VBRRestorePoint | where {$_.name -eq $VM} | Sort-Object creationtime -Descending | Select-Object -First 1 | Start-VBRWindowsFileRestore
$flrmountpoint = ($result.MountSession.MountedDevices | ? {$_.DriveLetter -eq (split-path -qualifier $Files)})
$file = $flrmountpoint.MountPoint + (split-path -NoQualifier $Files)
Copy $file $Destination -recurse -force
Stop-VBRWindowsFileRestore $result

This script results to restore a folder.
It mounts the Backup from a Veeam Backup, copy the folder with replacement and then Stops the restore process to release the mount point.
baltechnique
Novice
 
Posts: 8
Liked: 1 time
Joined: Mon Sep 30, 2013 7:11 am
Full Name: Network Consulting


Return to PowerShell



Who is online

Users browsing this forum: No registered users and 6 guests