-
Notifications
You must be signed in to change notification settings - Fork 31
/
make_backup_virtual_machine.ps1
85 lines (58 loc) · 2.97 KB
/
make_backup_virtual_machine.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<#
=================================================================================================
Hot Backup Hyper-V VM's
Name: make_backup_virtual_machine.ps1
Autor: Wanderlei Hüttel
Email: [email protected]
Versão 1.0 - 07/02/2019
Using cmdlets Windows Server Backup
https://docs.microsoft.com/en-us/powershell/module/windowsserverbackup/?view=win10-ps
=================================================================================================
#>
#======================== Not show warnings ========================#
$WarningPreference = "SilentlyContinue"
#======================== Function to return difference between dates ========================#
Function DateDiff-DateTime() {
Param ($DateTimeStart, $DateTimeEnd)
$ts = New-TimeSpan –Start $DateTimeStart –End $DateTimeEnd
$TotalHora = "{0:hh}:{0:mm}:{0:ss}" -f $ts #.Negate() #Caso fique negativo
return $TotalHora
}
#======================== Check argument 1 (VM_NAME) is passed ========================#
if ($args.count -eq 0){
Write-Host "Parameter 1 is required! (VM_NAME)"
Exit 1
}
else{
$vm_name = $args[0]
}
#======================== Configuration ========================#
# Backup Target is a localhost shared folder because Windows Backup
# not accept copy to a single folder
$BackupTarget = "\\localhost\D$\backup_hyper-v\$vm_name"
#======================== Start script ========================#
$StartTime = Get-Date
Write-Host "========== Hyper-V - Windows Backup Virtual Machines =========="
Write-Host "Backup VM ($vm_name) started: " $StartTime.toString("dd/MM/yyyy HH:mm:ss")
#============ Check if the folder with VM_NAME exists. If not exist, it created ============#
if(!(Test-Path $BackupTarget)){
New-Item -ItemType Directory -Path $BackupTarget | Out-Null
}
#======================== Set policy ========================#
$policy = New-WBPolicy
#======================== Set VSS option ========================#
Set-WBVssBackupOption -Policy $policy -VssFullBackup
#======================== Set Backup Location ========================#
$BackupLocation = New-WBBackupTarget -NetworkPath $BackupTarget
Add-WBBackupTarget -Policy $policy -Target $BackupLocation -Force | Out-Null
#======================== Set VM to Backup ========================#
$VirtualMachine = Get-WBVirtualMachine | where {$_.VMName -like $vm_name}
Add-WBVirtualMachine -Policy $policy -VirtualMachine $VirtualMachine | Out-Null
#======================== Start VM Backup ========================#
Start-WBBackup -Policy $policy -AllowDeleteOldBackups | Out-Null
#======================== End Backup ========================#
$EndTime = Get-Date
Write-Host "Backup VM ($vm_name) finished: " $EndTime.toString("dd/MM/yyyy HH:mm:ss")
Write-Host "Elapsed time: " (DateDiff-DateTime $StartTime $EndTime)
Write-Host "========== Hyper-V - Windows Backup Virtual Machines =========="
Exit 0