Skip to content

Instantly share code, notes, and snippets.

@nikiink
Last active December 11, 2018 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikiink/cdc687d1bbe227e5cfa3e5759c7f7214 to your computer and use it in GitHub Desktop.
Save nikiink/cdc687d1bbe227e5cfa3e5759c7f7214 to your computer and use it in GitHub Desktop.
Powershell script for backup Xenserver and XCP-ng VMs while running (tested with Xenserver 7.0 and XCP-ng 7.5 VMs)
# This script reads virtual machines to backup from an xml
# configuration file by default named XenBackup.xml.
# The configuration file can be given as parameter -config, for example
#
# XenBackup.ps1 -config "XenBackup-test.xml"
#
# [If not given is used the file XenBackup.xml]
#
# The values of maxBackups and backupDir
# can be overridden on specific machine.
# This is an example:
#
# <?xml version="1.0"?>
# <XenBackup>
# <backupDir>C:\BACKUPS</backupDir>
# <maxBackups>2</maxBackups>
# <logFile>XenBackup.log</logFile>
# <server host="xenserver001" user="root" password="password">
# <vm name="TEST2"/>
# <!-- override maxBackups and backupDir -->
# <vm name="Test VM" maxBackups="2" backupDir="C:\BACKUPS2"/>
# <vm name="Ubuntu Xenial" />
# <vm name="Knoppix" />
# </server>
# <server host="xenserver002" user="root" password="password">
# <vm name="Knoppix2" />
# </server>
# </XenBackup>
param (
[string]$config = "XenBackup.xml"
)
$configFile = $config
Write-Host("READING CONFIGURATION FROM FILE: " + $configFile)
[xml]$config = Get-Content $configFile
Import-Module XenServerPSModule
function go() {
Write-Log("CONFIGURATION LOADED FROM: " + $configFile)
foreach( $server in $config.XenBackup.server ) {
Connect-XenServer -NoWarnCertificates -NoWarnNewCertificates -url ("https://" + $server.host) $server.user $server.password
Write-Log("CONNECTED TO SERVER: " + $server.host)
foreach($vm in $server.vm) {
Write-Log("BACKUPING VM: " + $vm.name)
backupVm $server $vm
cleanOldBackups $vm
}
Disconnect-XenServer
Write-Log("DISCONNECTED FROM SERVER: " + $server.host)
}
}
function backupVm($server, $vm) {
$bckdir = $config.XenBackup.backupDir
if ($vm.backupDir) { $bckdir = $vm.backupDir }
$timestamp = (Get-Date -Format "yyyy-MM-ddTHHmmss")
$snapshot_name = $vm.name + "_bck_$timestamp"
#Create temporary snapshot for hot backup
Invoke-XenVM -Name $vm.name -XenAction Snapshot -NewName $snapshot_name
$snapshot = Get-XenVM -Name $snapshot_name
#Set is-a-template and ha-always-run to false
Set-XenVM -Uuid $snapshot.uuid -IsATemplate $false -HaAlwaysRun $false
#Export snapshot
setTempDir $bckdir
Export-XenVM -Uuid $snapshot.uuid -XenHost $server.host -Path ($bckdir + "\" + $vm.name + "-${timestamp}.xva")
#Get disk snapshots (for removal)
$vdis = @()
foreach($vbd in $snapshot.VBDs) {
if((Get-XenVBDProperty -Ref $vbd -XenProperty Mode) -eq [XenAPI.vbd_mode]::RW) {
$vdis += Get-XenVBDProperty -Ref $vbd -XenProperty VDI
}
}
#Remove vm snapshot (this does not remove disk snapshots)
Remove-XenVM -Uuid $snapshot.uuid
#Remove disks snapshots
foreach($vdi in $vdis) {
Remove-XenVDI -VDI $vdi
}
}
function setTempDir($newdir) {
$env:TMP = $newdir
$env:TEMP = $newdir
}
function cleanOldBackups($vm) {
$maxbkps = $config.XenBackup.maxBackups
$bckdir = $config.XenBackup.backupDir
if ($vm.maxBackups) { $maxbkps = $vm.maxBackups }
if ($vm.backupDir) { $bckdir = $vm.backupDir }
Write-Log("MAXBACKUPS: " + $maxbkps)
$vmbackupfiles = Get-ChildItem -Path $bckdir -File -Filter ($vm.name + "-*.xva") | sort name -Descending
$count = 0
foreach( $f in $vmbackupfiles ) {
$count++
Write-Log($count)
if ($count -gt $maxbkps) {
Write-Log("DELETING " + $f)
Remove-Item -Path ($bckdir + "\" + $f)
} else {
Write-Log("LEAVING " + $f)
}
}
}
Function Write-Log {
Param ([string]$message)
$logfile = $config.XenBackup.logFile
$stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$line = "$stamp - $message"
Add-content $logfile -Value $line
}
go
<?xml version="1.0"?>
<XenBackup>
<backupDir>C:\BACKUPS</backupDir>
<maxBackups>2</maxBackups>
<logFile>XenBackup.log</logFile>
<server host="xenserver001" user="root" password="password">
<vm name="TEST2"/>
<!-- override maxBackups and backupDir -->
<vm name="Test VM" maxBackups="2" backupDir="C:\BACKUPS2"/>
</server>
<server host="xenserver002" user="root" password="password">
<vm name="Filemaker" maxBackups="3"/>
</server>
<server host="xenserver003" user="root" password="password">
<vm name="Ubuntu Xenial" />
<vm name="Knoppix" />
</server>
</XenBackup>
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2016-10-26T09:15:12.7978673</Date>
<Author>Administrator</Author>
</RegistrationInfo>
<Triggers />
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-file XenBackup.ps1 -config XenBackup.xml</Arguments>
<WorkingDirectory>C:\XenBackup</WorkingDirectory>
</Exec>
</Actions>
</Task>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment