Chris Aylott

Automatically Mount Most Recent Snapshot and Connect to Target

Discussion created by Chris Aylott Employee on Feb 6, 2014
Latest reply on Oct 21, 2014 by Jason Liu

HI All,

 

Hope it's useful to someone. Uses batch scripting, it's simple, working and tested.

 

Applications: Clone and mount most recent snapshot...

  1. for development environment, scheduled it to mount the most recent snap at a DR environment so that devs would be working of the most recent data.
  2. for backup
  3. TODO - for backup and disable cache to prevent cache churn.
  4. TODO - clone all recent snapshots for a volume collection and mount on ESX to simulate DR test without affecting replication.

or simply use it to quickly clone the most recent snapshot for a volume

 

Behaviour:,

BATCH - Finds most recent snapshot for the volume you specify, clones it (sets it online), fetches the new iqn which can then be passed into a powershell script to either connect the cloned snapshot volume to ESX or a Windows box. Uses text files to filter the results.

POWERSHELL - Connects only the new volume target IQN and sets any offline disk online. From testing it will retain any drive letter previously associated. !! Make sure you limit your volumes by intiator groups for security, as is best practice. !!

> can be run without executing anything to ensure you return the expected results.

 

Environment:

It's a batch script which executes a powershell script. Tested on MS2012 (will work on 2008 however the the ps1 will have to be changed) with array versions 1.4.x and 2.0.x. Run this script from the server you want to mount the volume onto.

 

Useful references:

For ps scripts using the iscsi cmdlets Windows PowerShell  PS >_: New iSCSI cmdlets in PowerShell V3 Part - 1

 

Disclaimer:

Test it yourself use at your own peril ... etc etc. Read the comments.

 

Usage:

  1. Create a password-less connection to the array by generating a SSH key between the server and array.
    1. Install plink, puttyGEN and putty into c:\folder - PuTTY Download Page
    2. Run puttyGen selecting 1024 bits, see screenshot
    3. Save the key, selecting no to any passwords.
    4. Copy the key exactly as shown below.

generate-ssh.jpg

     5. Connect to the array via putty using the login admin and your GUI password.

     6. sshkey --add <name> --type rsa --key <paste the ssh key from previous step>

     7. Open putty again prefix the array ip address with admin@ set the auth to SSH and point it at the key you saved in step 3, save the session with the name of your array.

> You can now run commands on the array via plink directly from the machine.

putty-auth.jpg

2. Open powershell as administrator run Set-ExecutionPolicy Unrestricted > Read what this means to you! Using the Set-ExecutionPolicy Cmdlet

3. Save the code below into c:\folder and rename i.e. recent-snapshot.bat save the powershell script as connect-target.ps1

4. Change the modifications in the script, add the session name of putty from step 1.7 above to the array-name=XXXXX

5. Execute - on first run comment out the sections where the snapshot is actually cloned to ensure you get the expected results.

    name.bat VOLUME-NAME

    i.e. C:\folder\recent-snapshot AA-SQL01-DB01

 

Batch file

:: This script will return the most recent snapshot for the volume and clone it, which sets it online.
:: Run the bat file followed by the name of the volume i.e. recent-snapshot.bat AA-GOLD-EXCH01-DB01
:: Once the volume has been created you can discover it on the host and connect to it.
:: This can be done automatically however if scheduled the volume target iqn changes, increases by 1 every time.


:: NUMBER OF MODIFICATIONS 5:
:: MOD 1. Having setup a SSH key with your array define the putty session name (array name) below
:: MOD 2. If you are mounting a snapshot for a backup and do not want it to churn your cache set to true NOT IN USE
@ echo off
set array-name=array-01
set full-backup=false
:: paramater taken from the command line - do not change
set volume=%1


  echo Using the session %array-name%
  echo .
  echo .


:: MOD 3. If this is not a scheduled task you will want to comment the lines below to set the clone volume offline and delete it.
:: NOTE: there is a 20 second wait command, the ping, to allow the command to complete successfully
:: This is where you would invoke a powershell command if it is mounted in ESX | MS, on testing this is not neccessary for MS.
echo Removing the existing snapshot clone-%volume%
  echo .
  echo .
plink %array-name% vol --offline clone-%volume% --force
ping 192.0.2.2 -n 1 -w 20000 > nul
plink %array-name% vol --delete clone-%volume%
ping 192.0.2.2 -n 1 -w 20000 > nul


plink %array-name% snap --list > snap-list.txt
:: export the snapshot list
findstr /B /I %volume% c:snap-list.txt > snap.txt
:: find the snapshots related to the volume


for /f "tokens=2 delims= " %%a in (snap.txt) do (
:: retrieve the most recent volume collection name
  set mostrecent=%%a
  goto :break
  )
  :break
  echo Mounting the snapshot %mostrecent% for volume %volume% it will be called clone-%volume% it will take a minute or so to appear within the GUI.
  echo .
  plink %array-name% vol --clone %volume% --snapname %mostrecent% --clonename clone-%volume%


:: MOD 4. If you don't need the IQN to pass onto Powershell to automatically mount the volume then comment out the section below.
ping 192.0.2.2 -n 1 -w 60000 > nul
:: wait for the volume to be cloned
plink %array-name% vol --info clone-%volume% > vol-info.txt
:: get the new volume information
findstr /B /I iSCSI  c:vol-info.txt > iscsi.txt
:: find the right line
for /f "tokens=3 delims= " %%a in (iscsi.txt) do (
:: get the IQN of the new volume created this can be passed into a powershell script for esx or MS.
  echo %%a
  set iqn=%%a
  )
:: MOD 5. from here you pass the parameter into a Vmware or MS powershell script. Script below will set the new volume online on MS 2012 only!.
:: The first time you mount the volume assign a drive letter and it will use the same letter every time, or you can add the command to the PS script.
:: NOTE: On testing we found you need to restart the SQL service, add it to the PS script.


powershell.exe -file c:\chris\connect-target.ps1 "%iqn%"

exit /b




 

Powershell to connect to target

param(
[string]$a
)
Write-Host $a
Get-IscsiSession | Register-IscsiSession
Connect-IscsiTarget -NodeAddress "$a"
Get-Disk | ?{$_.operationalstatus -eq "offline"} | Set-Disk -IsOffline 0
Get-Disk | ?{$_.operationalstatus -eq "online"}




 

NOTE: Initiator groups and ACL's are not replicated, if run at a DR site these will have to be defined.

Outcomes