PS: Start-HVVEEAMZip
This script creates and maintains VEEAM Zip backups of virtual machines hosted on a single Hyper-V server. It is useful for test environments and fully functional with the VEEAM Free edition.
You have the ability to do some versioning of your backed up machines as the script takes care of a retention period. Also it uses the VEEAM backup encryption to encrypt the backups. To ensure data consistency a sha512 checksum is created of each created backup file.
The script is designed to backup all machines that are in the state "Running" in Hyper-V for machines that are set up to non-production checkpoints it shuts down the VM before the backup.
It creates a folder structure for each backup taken. By default this structure uses the format yyyyMMdd below the base folder.
Requirements
- Windows Server 2012R2 an higher
- VEEAM Backup and Replication (Tested with the following versions)
- 9.5 (Free)
Usage
This script can be included in a scheduled task.
Version History
- 1.0.0: Initial release
Download
>> Version 1.0.0 (current)
(MD5: c9bd41bd15bc48d8920b4f58f50a5a00)
(SHA1: 50b7103c837da32b2986891be539fd8c256b9435)
(SHA256: 22330eecb8275a449575f68332662ff5546bb3e6987721ed0cc5ff3e84ba27a6)
Script code:
##------------------------------------------------------------------------------------------------
##
## Start-HVVEEAMZip.ps1
##
## Version 1.0.0
##
##
## Copyright (c) 2017 Martin Mueller - www.sh-soft.com
##
## Permission is hereby granted, free of charge, to any person obtaining a copy of this software
## and associated documentation files (the "Software"), to deal in the Software without
## restriction, including without limitation the rights to use, copy, modify, merge, publish,
## distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in all copies or
## substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
## BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
## DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
## (The MIT License (MIT))
##
##------------------------------------------------------------------------------------------------
<#
.SYNOPSIS
This script creates and maintains VEEAM Zip Backups of virtual machines hosted on a single Hyper-V
server. It is useful for test environments and fully functional with the VEEAM Free edition.
You have the ability to do some versioning of your backed up machines as the script takes care of
a retention period. Also it uses the VEEAM backup encryption to encrypt the backups. To ensure
data consistency a sha512 checksum is created of each created backup file.
The script is designed to backup all machines that are in the state "Running" in Hyper-V
For machines that are set up to non-production checkpoints it shuts down the VM before the backup.
It creates a folder structure for each backup taken. By default this structure uses the format
yyyyMMdd below the base folder.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Be aware that the script will delete all folder older then the defined BackupAge in the base folder !
! so use a dedicated folder as FolderBase for this script and its backups. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#>
##------------------------------------------------------------------------------------------------
## Configuration parameters for the script
##------------------------------------------------------------------------------------------------
# IP or servername of the Hyper-V server as it is configured in VEEAM
$HVServer = "1.2.3.4"
# Backup Only machines in the folliwing state (use * for all)
$VMBackupState = "Running"
# Max age in days of the backups
$BackupAge = 7
# Base folder for the backups
# ! Warning use a folder dedicated for this script as it deletes all folders older then the backup
# ! age defined in the script
$FolderBase = "C:\Backup\VMs"
# Backup folder DateTime format
$BackupDTFormat = "yyyyMMdd"
# VEEAM encryption password (Decscription of the saved password)
$VEEAMEncryptionKeyDescription = "HighEndPassword"
###================================================================================================###
### ###
### MAIN SCRIPT PART - no change should be required from here on... ###
### ###
###================================================================================================###
##------------------------------------------------------------------------------------------------
## Internal variables
##------------------------------------------------------------------------------------------------
$TodayTarget = Join-Path -Path $FolderBase -ChildPath (get-date -Format "$BackupDTFormat")
VMStatus = @{}
$deleteolderThen = (Get-Date).AddDays(0-$BackupAge).Date
##------------------------------------------------------------------------------------------------
## Internal functions
##------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------
# Hash (SHA512)
function Create-FileHash ($FileName) {
try {
$CProvider = new-object System.Security.Cryptography.SHA512CryptoServiceProvider
$file = [System.IO.File]::Open($FileName,[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read)
$HASH = (([System.BitConverter]::ToString($CProvider.ComputeHash($file))).Replace("-", "")).ToLower()
$file.Dispose()
return ($HASH)
}
catch {
return ("[ERROR]")
}
}
#------------------------------------------------------------------------------------------------
# Include VEEAM Snapin
Add-PSSnapin -Name VeeamPSSnapIn -ErrorAction SilentlyContinue
##------------------------------------------------------------------------------------------------
## Start execution
#------------------------------------------------------------------------------------------------
# Clean up old backups
Write-Host -ForegroundColor Green "`n`nCleaning up old backups...`n"
Get-ChildItem $FolderBase -Directory | Select-Object -Property FullName, CreationTime | ForEach-Object {
if ($_.CreationTime.Date -lt $deleteolderThen) {
Write-Host -ForegroundColor Yellow "`tDELETE: " -NoNewline
Write-Host $_.FullName -NoNewline
try {
Remove-Item -Path "$($_.FullName)" -Recurse -Force -Confirm:$false
Write-Host -ForegroundColor Green " ... done :-)"
}
catch {
Write-Host -ForegroundColor red " ... failed :-("
}
}
else {
Write-Host -ForegroundColor Yellow "`tKEEP: " -NoNewline
Write-Host $_.FullName
}
}
#------------------------------------------------------------------------------------------------
# Start VEEAM ZIP backup
Write-Host -ForegroundColor Green "`n`nBacking up VMs...`n"
Get-VM | Where-Object {$_.State -eq "$VMBackupState"} | Select-Object -Property Name, CheckpointType | ForEach-Object {
$VMName = $_.Name
$Type = $_.CheckpointType
Write-Host -ForegroundColor Yellow "VEEAMZip VM: " -NoNewline
Write-Host $VMName -NoNewline
$VMstatus[$VMName]="Started"
# Shit down vm if no production backup is possible
if ($Type -ne "Production") {
$VMstatus[$VMName]="Stopping"
Stop-VM -Name "$VMName"
while ((Get-VM -Name "$VMName" | Select-Object -ExpandProperty State) -eq "Running") {
Start-Sleep -Seconds 1
Write-Host -ForegroundColor Yellow "." -NoNewline
}
$VMstatus[$VMName]="Stopped"
}
# Start Backup
$VMstatus[$VMName]="BackingUp"
$Stat = Find-VBRHvEntity -Server "$HVServer" -Name "$VMName" | Start-VBRZip -Folder "$TodayTarget" -Compression 5 -EncryptionKey (Get-VBREncryptionKey -Description "$VEEAMEncryptionKeyDescription")
# Start VM again
if ($Type -ne "Production") {
$VMstatus[$VMName]="Starting"
Start-VM -Name "$VMName"
$VMstatus[$VMName]="Started"
}
if ($Stat.Result -eq "Success") {
if ($Type -eq "Production" -or $VMstatus[$VMName] -eq "Started") {
$VMstatus[$VMName]="Success"
Write-Host -ForegroundColor Green " ... done :-)"
}
else {
$VMstatus[$VMName]="Failed"
Write-Host -ForegroundColor Red " ... Failed :-("
}
}
else {
$VMstatus[$VMName]="Failed"
Write-Host -ForegroundColor Red " ... Failed :-("
}
}
# Write Result to txt file for later use...
$VMstatus | Out-File (Join-path -Path $TodayTarget -ChildPath "result.txt")
#------------------------------------------------------------------------------------------------
# Create sha512 hashes if the backupd files
Write-Host -ForegroundColor Green "`n`nCreating Checksums...`n"
Get-ChildItem -filter "*.vbk" -path "$TodayTarget" | ForEach-Object {
Write-Host -ForegroundColor Yellow ("`t"+$_.Name)
$SHAName = ($_.FullName+"sha512")
Set-Content -Path "$SHAName" -Value ((Create-FileHash -FileName $_.FullName)+" "+(Split-Path -Path $_.FullName -Leaf)) -Encoding UTF8
}