zngguvnf's Blog

Posts tagged "homeassistant":

HomeAssistant: How I backup and restore my data!

<2019-12-17>

Update [2021-01-24 Sun]

Assuming you're using hass.io on a raspberry pi:

Backup

There are multiple option of backing up your home assistant data:

1. Build in option

  • Login to WebUI -> in the sidebar go to Hass.io -> snapshots
  • Here your can create a new snapshot
  • after clicking the reload button (upper right corner) you can download your snapshot

2. An automation to create snapshots on a regular base

  • Put the following in your automation.yaml
- alias: Create hourly backup
  trigger:
    - platform: time_pattern
      minutes: '/60'
  action:
    - service: notify.mobile_app_iphone_your_name
      data:
        message: Hourly Backup 💾 is created!
        title: Backup
    - service: hassio.snapshot_full
      data_template:
        name: >
          Backup_{{ now().strftime('%d-%m-%YT%H-%M-%S') }}

3. Automatic offside backups

  • Create new backup user on your server
  • Add the user to the allowed group of ssh users in /etc/ssh/sshd_config
  • sudo systemctl restart sshd
  • Create new ssh key for hass.io
  • Add new ssh pub key to ~/authorized_keys
  • Add this repo to your plugins https://github.com/carstenschroeder/hassio-addons

Addon config:

{
  "ssh_enabled": true,
  "ssh_host": "192.168.178.27",
  "ssh_port": 22,
  "ssh_user": "ms-backup",
  "ssh_key": [
    "-----BEGIN OPENSSH PRIVATE KEY-----",
    "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
    cat ~/.ssh/hassio-key
    "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
    "-----END OPENSSH PRIVATE KEY-----"
  ],
  "remote_directory": "/hpdata/homes/ms-backup/backup_hassio",
  "zip_password": "",
  "keep_local_backup": "48",
  "rsync_enabled": false,
  "rsync_host": "",
  "rsync_rootfolder": "hassio-sync",
  "rsync_user": "",
  "rsync_password": ""
}
  • Add your new private key to the configuration above
  • create a new automation in automations.yaml
- alias: Offside backup
  trigger:
    platform: time_pattern
    minutes: '/60'
  action:
  - service: notify.mobile_app_iphone_your_name
    data:
      message: Offside backup 💾 is created!
      title: Backup
  - service: hassio.addon_start
    data:
      addon: 36883ed7_remote_backup
      #      ^- to get the propper ID go to hass.io -> Addons -> Remote Backup 
      #         I couldn't figure out where this ID comes from
  • This will trigger the remote backup addon. After the backup is done the state of remote backup goes back to stopped till it is triggered the next time. While testing this did not worked for very short intervals < 3 Minutes. For longer intervals it works like expected.

Restore

No matter how you created the backup to restore it, proceed as follows

  • Install latest hass.io to sd card.
  • Boot raspberry pi
  • It will take up to 20 minutes to initialize hass.io
  • Create a tmp account
  • power off
  • copy backup to sd card (/hassos-data/supervisor/backup)
  • Restart raspberry pi
  • In the sidebar go to Hass.io -> Snapshots, choose your snapshot -> wipe and restore, You're done!

I had some problems, with a corrupt database. (HomeAssistant could not add any more new data. I think there was something like (sqlite3.DatabaseError) database disk image is malformed in the logs).

To fix/repair your database:

Copy your (corrupt) database (home-assistant_v2.db) to your local linux system

sqlite3 home-assistant_v2.db ".dump" | sqlite3 home-assistant_v2.db_fixed

Now delete your old (corrupt database) and replace it with home-assistant_v2.db_fixed. To do that delete the corrupt one and rename home-assistant_v2.db_fixed to home-assistant_v2.db. Now, power on your raspberry pi and everything should be fine again.

Other posts