Auto-commit: 2025-10-31 08:58:35
This commit is contained in:
39
HyperV-BackupVM/exportbackupVM.ps1
Normal file
39
HyperV-BackupVM/exportbackupVM.ps1
Normal file
@@ -0,0 +1,39 @@
|
||||
# Define paths
|
||||
$exportPath = "D:\export"
|
||||
$backupPath = "\\192.168.0.3\backup\VM"
|
||||
|
||||
# Check and create export directory
|
||||
if (-Not (Test-Path -Path $exportPath)) {
|
||||
New-Item -ItemType Directory -Path $exportPath -Force | Out-Null
|
||||
}
|
||||
|
||||
# Clear the export directory
|
||||
Get-ChildItem -Path $exportPath -Recurse | Remove-Item -Force -Recurse
|
||||
|
||||
# Get VMs containing "Backup_" in the name
|
||||
$vmList = Get-VM | Where-Object { $_.Name -like "*Backup_*" }
|
||||
|
||||
if ($vmList.Count -eq 0) {
|
||||
Write-Host "No VMs found with 'Backup_' in the name." -ForegroundColor Yellow
|
||||
return
|
||||
}
|
||||
|
||||
# Export VMs
|
||||
foreach ($vm in $vmList) {
|
||||
$exportDestination = Join-Path -Path $exportPath -ChildPath $vm.Name
|
||||
Export-VM -Name $vm.Name -Path $exportDestination
|
||||
Write-Host "Exported VM: $($vm.Name) to $exportDestination"
|
||||
}
|
||||
|
||||
# Copy exported files to the backup path
|
||||
if (Test-Path -Path $backupPath) {
|
||||
Copy-Item -Path "$exportPath\*" -Destination $backupPath -Recurse -Force
|
||||
Write-Host "Files copied to $backupPath"
|
||||
} else {
|
||||
Write-Host "Backup path not found: $backupPath" -ForegroundColor Red
|
||||
return
|
||||
}
|
||||
|
||||
# Clear the export directory after copying
|
||||
Get-ChildItem -Path $exportPath -Recurse | Remove-Item -Force -Recurse
|
||||
Write-Host "Export directory cleared: $exportPath"
|
||||
35
HyperV-BackupVM/readme.md
Normal file
35
HyperV-BackupVM/readme.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Hyper-V VM Backup Script
|
||||
|
||||
This PowerShell script automates the **export and backup of Hyper-V virtual machines** whose names contain `Backup_`.
|
||||
|
||||
## ✨ Features
|
||||
- Creates and cleans a temporary **export directory**
|
||||
- Exports all VMs matching `*Backup_*`
|
||||
- Copies the exported files to a **network backup path**
|
||||
- Cleans up the temporary export folder after transfer
|
||||
- Provides console messages for progress and errors
|
||||
|
||||
## 📌 Requirements
|
||||
- Windows with Hyper-V role installed
|
||||
- Run PowerShell as **Administrator**
|
||||
- Access to the network share defined in `$backupPath`
|
||||
|
||||
## 🚀 Usage
|
||||
1. Adjust the paths in the script:
|
||||
```powershell
|
||||
$exportPath = "D:\export"
|
||||
$backupPath = "\\192.168.0.3\backup\VM"
|
||||
```
|
||||
|
||||
2. Run the script:
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -File .\Backup-HyperV.ps1
|
||||
```
|
||||
|
||||
3. All VMs with Backup_ in their name will be exported and copied to the backup location.
|
||||
|
||||
⚠️ Notes
|
||||
|
||||
- The export folder ($exportPath) is cleared before and after each run.
|
||||
- Ensure enough free space in the export directory.
|
||||
- Network connectivity and write permissions to $backupPath are required.
|
||||
Reference in New Issue
Block a user