有没有方便的方法来自动化 Home Assistant (Hass.io) 备份/快照?

Is there a convenient way to automate HomeAssistent (Hass.io) backups / snapsnots?

我想知道是否有一种方便的方法来自动化 HomeAssistent (Hass.io) 备份/snapsnots?

网页前端只允许手动创建快照。这对配置来说很好,因为一旦你完成它就不会改变太多。

但是数据库呢?

我在服务器上使用 Python 脚本 运行 并模拟 Web 前端用来触发快照创建的 POST 请求。

首先,得到一个long-lived access token

通常它们用于附加组件,但它们在这里派上用场。您可以在 Web 前端的用户个人资料中找到一个,向下滚动并单击 "Create token"。

然后使用以下脚本:

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())

现在您只需要找到在安装中创建 snapsnot 的位置(例如 /usr/share/hassio/backup 并将其复制到云端或外部驱动器中。

我认为最简单方便的方法是使用 HA 自动化:

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') }}"

下一步,您可以通过 special addon 将备份存储到 Google 驱动器:

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"