Showing posts with label disaster recovery. Show all posts
Showing posts with label disaster recovery. Show all posts

26 August, 2010

XenServer disaster recovery script in batch

Hi all,
I wanted to make my life easier when the disaster will hit (and between you and me it allays a meter of time) so I wrote a menu driven disaster recovery script.
The script is covering:
  1. Pool Master failure
  2. Host Member Failure
  3. resetting the power state of VM's
  4. Recovering them to another HOST
My goal was giving a simple name based actions rather than using UUID's.

You will need 2 more files that create your environment:
  1. host_list.txt - all the hosts and their role, one each line. example: [servername, member]
  2. vm_list.txt - all the vm
Those 2 files are case sensitive so be sure to write the names correctly.
Note: in host_list.txt the role should be in lower letters (member/master)

To help you I saved it also as a GoogleDoc.

Any comments/improvements will be welcomed...

================================
Echo off
title "Xen Recover for 5.5 - at any time hit Cntrl+C and then Y to exit the script."

set user=[]
set password=[]
set server=[]

cls

echo This is a disaster recover script for Xen Server.
echo.
echo Host Infrastructure:
for /f "tokens=1,2 delims=," %%A in (Host_list.txt) do (echo Host: %%A - %%B)
echo.
echo Guest Machines:
for /f %%A in (VM_list.txt) do (echo VM: %%A)
echo.
echo.
echo is this OK ? (1-yes)
set /P C=
if %C%==1 goto continue
goto EOF

:continue


cls
echo Good.
echo What do you want to do ?
echo 1 - Recover from pool master failure
echo 2 - Recover from member failure
set /P C=
if %C%==1 goto master_failure
if %C%==2 goto member_failure


:master_failure
echo To which server to you want to transfer the pool master role ? (IP address only)
set /P C=
echo transfer pool master to %C%
pause
echo pool move to %C%
set server=%C%
echo on
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% pool-emergency-transition-to-master
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% pool-recover-slaves
Echo off
:master_failure_2
echo Type the new pool master server name: (case sensitive)
for /f "tokens=1,2 delims=," %%A in (Host_list.txt) do (
    if %%B==member (echo Host: %%A)
)
set /P xenhostname=
set caller=master_failure_3
goto check_host
:member_failure_3
if "%host_ok%"=="yes" (goto member_failure_4)
echo The Host name is not on the list
pause
goto member_failure_2
set failed-host=%xenhostname%
goto power_reset


:member_failure
echo Which member server has failed ? (type the name - case sensitive)
echo Showing only members.
for /f "tokens=1,2 delims=," %%A in (Host_list.txt) do (
    if %%B==member (echo Host: %%A)
)
echo.
echo It seems that the following host is down:
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% host-list host-metrics-live=false params=name-label
set /P xenhostname=
set caller=member_failure_2
goto check_host
:member_failure_2
if "%host_ok%"=="yes" (goto member_failure_3)
echo The Host name is not on the list
pause
goto member_failure
:member_failure_3
set failed-host=%xenhostname%
goto power_reset


:power_reset
echo resetting the power state on the machines that was on the failed host [%failed-host%]
pause
echo get host uuid
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% host-list name-label=%xenhostname% --minimal > host_uuid.txt
set /p host_uuid=
echo resetting power on VM's residing on %xenhostname% [uuid=%host_uuid%]
echo on
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% vm-reset-powerstate resident-on=%host_uuid% --force --multiple
Echo off
goto VM_recover

:vm_recover
echo Which VM do you need to recover ?
for /f %%A in (VM_list.txt) do (echo %%A)
echo it seems that the following VM's were on the failed host:
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% vm-list is-control-domain=false resident-on=%host_uuid% params=name-label
set /P vmname=
set caller=vm_recover_2
goto check_vm
:vm_recover_2
if "%vm_ok%"=="yes" (goto vm_recover_3)
echo The VM name is not on the list...
pause
goto vm_recover
:vm_recover_3
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% vm-list name-label=%vmname% --minimal > vm_uuid.txt
set /p vm_uuid=
:vm_recover_4
echo Recover to which HOST ?
for /f %%A in (host_list.txt) do (echo %%A)
echo.
echo Note: %failed-host% has failed and cannot be choosen.
set /P xenhostname=
if "%xenhostname%"==%failed-host% (
    echo %failed-host% has failed and cannot be choosen.
    goto :vm_recover_4
)
set caller=vm_recover_5
goto check_host
:vm_recover_5
if "%host_ok%"=="yes" (goto vm_recover_6)
echo The HOST name is not on the list...
pause
goto vm_recover_4
:vm_recover_6
"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% host-list name-label=%E% --minimal > host_uuid.txt
set /p host_uuid=
echo recover %vmname% [%vm_uuid%] on %xenhostname% [%host_uuid%]
echo on

"C:\Program Files\Citrix\XenCenter\xe.exe" -s %server% -u %user% -pw %Password% xe vm-param-set uuid=%vm_uuid% affinity=%host_uuid%
Echo off
echo.
echo Recover another VM ? (1-yes)
set /P C=
if %C%==1 goto vm_recover
goto end



rem *** general functions ***
:check_host
set host_ok=
for /f "tokens=1 delims=," %%A in (Host_list.txt) do (
    if "%xenhostname%"=="%%A" (set host_ok=yes)
)
goto %caller%

:check_vm
set vm_ok=
for /f %%A in (vm_list.txt) do (
    if "%vmname%"=="%%A" (set vm_ok=yes)
)
goto %caller%


:end

rem *** cleanup ***
del host_uuid.txt /q
del vm_uuid.txt /q
pause

:EOF
echo *** written by snir hoffman http://snirh.blogspot.com ***

==========================================