Quantcast
Channel: All Data Protection posts
Viewing all articles
Browse latest Browse all 3468

Re: restoring a file from within a LUN

$
0
0

Hi,

 

I tested the process in my lab, create a few LUNs, mapped them, formatted and mounted. Copied some data to a LUN, created a snapshot, deleted the data, then ran the following script which enabled me to restore the deleted files from the cloned LUN on the host.

 

The powershell code below will enable you to clone a source LUN and map it to an igroup based on a source snapshot name. I would recommend that you consider using WFA for this process instead of script but it's just an example for you. EG:

 

PS C:\Scripts\PowerShell\Projects\CreateLunClone> $credentials = Get-Credential -Credential admin
PS C:\Scripts\PowerShell\Projects\CreateLunClone> .\CreateLunClone.ps1 -Cluster cluster1.testlab.local -VserverName vserver3 -VolumeName iscsi_data_001 -SourcePath "/vol/iscsi_data_001/lun_001" -DestinationVolume iscsi_data_001 -Destination
Path "lun_001_clone" -Snapshot clonetest -SpaceReserved $false -IgroupName igroup_001 -Credentials $credentials
Executed Command: Connect-NcController -Name cluster1.testlab.local -HTTPS -Credential $credentials -ErrorAction Stop
Imported module "DataONTAP"
Executed Command: New-NcClone -Volume iscsi_data_001 -SourcePath lun_001 -DestinationPath lun_001_clone -Snapshot clonetest -DestinationVolume iscsi_data_001 -VserverContext vserver3 -ErrorAction Stop
Created clone of "/vol/iscsi_data_001/lun_001" on vserver "vserver3"
Executed Command: Add-NcLunMap -Path "/vol/iscsi_data_001/lun_001_clone" -InitiatorGroup igroup_001 -VserverContext vserver3 -ErrorAction Stop
Mapped LUN clone "/vol/iscsi_data_001/lun_001_clone" to igroup "igroup_001" on vserver "vserver3"

CLI result:

 

cluster1::> lun show
Vserver   Path                            State   Mapped   Type        Size
--------- ------------------------------- ------- -------- -------- --------
vserver3  /vol/iscsi_data_001/lun_001     online  mapped   windows   10.00GBvserver3  /vol/iscsi_data_001/lun_001_clone online mapped  windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_002     online  mapped   windows   10.00GB
vserver3  /vol/iscsi_data_001/lun_003     online  mapped   windows   10.00GB
4 entries were displayed.

cluster1::> lun mapping show
Vserver    Path                                      Igroup   LUN ID  Protocol
---------- ----------------------------------------  -------  ------  --------
vserver3   /vol/iscsi_data_001/lun_001               igroup_001    0  iscsivserver3   /vol/iscsi_data_001/lun_001_clone         igroup_001    3  iscsi
vserver3   /vol/iscsi_data_001/lun_002               igroup_001    1  iscsi
vserver3   /vol/iscsi_data_001/lun_003               igroup_001    2  iscsi
4 entries were displayed.

Source Code:

 

 

