Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- V2.0
- A Scirpt to backup XenServer VM's to Windows file shares.
- .DESCRIPTION
- This script will take a snapshot of the selected VM's on XenServer. Then it will export the snapshot to a fileshare. You can choose to send a log file trough email. And ZIP the backup.
- .NOTES
- File Name : Invoke-XenServerBackup.ps1
- Author : Chris Twiest - [email protected]
- To get PowerShell working with Citrix XenServer you need to download and install the XenServer SDK from here https://www.citrix.com/downloads/xenserver/product-software/xenserver-72-standard-edition.html under the Development Components section
- .LINK
- https://workspace-guru.com
- .EXAMPLE
- Invoke-XenBackup -ExportPath "C:\Backup" -VMnames "VM01" -username "root" -password "P@ssw0rd" -XenHost "XenHost01" -LogPath "C:\temp"
- This will backup VM01 from XenHost01 in C:\Backup. Username and Password are for XenHost01.Logfile will be written in C:\Temp
- .EXAMPLE
- Invoke-XenBackup -ExportPath "C:\Backup" -VMnames "VM01","VM02","VM03" -username "root" -password "P@ssw0rd" -XenHost "XenHost01" -LogPath "C:\Temp" -Email -SMTPServer "smtp.domain.com" -FromEmail "[email protected]" -ToEmail "[email protected]" -ZIP
- This will backup VM01, VM02 and VM03 from XenHost01 in C:\Backup. Username and Password are for XenHost01.
- After the backup the folder will be ZIP'd and the backup logfile will be send in a email to [email protected]
- #>
- Function Invoke-XenBackup {
- [CmdletBinding()]
- param (
- [Parameter(Mandatory = $True)]
- [string]$ExportPath,
- [array]$VMnames,
- [string]$username,
- [string]$password,
- [string]$XenHost,
- [string]$LogPath,
- [Parameter(Mandatory = $false)]
- [switch]$Email,
- [string]$SMTPServer,
- [string]$FromEmail,
- [string]$ToEmail,
- [switch]$Zip
- )
- #### If Export or Log path is network drive ###
- if ($Exportpath -like "\\*") {
- $ExportpathORG = $ExportPath
- $testnetworklocation = $Exportpath | test-path
- if (!$testnetworklocation) {
- write-host "Can't acces networkdrive make sure the script is run with the correct rights"
- }
- else {
- write-host "$ExportPath is Network drive, will mount drive"
- $share = "$Exportpath"
- $drvlist = (Get-PSDrive -PSProvider filesystem).Name
- Foreach ($drvletter in "DEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()) {
- If ($drvlist -notcontains $drvletter) {
- New-PSDrive -PSProvider filesystem -Name $drvletter -Root $share -Persist | out-null
- break
- }
- }
- $Exportpath = $drvletter + ":"
- }
- }
- if ($logpath -like "\\*") {
- $LogPathOrg = $LogPath
- $testnetworkloglocation = $Logpath | test-path
- if (!$testnetworkloglocation) {
- write-host "Can't acces networkdrive make sure the script is run with the correct rights"
- }
- else {
- write-host "$LogPath is Network drive, will mount drive"
- $logshare = "$logpath"
- $logdrvlist = (Get-PSDrive -PSProvider filesystem).Name
- Foreach ($logdrvletter in "DEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()) {
- If ($logdrvlist -notcontains $logdrvletter) {
- New-PSDrive -PSProvider filesystem -Name $logdrvletter -Root $logshare -Persist | out-null
- break
- }
- }
- $LogPath = $logdrvletter + ":"
- }
- }
- Import-Module XenServerPSModule
- $env:TMP = $ExportPath
- $env:TEMP = $Exportpath
- #### Create Logfile #######
- $datelogfile = Get-Date -format yyyy-dd-MM-hh-mm-ss
- $logfile = "$LogPath\XenBackupLog_$datelogfile.txt"
- $testlogpath = "$LogPath" | test-path
- if ($testlogpath -like "False") {
- New-Item -ItemType Directory -Path "$LogPath"
- }
- New-Item -ItemType File -Path "$logfile" | Out-Null
- #### Create Backup Folder ######
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Start Backup"
- Add-Content $logfile "$Date --- Start Backup for $VMNames to $Exportpath"
- $backupdate = Get-Date -format yyyy-MM-dd
- $Testexportpath = $ExportPath | Test-Path
- if (!$Testexportpath) {
- New-Item -ItemType Directory -Path "$ExportPath" | out-null
- }
- $Testbackuppath = "$ExportPath\XenBackup-$backupdate" | Test-Path
- if (!$Testbackuppath) {
- New-Item -ItemType Directory -Path "$ExportPath\XenBackup-$backupdate" | out-null
- }
- Import-Module XenServerPSModule
- Try {
- $Session = Connect-XenServer -Url https://$Xenhost -UserName $username -Password $password -NoWarnCertificates -SetDefaultSession
- }
- Catch [XenAPI.Failure] {
- [string]$PoolMaster = $_.Exception.ErrorDescription[1]
- Write-Host -ForegroundColor Red "$($Pools.$Pool) is slave, Master was identified as $PoolMaster, trying to connect"
- $Pools.Pool = $PoolMaster
- $Session = Connect-XenServer -url "http://$PoolMaster" -UserName $username -Password $password -NoWarnCertificates -SetDefaultSession
- }
- #### Create Snapshot and backup folder for each VM ####
- foreach ($VMname in $VMnames) {
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Create Snapshot $VMname"
- Add-Content $logfile "$Date --- Create Snapshot $VMname"
- $Testbackupvmpath = "$ExportPath\XenBackup-$backupdate\$VMName" | Test-Path
- if ($testbackupvmpath -like "False") {
- New-Item -ItemType Directory -Path "$ExportPath\XenBackup-$backupdate\$VMName" | out-null
- }
- & {
- $VMUuid = get-XenVM -Name $VMName | Select-Object UUID
- Invoke-XenVM -Uuid $VMUUid.uuid -XenAction Snapshot -NewName "$VMName-Backup-$backupdate"
- }2>&1 | Tee-Object $logfile -append
- }
- #### Export and Remove Snapshot #####
- foreach ($VMname in $VMnames) {
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Export Snapshot $VMname"
- Add-Content $logfile "$Date --- Export Snapshot $VMname"
- $SnapshotUuid = get-XenVM -Name "$VMName-Backup-$backupdate" | select UUID
- #Set-XenVM -Uuid $SnapshotUuid.uuid -IsATemplate $false -HaAlwaysRun $false
- Export-XenVm -Uuid $SnapshotUuid.uuid -XenHost "$XenHost" -path "$ExportPath\XenBackup-$backupdate\$VMName\$VMName-backup.xva"
- $pathtest = "$ExportPath\XenBackup-$backupdate\$VMName\$VMName-backup.xva" | test-path
- if ($pathtest -like "False") {
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- ERROR ---- Export Snapshot $VMname failed"
- Add-Content $logfile "$Date --- ERROR ---- Export Snapshot $VMname failed"
- }
- else {
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Export Snapshot $VMname finished"
- Add-Content $logfile "$Date --- Export Snapshot $VMname finished"
- }
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Delete Snapshot $VMname"
- Add-Content $logfile "$Date --- Delete Snapshot $VMname"
- $snapshotName = "$VMName-Backup-$backupdate"
- Try { $SnapShot = Get-XenVM -Name $SnapshotName | Where is_a_snapshot } Catch { "$SnapshotName was not found on $XenHost" }
- Try { $SnapShot | Select -ExpandProperty VBDs | Get-XenVBD | Select -ExpandProperty VDI | where {$_ -ne "OpaqueRef:NULL"} | Remove-XenVDI } Catch { " --> $($_.Exception.Message)"}
- Try { Remove-XenVM -Uuid $SnapShot.uuid } Catch { return " --> $($_.Exception.Message)" }
- }
- #### ZIP Backup #####
- if ($zip) {
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Start ZIP of Backup"
- Add-Content $logfile "$Date --- Start ZIP of Backup"
- $source = "$ExportPath\XenBackup-$backupdate"
- $destination = "$ExportPath\XenBackup-$backupdate.zip"
- If (Test-path $destination) { Remove-item $destination }
- Add-Type -assembly "system.io.compression.filesystem"
- [io.compression.zipfile]::CreateFromDirectory($Source, $destination)
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Delete non ZIPd backup"
- Add-Content $logfile "$Date --- Delete non ZIPd backup"
- Move-Item $destination $ExportPath
- Get-ChildItem -Path $Source -Recurse | Foreach-object { Remove-item -Recurse -path $_.FullName }
- Remove-item $source
- }
- #### Backup Finished ###
- $date = Get-Date -format yyyy-dd-MM-hh-mm-ss
- Write-Host "$Date --- Backup done"
- Add-Content $logfile "$Date --- Backup done"
- #### Email Log ####
- if ($Email) {
- $MessageContent = "
- <head>
- <style>
- body {
- font-family: Arial;
- font-size: 12px;
- }
- </style>
- </head>
- <BODY>
- <P>To Whom It May Concern,<BR>
- <BR>
- XenServer Backup of $VMnames to $ExportPath has finished on $date check attached logfile for more info.
- <P>
- Greetings,<BR>
- <BR>
- XenServer Backup Script
- </P>
- </BODY>
- "
- $messageSubject = "XenServer Backup $(Get-Date -UFormat "%A %d %B %Y") is done."
- $body = $MessageContent
- send-mailmessage -from $FromEmail -to $ToEmail -subject $messageSubject -body $body -BodyAsHtml -smtpServer $SMTPServer -Attachments "$Logfile"
- }
- #### If Export or Log path is network drive ###
- if ($ExportpathORG -like "\\*") {
- write-host "$ExportPath is Network drive, will dismount drive"
- Remove-Psdrive -Name $drvletter
- }
- if ($LogPathOrg -like "\\*") {
- write-host "$LogPath is Network drive, will dismount drive"
- Remove-Psdrive -Name $logdrvletter
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment