0

I was wondering if there is a convenient way to automate HomeAssistent (Hass.io) backups / snapsnots?

The web frontend only allows to manually create snapshots. That's fine for the config, as it will not change a lot once you are finished.

But what about the database?

Joe
  • 6,758
  • 2
  • 26
  • 47

2 Answers2

2

I think is most simple and convinient way is to use HA automations:

automation:
  - alias: '[System] Weekly Backup Monday at 5:00'
    initial_state: on
    trigger:
      platform: time
      at: '05:00'
    condition:
      - condition: time
        weekday:
          - mon
    action:
      - service: hassio.snapshot_full
        data_template:
          name: "Automated Snapshot {{ now().strftime('%F') }}"
      - service: notify.hass_info
        data_template:
          message: "Automated Snapshot {{ now().strftime('%F') }}"

As next step, you can store backups to Google Drive via special addon:

rest_command:
  google_backup:
    url: 'http://localhost:8055/gb/doBackup'
    timeout: '300'

automation:
  - alias: '[System] Weekly Backup Monday to Google at 5:30'
    initial_state: on
    trigger:
      - platform: time
        at: '05:30'
    condition:
      - condition: time
        weekday:
          - mon
    action:
      - service: rest_command.google_backup
      - service: notify.hass_info
        data:
          message: "Automatic snapshot uploaded"

Limych
  • 121
  • 4
0

I use a Python script running on the server and mimic the POST request that the web frontend uses to trigger the snapshot creation.

First, get a long-lived access token.

Usually they are used for add-ons, but they come in handy here. You can get one in your user profile in the web frontend, scroll down and click "Create token".

Then use the following script:

import datetime
import requests

TOKEN = 'your-long-lived-access-token'

date_string = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

url = 'http://hassio.local:8123/api/hassio/snapshots/new/full'

headers = {'authorization': ('Bearer ' + TOKEN)}

response = requests.post(url,
                         headers=headers,   
                         json={"name": date_string},
                         timeout=600) # should be enough, check duration

# check the status code to make sure the backup worked
print(response.status_code)
print(response.text)
print(response.json())

Now you just have to find out where the snapsnot was created in your installation (e.g. /usr/share/hassio/backup and copy it into the clouds or an external drive.

Joe
  • 6,758
  • 2
  • 26
  • 47