When managing production Hyper-V environments, the traditional shutdown-and-copy approach causes unacceptable downtime. Microsoft's Volume Shadow Copy Service (VSS) provides the solution through disk shadow copies - but implementing it via command line requires precise syntax.
1. Diskshadow.exe (built into Windows Server) 2. VSS-aware Hyper-V integration services 3. Target storage (local disk or UNC path) 4. Administrator privileges
Here's the minimal diskshadow script to snapshot running VMs:
set context persistent set verbose on begin backup add volume C: alias SystemVolume add volume E: alias VMStorage create expose %SystemVolume% Z: expose %VMStorage% Y: exec "C:\scripts\robocopy_backup.bat" end backup
This complete solution handles multiple VMs with verification:
:: diskshadow_script.txt set metadata C:\backups\vss_meta.cab set context persistent begin backup add volume C: alias SystemDrive add volume F: alias VM_Cluster create expose %SystemDrive% S: expose %VM_Cluster% V: :: Robocopy the exposed shadow copies exec "cmd /c robocopy V:\VM01 D:\backups\VM01 /MIR /R:1 /W:1 /NP /TEE /LOG+:C:\logs\vm01_backup.log" exec "cmd /c robocopy V:\VM02 D:\backups\VM02 /MIR /R:1 /W:1 /NP /TEE /LOG+:C:\logs\vm02_backup.log" :: Verify VSS integrity exec "cmd /c vssadmin list shadows /for=F: > C:\logs\vss_verify.log" end backup
Persistent context: Maintains the shadow copy beyond command execution
Alias naming: Required for subsequent volume exposure
Robocopy switches: /MIR mirrors directory trees, /R/W control retries
:: Add to your batch file: if %ERRORLEVEL% NEQ 0 ( echo Backup failed at %TIME% >> C:\logs\backup_errors.log diskshadow /s C:\scripts\cleanup.txt exit /b 1 )
When targeting UNC paths:
net use Z: \\backup_server\share /USER:backup_agent P@ssw0rd /PERSISTENT:YES robocopy V:\VM01 Z:\hyperv_backups\VM01 /ZB /MIR /COPYALL
Create a scheduled task with these actions:
1. Program: diskshadow.exe 2. Arguments: /s C:\scripts\diskshadow_script.txt 3. Run as: SYSTEM account 4. Triggers: Daily at 2AM 5. Conditions: Start only if network available
Backing up running Hyper-V VMs without downtime is crucial for production environments. The built-in Windows Volume Shadow Copy Service (VSS) combined with diskshadow.exe provides a robust CLI approach that's scriptable and reliable.
1. VSS Writers: Hyper-V VSS writer coordinates VM snapshots
2. Diskshadow.exe: Microsoft's VSS requester tool with scripting capabilities
3. Storage: Local disk or SMB share for backup destination
@echo off set BACKUP_DIR=\\backupserver\vmbackups set VM_NAME=ProdServer2019 diskshadow.exe /s backup_script.txt robocopy C:\ClusterStorage\Volume1\ %BACKUP_DIR%\%VM_NAME%_%DATE% /MIR /ZB /R:1 /W:1 /LOG:%BACKUP_DIR%\%VM_NAME%.log
Corresponding diskshadow script (backup_script.txt):
set context persistent set verbose on begin backup add volume C: alias SystemVolume add volume D: alias VMStorage create expose %SystemVolume% S: expose %VMStorage% V: end backup
- Run as Administrator with elevated privileges
- Ensure Hyper-V Integration Services are updated in VMs
- Test restore procedures regularly
- Monitor VSS writer health with
vssadmin list writers
For multiple VMs with error handling:
powershell.exe -Command { $vms = Get-VM | Where-Object {$_.State -eq 'Running'} foreach ($vm in $vms) { try { $backupName = "$($vm.Name)_$(Get-Date -Format 'yyyyMMddHHmm')" diskshadow.exe /s "C:\scripts\$backupName.dsh" Export-VM -Name $vm.Name -Path "\\backupnas\vmexports\$backupName" } catch { Write-EventLog -LogName Application -Source "HyperVBackup" -EntryType Error -EventId 500 -Message $_.Exception.Message } } }
Factor | Recommendation |
---|---|
Backup Window | Schedule during low-usage periods |
Network | Use dedicated NIC for backup traffic |
Storage | Separate physical disks for VMs and backups |
Compression | Enable NTFS compression on backup target |
VSS_E_WRITERERROR: Check application logs in VMs and restart VSS service
Insufficient storage: Implement pre-backup size checks with PowerShell's Get-Volume
Access denied: Configure proper share permissions and firewall rules for SMB