Auto Back up Reactor Directory to NAS for Home Assistant and RPi Bare Metal Install
-
I am paranoid that my trusty old Rpi 3b+ running on an old micro SD card is gonna eventually give out, and the countless hours I have put into Reactor will be put to waste. I created a simple script that backups the Reactor directory, dates it and auto deletes any previous backups 7 days old. I have a rule in Reactor that calls a SSH command to my Rpi to run the backup script once every day. Here's the nitty gritty:
The Backup Script
SSH into your Rpi and use the command
sudo nano reactorbackup.sh
. In the file editor that opens up enter these lines (remember to change the paths):#! /bin/bash sudo cp -r {path_to_reactor_directory} {path_to_backup_location}-$(date +'%Y_%m_%d_%H:%M') sudo {path_to_backup_location} -maxdepth 1 -mtime +7 -exec rm -rf {} +
Some notes about the script: you can update the date format if you want something else, also change the
+7
in-mtime +7
changes the amount of days-old backups to be deleted. Currently it's 7 days.Home Assistant Config
There are many ways you can send SSH commands from HA to other devices, the simplest way I have found is using this HACS integration. Following the install instructions on the GitHub page you will now have a new service called
SSH Command: Execute command
. This is the service we will use in our Reactor ruleset.Reactor Backup Rule
Now it's time to create a rule that auto runs the previously created
reactorbackup.sh
script. Very simple stuff:Triggers:
Date/Time between 1:00am and 1:15am (can edit this to whatever time you'd like).
Set Reaction:
Entity Action - hass>system - call service
service ID: SSH Command: Execute command
Data:
host: {Rpi IP Adress}
port: 22
user: {user}
pass: {password}
command: sh reactorbackup.shI do recommend you test this and do check on your backup files, I have yet to think of a way to check if the backup was successful without needing to go into the backup location but its better than nothing.
Bonus: Mounting NAS to your RPi
So a backup is only useful if you can access it in the event of failure. If you backup your Reactor directory somewhere in your RPi, it's basically useless if something happens to your RPi. I mapped my NAS so that I can backup the Reactor directory into my NAS. Here's how:
Create a new directory somewhere under
/home/{USR}
I created one called backup.sudo nano /etc/fstab
and add this line//{NAS_IP} /home/{USR}/backup cifs username={NAS_Username},password={NAS_Password},x-systemd.automount 0 0
To mount
sudo mount /home/{USR}/backup
Once It mounts successfully set the
{path_to_backup_location}
to the location you mounted your NAS to. In this case it would be/home/{USR}/backup
. Now whatever file you copy into that directory it will copy to your NAS.I hope this saves your butt one day, I am sure these instructions can be modified to work with other install environments so encourage you to share your backup instructions. If you have any tips you'd like to share please let me know! Hoping to figure out a way to get Reactor to check if the back up was successful.
-
Can we get the terminology right? A Ruleset is a collection of Rules. A Rule contains triggers, constraints, and reactions.
-
I am paranoid that my trusty old Rpi 3b+ running on an old micro SD card is gonna eventually give out, and the countless hours I have put into Reactor will be put to waste. I created a simple script that backups the Reactor directory, dates it and auto deletes any previous backups 7 days old. I have a rule in Reactor that calls a SSH command to my Rpi to run the backup script once every day. Here's the nitty gritty:
The Backup Script
SSH into your Rpi and use the command
sudo nano reactorbackup.sh
. In the file editor that opens up enter these lines (remember to change the paths):#! /bin/bash sudo cp -r {path_to_reactor_directory} {path_to_backup_location}-$(date +'%Y_%m_%d_%H:%M') sudo {path_to_backup_location} -maxdepth 1 -mtime +7 -exec rm -rf {} +
Some notes about the script: you can update the date format if you want something else, also change the
+7
in-mtime +7
changes the amount of days-old backups to be deleted. Currently it's 7 days.Home Assistant Config
There are many ways you can send SSH commands from HA to other devices, the simplest way I have found is using this HACS integration. Following the install instructions on the GitHub page you will now have a new service called
SSH Command: Execute command
. This is the service we will use in our Reactor ruleset.Reactor Backup Rule
Now it's time to create a rule that auto runs the previously created
reactorbackup.sh
script. Very simple stuff:Triggers:
Date/Time between 1:00am and 1:15am (can edit this to whatever time you'd like).
Set Reaction:
Entity Action - hass>system - call service
service ID: SSH Command: Execute command
Data:
host: {Rpi IP Adress}
port: 22
user: {user}
pass: {password}
command: sh reactorbackup.shI do recommend you test this and do check on your backup files, I have yet to think of a way to check if the backup was successful without needing to go into the backup location but its better than nothing.
Bonus: Mounting NAS to your RPi
So a backup is only useful if you can access it in the event of failure. If you backup your Reactor directory somewhere in your RPi, it's basically useless if something happens to your RPi. I mapped my NAS so that I can backup the Reactor directory into my NAS. Here's how:
Create a new directory somewhere under
/home/{USR}
I created one called backup.sudo nano /etc/fstab
and add this line//{NAS_IP} /home/{USR}/backup cifs username={NAS_Username},password={NAS_Password},x-systemd.automount 0 0
To mount
sudo mount /home/{USR}/backup
Once It mounts successfully set the
{path_to_backup_location}
to the location you mounted your NAS to. In this case it would be/home/{USR}/backup
. Now whatever file you copy into that directory it will copy to your NAS.I hope this saves your butt one day, I am sure these instructions can be modified to work with other install environments so encourage you to share your backup instructions. If you have any tips you'd like to share please let me know! Hoping to figure out a way to get Reactor to check if the back up was successful.
@Pabla This post inspired me.
After poking at this a bit I went a different route and have my MSR backing up to a Github repo. The only thing it's unhappy about is backing up the
storage
directory due to the sheer number of files so I circumvented that and have that directory tarred into an archive and then the archive itself is backed up. Github is happy, I'm happy. -
T toggledbits locked this topic on