feat(homeassistant): add HomeAssistant backup

This commit is contained in:
Adrien Poupa
2024-04-15 00:00:12 -04:00
parent e845aa96f5
commit d5c4df30fc
11 changed files with 188 additions and 87 deletions

4
homeassistant/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
*
!README.md
!docker-compose.yml
!backup.env.example

92
homeassistant/README.md Normal file
View File

@@ -0,0 +1,92 @@
# Home Assistant
Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts
## Installation
Enable Home Assistant by setting `COMPOSE_PROFILES=homeassistant`.
Set the `HOMEASSISTANT_HOSTNAME`, since it does not support
[running in a subfolder](https://github.com/home-assistant/architecture/issues/156).
Add the necessary DNS records in your domain.
You will need to allow Traefik to access Home Assistant by adding the following in `homeassistant/configuration.yaml`:
```yaml
http:
use_x_forwarded_for: true
trusted_proxies:
- 172.0.0.0/8 # You can put a more precise range instead
```
Set the `HOMEASSISTANT_ACCESS_TOKEN` for homepage support.
## Backup
### Enable Backups in HomeAssistant
We will create an automation that will create backups nightly and clear old ones.
Add a `command_line` inclusion in your `configuration.yaml`: `command_line: !include command_lines.yaml`
The `command_lines.yaml` defines a switch that removes backups older than 7 days:
```yaml
- switch:
name: Purge old backups
unique_id: switch.purge_backups
icon: mdi:trash-can
command_on: 'cd /config/backups/ && find . -maxdepth 1 -type f -mtime +7 -print | xargs rm -f'
```
Then, create an automation that will trigger backups nightly and call the purge old backups switch:
```yaml
alias: Backup Home Assistant every night at 3 AM
description: Backup Home Assistant every night at 3 AM
trigger:
- platform: time
at: "03:00:00"
action:
- service: backup.create
data: {}
- service: switch.turn_on
data: {}
target:
entity_id: switch.purge_old_backups
- service: switch.turn_off
data: {}
target:
entity_id: switch.purge_old_backups
mode: single
```
### Save Backups Remotely
Home Assistant can be backed up in the cloud storage product of your choice with [Rclone](https://rclone.org/).
Before a backup can be made, `rclone config` must be run to generate the configuration file:
```shell
docker compose run --rm -it homeassistant-backup rclone config
```
It will generate a `rclone.conf` configuration file in ./homeassistant/rclone/rclone.conf.
Copy the backup environment file to `backup.env` and fill it as needed:
`cp backup.env.exmple backup.env`
| Variable | Description | Default |
|----------------------|---------------------------------------------------------------------|---------------------------|
| `RCLONE_REMOTE_NAME` | Name of the remote you chose during rclone config | |
| `RCLONE_REMOTE_DIR` | Name of the rclone remote dir, eg: S3 bucket name, folder name, etc | |
| `CRON` | How often to run the backup | `@daily` backup every day |
| `TIMEZONE` | Timezone, used for cron times | `America/New_York` |
| `ZIP_PASSWORD` | Password to protect the backup archive with | `123456` |
| `BACKUP_KEEP_DAYS` | How long to keep the backup in the destination | `31` days |
You can test your backup manually with:
```shell
docker compose run --rm -it homeassistant-backup backup
```

View File

@@ -0,0 +1,6 @@
RCLONE_REMOTE_NAME=
RCLONE_REMOTE_DIR=
CRON=@daily
TIMEZONE=America/New_York
ZIP_PASSWORD=123456
BACKUP_KEEP_DAYS=1

View File

@@ -0,0 +1,50 @@
services:
homeassistant:
image: ghcr.io/home-assistant/home-assistant:stable
container_name: homeassistant
network_mode: host
environment:
- PUID=${USER_ID}
- PGID=${GROUP_ID}
- TZ=${TIMEZONE}
volumes:
- ${CONFIG_ROOT:-.}/homeassistant:/config
- /etc/localtime:/etc/localtime:ro
- /run/dbus:/run/dbus:ro
restart: always
healthcheck:
test: [ "CMD", "curl", "--fail", "http://127.0.0.1:8123" ]
interval: 30s
retries: 10
privileged: true
labels:
- traefik.enable=true
- traefik.http.routers.homeassistant.rule=(Host(`${HOMEASSISTANT_HOSTNAME}`))
- traefik.http.routers.homeassistant.tls=true
- traefik.http.routers.homeassistant.tls.certresolver=myresolver
- traefik.http.services.homeassistant.loadbalancer.server.port=8123
- homepage.group=Apps
- homepage.name=Home Assistant
- homepage.icon=home-assistant.png
- homepage.href=https://${HOMEASSISTANT_HOSTNAME}
- homepage.description=Open source home automation that puts local control and privacy first
- homepage.weight=3
- homepage.widget.type=homeassistant
- homepage.widget.url=https://${HOMEASSISTANT_HOSTNAME}
- homepage.widget.key=${HOMEASSISTANT_ACCESS_TOKEN}
profiles:
- homeassistant
homeassistant-backup:
image: adrienpoupa/rclone-backup:latest
container_name: homeassistant-backup
restart: always
env_file:
- ${CONFIG_ROOT:-.}/homeassistant/backup.env
environment:
- BACKUP_FOLDER_NAME=backups
- BACKUP_FOLDER_PATH=/backups
volumes:
- ${CONFIG_ROOT:-.}/homeassistant/backups:/backups
- ${CONFIG_ROOT:-.}/homeassistant/backup:/config
profiles:
- homeassistant