Param(
   [Parameter(Mandatory =$True, HelpMessage = "The cluster name or IP address")]
   [String]$Cluster,
   [Parameter(Mandatory = $True, HelpMessage = "The name of the vserver")]
   [String]$VserverName,
   [Parameter(Mandatory = $True, HelpMessage = "The name of volume where the source and destination files or LUNs reside")]
   [String]$VolumeName,
   [Parameter(Mandatory = $True, HelpMessage = "The relative path of the source file or source LUN inside the volume")]
   [String]$SourcePath,
   [Parameter(Mandatory = $False, HelpMessage = "The relative path of the destination file or destination LUN inside the volume. The destination must be in same volume as the source. If not specified, the destination is taken as the source appended with 'Clone'")]
   [String]$DestinationPath,
   [Parameter(Mandatory = $False, HelpMessage = "The token which was used to reserve split load for creation of file or LUN clones")]
   [String]$TokenUuid,
   [Parameter(Mandatory = $False, HelpMessage = "The snapshot name from which to clone the source file or LUN. If not specified, the contents of the active filesystem will be used")]
   [String]$Snapshot,
   [Parameter(Mandatory = $False, HelpMessage = "The space reservation setting of the destination clone.  By default space reservation is inherited from source")]
   [Bool]$SpaceReserved,
   [Parameter(Mandatory = $False, HelpMessage = "If specified, clone only the base file and ignore streams on source or destination")]
   [Bool]$IgnoreStreams,
   [Parameter(Mandatory = $False, HelpMessage = "If specified, clone even if (advisory/mandatory) byte_range locks or share_mode locks exist on the source or destination")]
   [Bool]$IgnoreLocks,
   [Parameter(Mandatory = $False, HelpMessage = "The quality of service policy group name to assign to the created flexclone file")]
   [String]$QosPolicyGroup,
   [Parameter(Mandatory = $False, HelpMessage = "If specified, clones will be autodeleted to reclaim space when the volume runs out of space")]
   [Bool]$EnableAutodelete,
   [Parameter(Mandatory = $False, HelpMessage = "Specifies whether the clone is being created for backup - that is source file will get periodically modified, but the clone file should not be modified. The default value is false")]
   [Bool]$IsBackup,
   [Parameter(Mandatory = $False, HelpMessage = "The destination volume name")]
   [String]$DestinationVolume,
   [Parameter(Mandatory = $False, HelpMessage = "The number of times to retry commands that return with errors that may succeed after a retry")]
   [Int]$ZapiRetryCount,
   [Parameter(Mandatory = $False, HelpMessage = "The igroup name to map the LUN clone to")]
   [String]$IgroupName,
   [Parameter(Mandatory = $True, HelpMessage = "The credentials to connect to the cluster")]
   [System.Management.Automation.PSCredential]$Credentials
)
#'------------------------------------------------------------------------------
#'Import the DataONTAP PSTK.
#'------------------------------------------------------------------------------
[String]$moduleName = "DataONTAP"
[String]$command    = "Import-Module -Name $moduleName" 
Try{
   Invoke-Expression -Command $command -ErrorAction Stop
   Write-Host "Executed Command`: $command"
   Write-Host "Imported module ""$moduleName"""
}Catch{
   Write-Warning -Message $("Failed Executed Command`: $command. Error " + $_.Exception.Message)
   Write-Warning "Failed importing module ""$moduleName"""
   Break;
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
[String]$command = "Connect-NcController -Name $Cluster -HTTPS -Credential `$credentials -ErrorAction Stop"
Try{
   Invoke-Expression -Command $command -ErrorAction Stop | Out-Null
   Write-Host "Executed Command`: $command"
   Write-Host "Connected to cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed Executed Command`: $command. Error " + $_.Exception.Message)
   Write-Warning "Failed connecting to cluster ""$Cluster"""
   Break;
}
#'------------------------------------------------------------------------------
#'Remove the volume mount point from the source path to create the LUN's relative path.
#'------------------------------------------------------------------------------
If($SourcePath.StartsWith("/vol/$VolumeName/")){
   [String]$SourcePath = $SourcePath -Replace("/vol/$VolumeName/", "")
}
If($SourcePath.StartsWith("/$VolumeName/")){
   [String]$SourcePath = $SourcePath -Replace("/$VolumeName/", "")
}
#'------------------------------------------------------------------------------
#'Create the command to create the file or LUN clone.
#'------------------------------------------------------------------------------
[String]$command = "New-NcClone -Volume $VolumeName -SourcePath $SourcePath "
If($DestinationPath){
   [String]$command += "-DestinationPath $DestinationPath "
}
If($TokenUuid){
   [String]$command += "-TokenUuid $TokenUuid "
}
If($Snapshot){
   [String]$command += "-Snapshot $Snapshot "
}
If($SpaceReserved){
   [String]$command += $("-SpaceReserved `$" + $SpaceReserved + " ")
}
If($IgnoreStreams){
   [String]$command += "-IgnoreStreams "
}
If($IgnoreLocks){
   [String]$command += "-IgnoreLocks "
}
If($QosPolicyGroup){
   [String]$command += "-QosPolicyGroup $QosPolicyGroup "
}
If($EnableAutodelete){
   [String]$command += "-EnableAutodelete "
}
If($IsBackup){
   [String]$command += $("-IsBackup `$" + $IsBackup + " ")
}
If($DestinationVolume){
   [String]$command += "-DestinationVolume $DestinationVolume "
}
If($ZapiRetryCount){
   [String]$command += $("-ZapiRetryCount " + $ZapiRetryCount.ToString() + " ")
}
[String]$command += "-VserverContext $VserverName -ErrorAction Stop"
#'------------------------------------------------------------------------------
#'Invoke the command to create the file or LUN clone.
#'------------------------------------------------------------------------------
Try{
   Invoke-Expression -Command $command -ErrorAction Stop
   Write-Host "Executed Command`: $command"
   Write-Host "Created clone of ""/vol/$VolumeName/$SourcePath"" on vserver ""$VserverName"""
}Catch{
   Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
   Write-Warning -Message "Failed creating clone of ""/vol/$VolumeName/$SourcePath"" on vserver ""$VserverName""" 
   Break;
}
#'------------------------------------------------------------------------------
#'Map the LUN clone to the igroup.
#'------------------------------------------------------------------------------
If($IgroupName){
   [String]$command = "Add-NcLunMap -Path ""/vol/$DestinationVolume/$DestinationPath"" -InitiatorGroup $IgroupName -VserverContext $VserverName -ErrorAction Stop"
   Try{
      Invoke-Expression -Command $command -ErrorAction Stop | Out-Null
      Write-Host "Executed Command`: $command"
      Write-Host "Mapped LUN clone ""/vol/$DestinationVolume/$DestinationPath"" to igroup ""$IgroupName"" on vserver ""$VserverName"""
   }Catch{
      Write-Warning -Message $("Failed Executing Command`: $command. Error " + $_.Exception.Message)
      Write-Warning -Message "Failed mapping LUN clone ""/vol/$DestinationVolume/$DestinationPath"" to igroup ""$IgroupName""  on vserver ""$VserverName""" 
   }
}
#'------------------------------------------------------------------------------

Note: I highly recommend you consider using WFA as an orchestration application for process like this instead of a script as it enables centralised RBAC access, a REST API, logging, auditing etc (Also It's FREE)

 

/Matt

 

 


Viewing all articles
Browse latest Browse all 3468

Trending Articles