This is a guide on how to trigger Hyper-V checkpoints within Home Assistant and somewhat manage them with an automation. (it deletes old ones depending on how much free space we want. Checkpoints are a strong feature of hyper-v, it’s possible to do it on vbox as well courtesy of hass.agent but the machine freezes when creating snapshots and needs to be off when restoring.
Quick reminder: they are not proper backups, use hassio-onedrive-backup or -home-assistant-google-drive-backup addons as well. Checkpoints however do save time and make possible to recover short term data. For example, at power failure, i can just restore a less than a hour old snapshot in seconds if database gets corrupted.
Prerequisites:
- Install Home Assistant OS on Hyper-V.
- Install and configure hass.agent on both the Home Assistant and Windows PC.
- Enable and configure the Samba Share add-on for accessing files from Windows (if you want ha core version mentioned in the checkpoint name)
Create a PowerShell script named “checkpoint.ps1” and replace ‘vm-name’ and ‘ha-ip’(optional) with your specific information.
$vmName = "<vm-name>"
$versionFilePath = "\\<ha-ip>\config\.HA_VERSION"
$snapshotName = ""
if (Test-Path $versionFilePath) {
$versionNumber = (Get-Content -Path $versionFilePath).Trim()
$snapshotName = "hassagent auto - {0:dd.MM.yyyy - HH:mm:ss} - ha core v.{1}" -f (Get-Date), $versionNumber
} else {
$snapshotName = "hassagent auto - {0:dd.MM.yyyy - HH:mm:ss}" -f (Get-Date)
}
Get-VM -Name $vmName | Checkpoint-VM -SnapshotName $snapshotName
Create a task in task scheduler:
Under the “General” tab, check “Run whether the user is logged on or not” and “Run with highest privileges.”
Under the “Actions” tab, configure the following:
Program/script: Powershell.exe
Add arguments:
-ExecutionPolicy Bypass -File <path to ps1 file>
(e.g., -ExecutionPolicy Bypass -File A:\checkpoint.ps1
)
Save and enter credentials.
Run the task. It should work.
Create a hass.agent command (button for Home Assistant) in Windows:
open hass,agent->commands-> add new → type: custom ->button, command:
schtasks /run /tn "<name_of_task_you_created>"
test button from ha.
now, to manage space, we need a new powershell script triggered by task sheduler with its correspunding hass agent command.
remove_oldest_hyperv_checkpoint.ps1:
$VMName = "HomeAssistant"
$Snapshots = Get-VM -Name $VMName | Get-VMSnapshot
$Snapshots = $Snapshots | Sort-Object CreationTime
$OldestSnapshot = $Snapshots[0]
$OldestSnapshot | Remove-VMSnapshot -Confirm:$false
Write-Host "Oldest snapshot '$($OldestSnapshot.Name)' has been deleted."
Get-VM -Name $VMName | Get-VMSnapshot
create a hass agent storage sensor. sensors->add new->storage.
We need the attribute UsedSpacePercentage for the drive where the avhdx files appear.
On ha, go to dev tools->status and find the attribute there.
New ha automation:
trigger checkpoint once an hour: time pattern-> hours: /1
actions:
if-then->if template condition
{{ state_attr('sensor.storage_x', 'UsedSpacePercentage')| float > 90 }}
then:
repeat-> under repeat select “while” template condition:
{{ state_attr('sensor.storage_x', 'UsedSpacePercentage')| float > 85 }}
actions while template true:
action:
service->button press->remove_oldest_hyperv_checkpoint
action:
wait for time to pass 30s
action after repeat over:
service->button press->checkpoint
*end of if
else
service->button press->checkpoint
done.
Update: added load_load_lastest_checkpoint.ps1, usefull for automatic recovery after a power failure.