Veeam Backup Free Edition 並沒有提供排程備份的功能,但它並沒有禁止我們自行撰寫指令來執行它的備份程式。我從網路上找到一個好心人分享的 Powershell 指令,稍微修改,貼在這裡。
我參考的文章在這裡:
Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
我修改的部分:
修改後的 Powershell 指令如下:
儲存為檔案:VeeamBackupAllVMs.ps1。然後開啟 Windows 的工作排程器,增加一個新工作。比較需要說明的是執行動作的設定,參考下圖:
我參考的文章在這裡:
Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition)
我修改的部分:
- 變數 $AllVMs:在抓取虛擬機器名單時,原始文章是使用 Find-VBRViEntity 指令,那個是給 VMWare 用的。我的是 Hyper-V,所以改成 Find-VBRHvEntity。
- 增加刪除老舊備份檔案的工具函式。原因:Start-VBRZip 指令的 -AutoDelete 參數沒有作用,造成備份檔案的數量不斷增加。
- 把備份目標位置從網路位置改為本機的 D: 槽。
- 輸出一些訊息到 console,方便除錯。
修改後的 Powershell 指令如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Utility functions for deleting old files. | |
Source: http://blog.danskingdom.com/powershell-functions-to-delete-old-files-and-empty-directories/ | |
#> | |
# Function to remove all empty directories under the given path. | |
# If -DeletePathIfEmpty is provided the given Path directory will also be deleted if it is empty. | |
# If -OnlyDeleteDirectoriesCreatedBeforeDate is provided, empty folders will only be deleted if they were created before the given date. | |
# If -OnlyDeleteDirectoriesNotModifiedAfterDate is provided, empty folders will only be deleted if they have not been written to after the given date. | |
function Remove-EmptyDirectories([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [switch] $DeletePathIfEmpty, [DateTime] $OnlyDeleteDirectoriesCreatedBeforeDate = [DateTime]::MaxValue, [DateTime] $OnlyDeleteDirectoriesNotModifiedAfterDate = [DateTime]::MaxValue, [switch] $OutputDeletedPaths, [switch] $WhatIf) | |
{ | |
Get-ChildItem -Path $Path -Recurse -Force -Directory | Where-Object { (Get-ChildItem -Path $_.FullName -Recurse -Force -File) -eq $null } | | |
Where-Object { $_.CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate -and $_.LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate } | | |
ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf } | |
# If we should delete the given path when it is empty, and it is a directory, and it is empty, and it meets the date requirements, then delete it. | |
if ($DeletePathIfEmpty -and (Test-Path -Path $Path -PathType Container) -and (Get-ChildItem -Path $Path -Force) -eq $null -and | |
((Get-Item $Path).CreationTime -lt $OnlyDeleteDirectoriesCreatedBeforeDate) -and ((Get-Item $Path).LastWriteTime -lt $OnlyDeleteDirectoriesNotModifiedAfterDate)) | |
{ if ($OutputDeletedPaths) { Write-Output $Path } Remove-Item -Path $Path -Force -WhatIf:$WhatIf } | |
} | |
# Function to remove all files in the given Path that were created before the given date, as well as any empty directories that may be left behind. | |
function Remove-FilesCreatedBeforeDate([parameter(Mandatory)][ValidateScript({Test-Path $_})][string] $Path, [parameter(Mandatory)][DateTime] $DateTime, [switch] $DeletePathIfEmpty, [switch] $OutputDeletedPaths, [switch] $WhatIf) | |
{ | |
Get-ChildItem -Path $Path -Recurse -Force -File | Where-Object { $_.CreationTime -lt $DateTime } | | |
ForEach-Object { if ($OutputDeletedPaths) { Write-Output $_.FullName } Remove-Item -Path $_.FullName -Force -WhatIf:$WhatIf } | |
Remove-EmptyDirectories -Path $Path -DeletePathIfEmpty:$DeletePathIfEmpty -OnlyDeleteDirectoriesCreatedBeforeDate $DateTime -OutputDeletedPaths:$OutputDeletedPaths -WhatIf:$WhatIf | |
} | |
# *** End of utility functions *** | |
<# | |
.Synopsis | |
Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition) | |
For updated help and examples refer to -Online version. | |
.DESCRIPTION | |
Automate Backups with Start-VBRZip in Powershell (Veeam Backup Free Edition) | |
For updated help and examples refer to -Online version. | |
.NOTES | |
Veeam_BackupAllVMs | |
Author: the Sysadmin Channel | |
Version: 1.0 | |
DateCreated: 2018-Apr-25 | |
DateUpdated: 2018-Apr-25 | |
.LINK | |
https://thesysadminchannel.com/automate-backups-start-vbrzip-powershell-veeam-backup-free-edition - | |
.EXAMPLE | |
For updated help and examples refer to -Online version. | |
#> | |
Add-PSSnapin VeeamPSSnapIn | |
$AllVMs = Find-VBRHvEntity -Name * | Where-Object {($_.Type -eq "VM") -and ($_.PowerState -eq "PoweredOn")} | Sort-Object Name | |
$BackupFolder = "D:\backup\VeeamZIP-AllVMs" | |
$StartTime = Get-Date | |
$Date = (Get-Date).ToString("yyyy") | |
$LogFile = "$BackupFolder\Logs\log.csv" | |
$DeleteAfter = "In3days" <# Valid Options: Never Tonight TomorrowNight In3days In1Week In2Weeks In1Month In3Months In6Months In1Year #> | |
$DeleteBeforeDate = ((Get-Date).AddDays(-3)) | |
Write-Host "Running..." | |
Foreach ($VM in $AllVMs) { | |
$VMName = $VM.Name | |
$VMName = $VMName.ToUpper() | |
Write-Host "Working on" $VMName | |
if (!$(Test-Path $BackupFolder\$VMName)) { | |
mkdir $BackupFolder\$VMName | |
} | |
Start-VBRZip -Folder "$BackupFolder\$VMName" -Entity $VM -Compression 5 -AutoDelete $DeleteAfter | |
$Results = Get-VBRBackupSession | Where-Object {$_.Name -match $VMName} | Sort-Object EndTime -Descending | select -First 1 | |
$FileName = Get-ChildItem -Path $BackupFolder\$VMName | Sort-Object LastWriteTime -Descending | select -ExpandProperty Name -First 1 | |
#Outputting results into logfile. | |
" " | Select @{Name = "VMName"; Expression = {$VMName}}, @{Name = "Status"; Expression = {$Results.Result}}, @{Name = "StartTime"; Expression= {$Results.CreationTime.ToString('MM/dd/yyyy hh:mmtt')}}, @{Name = "EndTime"; Expression= {$Results.EndTime.ToString('MM/dd/yyyy hh:mmtt')}}, @{Name = "TotalTime"; Expression= {$VMTime = ($Results.EndTime - $Results.CreationTime); "Hours: " + $VMTime.ToString('hh') + " " + "Minutes: " + $VMTime.ToString('mm')}}, @{Name = "Filename"; Expression = {$FileName}}, @{Name = "SizeGB"; Expression= {[math]::Round((Get-ChildItem -Path $BackupFolder\$VMName -Filter $FileName | select -ExpandProperty Length) / 1GB,2)}}, @{Name = "AutoDelete"; Expression = {$DeleteAfter}} | Export-Csv $LogFile -NoTypeInformation -Append | |
#If the result failed, delete the backup file since it is not valid. | |
if ($Results.Result -eq "Failed") { | |
Remove-Item $BackupFolder\$VMName\$FileName -Force | |
} | |
# Delete all files created more than 3 days ago. In case Start-VBRZip failed to delete old files. | |
Remove-FilesCreatedBeforeDate -Path "$BackupFolder\$VMName" -DateTime $DeleteBeforeDate | |
} | |
儲存為檔案:VeeamBackupAllVMs.ps1。然後開啟 Windows 的工作排程器,增加一個新工作。比較需要說明的是執行動作的設定,參考下圖: