08 July, 2010

Live Xen Backup using a batch job

Hi,

I like batch jobs more than VBS, I'm old fashion. :)
I wrote a script to backup Xen Server guests machines using batch.

The script will clean old snapshots with a single SR, then will try to create a snapshot with  quiesce (using VSS) and if not successful, will do a regular backup.

It contains 4 files:
1. VM_List.exe - the list of the vm names you want to backup (one per line)
2. Step1.cmd - the file you need to run, will enumerate the list of machines to backup
3. Step2.cmd - a command to delete all the old snapshots (more complicated that it sounds)
4. Step3.cmd - the actual backup script

and now the content of the files (VM_List.exe I believe you can create yourself):
Replace [] with your data.

Step1.cmd:
=====================================
echo off
set server=[pool master ip]
set user=root
set password=[password]
call XenBackup-Snapshots-step2.cmd > log_snap.txt
for /f %%A in (vm_list.txt) do (XenBackup-Snapshots-step3.cmd %%A >>log_snap.txt)
=====================================

Step2.cmd: (I assume you have 1 SR, if you have more just replicate this script per SR)
=====================================
echo === deleting old snapshot ===
echo Start at:
echo %date% - %time%
:start
C:\Progra~1\Citrix\XenCenter\xe.exe -s %server% -u %user% -pw %Password% vdi-list is-a-snapshot=true sr-uuid=[sr-uuid] --minimal > vdi.txt
for /f "tokens=1 delims=," %%G in (vdi.txt) do (
if [%%G] EQU [] goto end
C:\Progra~1\Citrix\XenCenter\xe.exe -s %server% -u %user% -pw %Password% vdi-destroy uuid=%%G
echo.
goto start
)
:end
=====================================

Step3.cmd:
=====================================
echo off
setLocal EnableDELAYedExpansion
title ***** Working on %1 *****
echo ***** Start working on %1 *****
echo.
echo === Start Backup for %1 ===
echo.
echo Start at:
echo %date% - %time%
set vm_uuid=
set template_uuid=
"C:\Program Files\Citrix\XenCenter\xe.exe" -s 192.168.120.10 -u %user% -pw %Password% vm-list name-label="%1" --minimal > vm_uuid.txt
set /p vm_uuid=

set snapshot-type=vm-snapshot-with-quiesce
:snapshot
C:\Progra~1\Citrix\XenCenter\xe.exe -s %server% -u %user% -pw %Password% %snapshot-type% new-name-label=backup uuid=%vm_uuid% > template_uuid.txt
set /p template_uuid=

set C=-
set N=
:loop
if !template_uuid:~0^,1! equ !C! (
set /a N+=1
)
if "!template_uuid:~1!" neq "" (
set template_uuid=!template_uuid:~1!
goto :loop
)
if !N! NEQ 4 do (
if %snapshot-type%==vm-snapshot goto no_snap else
set snapshot-type=vm-snapshot
goto snapshot
)

C:\Progra~1\Citrix\XenCenter\xe.exe -s %server% -u %user% -pw %Password% template-export template-uuid=%template_uuid% filename=d:\VM_Backup\%1.xva
echo.

:no_snap
echo NO SNAPSHOT !!!
:emd
echo.
echo %date% - %time%
echo ***** End working on %1 *****
echo.

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

No comments:

Post a Comment