rzlbrnft
Goto Top

Einfaches Powershell Script zum Backup aller HyperV virtuellen Maschinen auf NAS Freigabe

# ErrorHandling
$Error.clear()

# Temporary Backup Path
$BackupRoot = "C:\Export\"  
# Backup Target on the NAS
$BackupTarget = "\\192.168.108.100\Backup\HyperV\"  
# Days the backup shall be kept
$Limit = (Get-Date).AddDays(-2)

$BackupPath = $BackupRoot + "BCK-" + (get-date).toString('yyyy-MM-dd')  

#Export All VMs on the server
Get-VM | Export-VM –Path $BackupPath

if ($Error.Count -eq 0) {
    # Delete files older than the $limit.
    Get-ChildItem -Path $BackupTarget -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $Limit } | Remove-Item -Force

    # Delete any empty directories left behind after deleting the old files.
    Get-ChildItem -Path $BackupTarget -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
}

# Move the backup folders to the nas
Move-Item $BackupPath $BackupTarget -Force -ErrorAction Stop 

Die NAS die wir benutzen kann keine Computerkonten in die Schreibberechtigung für eine Freigabe aufnehmen.
Da Export-VM immer mit dem Systemkonto des HyperV Hosts ausgeführt wird, kann er also nicht direkt dorthin schreiben.
Wir haben uns damit beholfen, den Export lokal durchzuführen und die Dateien dann zu verschieben.

Content-Key: 250795

Url: https://administrator.de/contentid/250795

Printed on: April 25, 2024 at 20:04 o'clock