Using REST integration to automate FreeNAS backup replication

Howdy! Wanted to share this but not sure how many people use FreeNAS with replication. I use FreeNAS as my nas OS. I have a secondary FreeNAS box I use as a target to backup replication snapshots from the main system. I don’t power on the secondary box more than every couple of weeks to run the replication backup - thought it would be fun to automate the process.

start_replication: a REST Command which runs the already setup replication task of a certain ID, in my case 13. It starts backup process on the primary FreeNAS system.

rest_command:
  start_replication:
    url: http://192.168.x.x/api/v2.0/replication/id/13/run
    method: POST
    username: !secret fn_user
    password: !secret fn_pass

replication_state: a REST sensor that gets replication information from the FreeNAS api. I marked the value_template as ‘OK’ because the raw GET data from the api ended up kicking back an error, being more than the 255 characters or so that’s allowed. I parse the state of the replication into the sensor

  - platform: rest
    method: GET
    resource: http://192.168.x.x/api/v2.0/replication/id/13
    username: !secret fn_user
    password: !secret fn_pass
    authentication: basic
    name: replication
    json_attributes:
      - state
    value_template: 'OK'
  - platform: template
    sensors:
      replication_state:
        value_template: '{{ states.sensor.replication.attributes["state"]["state"] }}'

script: using the recently updated script syntax, I essentially power on my secondary FreeNAS box, giving it 4 minutes to boot completely, start the replication task, use “while loop” to check every 30 seconds whether the replication_state is “running”. Once it stops running (or has run for more than 12 hours) it will shut down the box.

replication:
  sequence:
    - service: shell_command.start_mini
    - delay:
        seconds: 240
    - service: rest_command.start_replication
    - delay:
        seconds: 45
    - alias: as long as conditions
      repeat:
        while:
          - condition: state
            entity_id: sensor.replication_state
            state: 'RUNNING'
          - condition: template
            value_template: "{{ repeat.index <= 1440 }}"
        sequence:
          - delay:
              seconds: 30
    - delay:
        seconds: 30
    - service: shell_command.stop_mini

shell_command(s) to turn on/off secondary Freenas box (motherboard is ASRock which can be remotely controlled using ipmitool). HA will ssh out of the docker container into the ubuntu host I have running HA the use ipmitool to send the command.

shell_command:
 start_mini: ssh -i /ha/.ssh/docker -o "StrictHostKeyChecking no" [email protected] 'ipmitool -H 192.168.x.x -U user -P pass chassis power on'
 stop_mini: ssh -i /ha/.ssh/docker -o "StrictHostKeyChecking no" [email protected] 'ipmitool -H 192.168.x.x -U user -P pass chassis power soft'
1 Like