Skip to content

daily zfs snapshots from Home Assistant

Troy Prelog edited this page Dec 12, 2020 · 2 revisions

Taking ZFS Snapshots from Home Assistant

This can be easily accomplished by using the TrueNAS V2 REST API and the rest_command integration in Home Assistant. I'm not trying to schedule all my dataset snapshots from Home Assistant, but I find it's helpful for a few. For example, I like to take snapshots of my Home Assistant configuration before I make changes. I find it convenient to trigger that snapshot from Home Assistant.

I think it's pretty cool, when I sit down at my computer to make changes in Home Assistant, I say "Hey Google, activate snapshot configuration" and just like that, TrueNAS take a new snapshot of my configuration dataset!

If you're interested, here are some ideas to set that up.

Store configuration in your secrets file

  • add to your homeassistant/secrets.yaml
## Configuration for the TrueNAS v2.0 RESTful api
tn_server: http://truenas.local
tn_bearer_token: Bearer PASTE_YOUR_TRUENAS_API_KEY_HERE

Expose the rest_command as a service using this package.yaml file

  • create file homeassistant/packages/truenas/rest_commands/zfs_snapshot.yaml
  • copy/paste the following (do not edit) to the file you created
## This package.yaml uses the TrueNAS v2.0 RESTful api to create
## a rest_command service for takeing snapshots of a zfs dataset.
rest_command:
  zfs_snapshot:
    url: "{{ truenas_server }}/api/v2.0/zfs/snapshot"
    method: POST
    headers:
      Authorization: !secret tn_bearer_token
      Content-Type: application/json
      User-Agent: Home Assistant
    payload: '{"dataset":"{{ dataset_name }}","name":"{{ snapshot_name }}"}'

The blueprint to create automations

  • create file homeassistant/blueprints/automation/truenas_api/zfs_snapshot.yaml
  • copy/paste the following (do not edit) to the file you created
blueprint:
  name: Daily ZFS Snapshot
  description: Creates snapshot of a zfs dataset at a given time every day.
  domain: automation
  input: 
    dataset_name:
      name: ZFS dataset to snapshot
      description: Take snapshots of this zpool/dataset 
    trigger_time:
      name: Snapshot creation time
      description: The snapshot will be created at this time, every day
      selector:
        time:
    send_notification:
      name: Send notification
      description: Sends a notification to a device if enabled
      selector:
        boolean:
      default: false
    notify_device:
      name: Device to notify
      description: Device needs to run the official Home Assistant app to receive notifications
      selector:
        device:
          integration: mobile_app
      default: ""
mode: single
max_exceeded: silent
variables:
  truenas_server: !secret tn_server
  snapshot_name: "daily_snapshot_{{ now().strftime('%Y.%m.%d_%H.%M') }}"
  dataset_name: !input dataset_name
  send_notification: !input send_notification
  notify_device: !input notify_device
  notification_title: Daily ZFS Snapshot
  notification_message: "Snapshot Created: {{ snapshot_name }}"
trigger:
  - platform: time
    at: !input trigger_time
action:
  - service: rest_command.zfs_snapshot
    data_template:
      truenas_server: "{{ truenas_server }}"
      dataset_name: "{{dataset_name }}"
      snapshot_name: "{{ snapshot_name }}"
  - choose:
      - conditions: "{{ send_notification }}"
        sequence:
          - device_id: !input notify_device 
            domain: mobile_app
            type: notify
            title: "{{ notification_title }}"
            message: "{{ notification_message }}"

Blueprint Preview

Use the Blueprint to configure daily zfs snapshots