Backup
The backup integration makes the backups of Home Assistant. Its settings make an automatic backup on a schedule, and delete the oldest automatic backups beyond the number or the age you keep.
Only the automatic ones, though. A backup you made by hand, or one Home Assistant made before an update, stays until you delete it on the backup page. One at a time. Those pile up, and eat your disk or your cloud storage. And an automation can make a backup, but not with your choice of what goes in it, and it cannot list or delete one at all.
Spook adds four actions for that: list the backups, delete them, clean them up by a rule, and make a manual backup with what you want in it.
Actions
Spook adds four actions to the backup integration. You find them wherever you add an action, marked with a 👻. All four are for administrators only: a backup holds everything of your home, and is what brings it back.
List backups
Lists every backup, the newest first, as the backup page shows them: its name and date, where it is stored, whether it is encrypted, whether it was made automatically, and what it holds.
How to use it
- Open the automation or script that needs the list, and select Add action.
- Search for backups, and select Backup: List backups 👻.
- Under Response variable, give the list a name, like
backups. The steps after this one can use it. - Select Save.
Just want to see your backups? Try this action opens it in your Home Assistant, ready to run once. The list shows up below the action.
| Action properties | |
|---|---|
| Action | Backup: List backups 👻 |
| Action name | backup.list |
| Action targets | No targets |
| Action response | Yes, every backup and what it holds |
| Spook's influence | Newly added action |
| Tools | Try this action |
This action takes no data.
The response:
| Attribute | Type | Description |
|---|---|---|
backups | list | The backups, the newest first. |
backups[].backup_id | string | The ID of the backup. The delete action takes it. |
backups[].name | string | The name of the backup. |
backups[].date | string | When it was made, as Home Assistant has it. |
backups[].age_days | float | How many days ago it was made. Empty when its date cannot be read. |
backups[].size_mb | float | Its size, in megabytes. |
backups[].automatic | boolean | true when made with the automatic backup settings, false when made otherwise. Empty for a backup of another installation, or from before Home Assistant kept track of it. |
backups[].locations | list | Where it is stored: the id and name of each location, and whether the copy there is encrypted. |
backups[].failed_locations | list of strings | The locations it should have been stored in, but was not. |
backups[].homeassistant_version | string | The version of Home Assistant that made it. |
backups[].contents | object | What it holds: homeassistant and database as true or false, and the names of the apps and folders in it. |
backups[].failed_apps | list of strings | The apps that should have been in it, but are not. |
backups[].failed_folders | list of strings | The folders that should have been in it, but are not. |
locations_not_reached | list | The locations that could not be asked for their backups, with why. A backup stored only there is missing from the list. |
Get a notification when there is no backup from the last two days. The newest backup comes first, and one with a date that cannot be read comes last, so when the first has no date that can be read, none has:
- action: backup.list
response_variable: result
- if:
- condition: template
value_template: >-
{% set newest = result.backups | first | default(none) %}
{% set made = as_datetime(newest.date, none) if newest else none %}
{{ made is none or now() - made > timedelta(days=2) }}
then:
- action: notify.persistent_notification
data:
title: No recent backup
message: >-
There is no backup, none with a date that can be read, or the
newest is more than two days old.
Delete backups
Deletes one or more backups, from every location they are stored in. The same as the delete on the backup page, for as many backups as you like.
A backup that does not exist is skipped, not an error. Ask for the response to see which backups were deleted, and which were not found.
When a location cannot delete a backup, for example because the cloud is out of reach, the action still deletes everything else. Then it fails with an error that names each backup it could not delete, and where.
Before each backup it deletes, the action checks that no backup is being made or restored: a restore reads the backup it restores. When one has started, the action stops, and the error says which backups it already deleted, and which a location could not delete.
How to use it
- Open the automation or script that should delete backups, and select Add action.
- Search for backups, and select Backup: Delete backups 👻.
- Under Backup ID, enter the IDs of the backups to delete. Backup: List backups 👻 gives them.
- Select Save.
| Action properties | |
|---|---|
| Action | Backup: Delete backups 👻 |
| Action name | backup.delete |
| Action targets | No targets |
| Action response | Optional, the backups deleted and the ones not found |
| Spook's influence | Newly added action |
| Tools | Try this action |
| Action data parameters | |||
|---|---|---|---|
| Attribute | Type | Required | Default / Example |
backup_id | string | list of strings | Yes | a1b2c3d4 |
The response:
| Attribute | Type | Description |
|---|---|---|
deleted | list of strings | The backups that were there and are deleted, in the order you gave. |
not_found | list of strings | The backups no location had, in the order you gave. Only when every location could be listed. |
not_listed | list of strings | The backups missing from the list while a location could not be listed, in the order you gave. They could be in that location. |
Delete two backups, and check which ones were there:
- action: backup.delete
data:
backup_id:
- a1b2c3d4
- e5f6a7b8
response_variable: result
- action: notify.persistent_notification
data:
message: "Deleted: {{ result.deleted | join(', ') or 'nothing' }}"
- A restore that starts while a backup is being deleted is not stopped: Home Assistant offers no way to hold one off. The action checks before each backup it deletes, not during one.
Purge backups
Deletes backups by a rule. Keep a number of the newest, delete the ones older than a while, or both. Unlike the backup settings of Home Assistant, it deletes the backups you made by hand too.
- Keep keeps that many of the newest backups, however old they are.
- Older than deletes only the backups older than that, but never the newest one.
- With both, a backup is deleted only when it is not one of the newest to keep and older than that. Keep 3 and older than 30 days always leaves the newest three, and keeps every backup younger than 30 days on top.
One of the two is needed. Without either, the action refuses.
Purge always keeps the newest backup it looks at. Without Keep, it keeps that one as if you said keep 1. A backup schedule that broke weeks ago makes every backup old, and an age rule alone would then delete the last backup you have. This one stays.
Automatic backups chooses which backups the rule looks at. Leave it out for all of them. Turn it on for only the backups the automatic backup settings made. Turn it off for only the ones made otherwise, like by hand or before an update. A backup made by another installation, or before Home Assistant kept track, is neither: Home Assistant does not know how it was made. Those are only purged with Automatic backups left out. Keep counts among the backups the rule looks at: keep 2 with automatic backups off keeps the two newest backups the automatic settings did not make, by hand or before an update, whatever the automatic ones are.
A backup with a date that cannot be read is never deleted by age: nobody knows how old it is. For keep, it counts as the oldest.
Try a rule with Dry run first. It decides what to delete, deletes nothing, and the response says what it would have deleted.
The action does not purge when it cannot see every backup:
- When a location could not be asked for its backups, like the cloud being out of reach. A backup stored only there would be missing when counting the newest, and the newest you have could be deleted. The error names the locations.
- While a backup is being made or restored, also when one starts while the action lists, or between two deletes. Try again once it is done. When it stops halfway, the error says which backups it already deleted, and which a location could not delete.
How to use it
- Open the automation or script that should clean up, and select Add action.
- Search for purge, and select Backup: Purge backups 👻.
- Fill in Keep, Older than, or both.
- Select Save.
| Action properties | |
|---|---|
| Action | Backup: Purge backups 👻 |
| Action name | backup.purge |
| Action targets | No targets |
| Action response | Optional, the backups deleted and the ones kept |
| Spook's influence | Newly added action |
| Tools | Try this action |
| Action data parameters | |||
|---|---|---|---|
| Attribute | Type | Required | Default / Example |
keep | integer | No | 5 |
older_than | string | No | 720:00:00 |
automatic | boolean | No | Left out: all |
dry_run | boolean | No | false |
Give keep, older_than, or both: one of them is needed. keep is at least 1. older_than is a duration, in hours, minutes and seconds like 720:00:00, or in days like days: 30 in YAML.
The response:
| Attribute | Type | Description |
|---|---|---|
deleted | list | The backups deleted, or with a dry run the ones that would be. The newest first. |
deleted[].backup_id | string | The ID of the backup. |
deleted[].name | string | The name of the backup. |
deleted[].date | string | When it was made, as Home Assistant has it. |
kept | list | The backups the rule looked at and kept, the same way. The newest first. |
The backups the rule does not look at, by Automatic backups, are in neither list.
Every night, delete the backups older than two weeks that the automatic settings did not make, like the ones made by hand or before an update, but always keep the newest three of those:
triggers:
- trigger: time
at: "03:00:00"
actions:
- action: backup.purge
data:
automatic: false
keep: 3
older_than:
days: 14
See what a rule would delete, without deleting anything:
- action: backup.purge
data:
keep: 5
dry_run: true
response_variable: purge
- action: notify.persistent_notification
data:
message: >-
Would delete: {{ purge.deleted | map(attribute='name') | join(', ')
or 'nothing' }}
- A location that cannot delete a backup does not stop the rest. The action deletes what it can, and then fails with an error naming what is left, and where.
- A restore that starts while a backup is being deleted is not stopped: Home Assistant offers no way to hold one off. The action checks before each backup it deletes, not during one.
Create manual backup
Makes a backup with what you choose in it and where it goes. The backup page calls that a manual backup: the backup settings never delete it, so it stays until you delete it, or purge it.
The action waits until the backup is done, as the backup action of Home Assistant itself does. Ask for the response to get the ID and name of the new backup.
Without Locations, the backup goes where your automatic backups go. Without automatic backups set up, it goes to this machine, as backup.create of Home Assistant does. On Home Assistant Operating System, that is the Supervisor's location on this machine. A location that is not available right now is skipped, as long as one of them is.
The backup is encrypted with the encryption key of your automatic backup settings, the same key the backup page uses for a manual backup. Home Assistant makes that key when you set up automatic backups. Before that, there is no key, and with Encrypted left out the backup is made without encryption. Without automatic backups it goes to this machine only, so nothing unencrypted leaves it unless you give Locations. Turn Encrypted on to refuse a backup without encryption, or off for a backup that is never encrypted. A location you set to keep its backups unencrypted, in the backup settings, still gets its copy unencrypted.
Apps and folders only go in a backup on a Home Assistant with a Supervisor, like Home Assistant Operating System. Elsewhere, the action refuses them.
Only one backup at a time: while one is being made or restored, the action refuses.
How to use it
- Open the automation or script that should make the backup, and select Add action.
- Search for backup, and select Backup: Create manual backup 👻.
- Choose what goes in it, and where it goes.
- Select Save.
| Action properties | |
|---|---|
| Action | Backup: Create manual backup 👻 |
| Action name | backup.create_manual |
| Action targets | No targets |
| Action response | Optional, the ID and name of the new backup |
| Spook's influence | Newly added action |
| Tools | Try this action |
| Action data parameters | |||
|---|---|---|---|
| Attribute | Type | Required | Default / Example |
name | string | No | Before the big update |
locations | string | list of strings | No | The locations of the automatic backups |
include_database | boolean | No | true |
include_all_apps | boolean | No | false |
include_apps | string | list of strings | No | core_mosquitto |
include_folders | string | list of strings | No | share |
encrypted | boolean | No | Encrypted when there is a key |
A location is its ID, like backup.local for this machine, hassio.local on Home Assistant Operating System, or cloud.cloud for Home Assistant Cloud. A location that does not exist is refused, with the ones there are.
The folders are share, addons/local (your local apps), ssl and media. An app is its slug, like core_mosquitto. Give include_all_apps or include_apps, not both.
Without name, the backup gets the name Home Assistant gives a manual backup.
The response:
A backup with the database, before you start a big update, stored on this machine and in the cloud:
- action: backup.create_manual
data:
name: Before the big update
locations:
- backup.local
- cloud.cloud
response_variable: made
- action: notify.persistent_notification
data:
message: "Backup {{ made.name }} is done."
- When the backup is made but cannot be stored in one of its locations, the action fails with an error that names them. The backup is in the other locations.
- When an app or folder cannot be backed up, the Supervisor makes the backup without it. The action then fails with an error that names what is missing, so an automation does not go on with an incomplete backup.
- An app slug that does not exist is not checked by Spook. The Supervisor refuses it, and the action fails with what it said.
Missing something here?
An action, an entity or a repair this integration should have? Say so. Stuck, or found a bug? There is help.
