Automating Shell Scripts

So I have to backup plans in place now thanks to the great info on here and @turboc and others.

  1. rsync
    I use rsync over ssh to back up the configuration directory to another Pi I have on the network that does duty as a file server.

  2. dd
    I set up a command to run dd over ssh to make an image of the SD card and put that image on the file server Pi mentioned above.

What I am wondering is whether or not there is any benefit or use in automating this in HA? Or am I better off just using crontab?

I would like to run the rsync from within HA so I can synchronize those files as needed but I am not sure how to do this in HA. An input slider doesn’t seem like the thing you would use, and an input select might be better, but I am not sure how to set such a thing up.

Anyone have examples of how they are using and automating shell scripts from within HA?

HA automation

- alias: 'backup automation'
  trigger:
    platform: time
    hours: 0
    minutes: 1
    seconds: 0
  action:
    service: shell_command.backupha

Shell script

# Developed 30Nov2016
# Chip Cox
# designed to backup hass directory from a Raspberry Pi running Home Assistant in a virtual environment

# Set Directory and filename variables
# This is the mount point for my usb drive.  
# your destination directory will probably be different
filedir=/mnt/usbdrive/

# I'm starting this backup in the home directory for the hass user.
srcdir="~"

#current date format DDMONYYYY
current_time=$(date "+%d%b%y")

# build output filename
filename=$filedir"hass"$current_time"backup.tar"

# display backup information.
echo $filename > ~/backup.log
echo "Current Time : $current_time" >> ~/backup.log

# Create the tar file given with verbose output and put the contents of srcdir in it.
# excluding .cache directory
#
cmd="tar -cvf $filename --exclude .cache $srcdir"

echo $cmd >> ~/backup.log

# DoIt
eval $cmd >> ~/backup.log
#now that you have the tar file, zip it up to save space.
gzip $filename >> ~/backup.log

Just realized I had left this line out that’s in my configuration.yaml

shell_command:
`  backupha: '/home/hass/habackup'`

@turboc, Was that in the documentation somewhere? If so, I am sorry, I looked in the Components section and saw shell script it didn’t lend itself well to helping me out.

LOL, yea, some of the documentation is that way. I’ve had lots of help from the people on the board too, just like you. This is just what I am using for my nightly backups

It’s under shell command

https://home-assistant.io/components/shell_command/

Where should one place shell commands? I use hass.io, but is /config the right location?

1 Like

Hi all - I am trying to automate a github backup similar to this.

I am able run the shell script from terminal. But, I cannot get an automation to execute this script.
here is what I have tried:

- id: '1600259092041'
  alias: HA backup github (youtube example)
  trigger:
  - at: 02:00
    platform: time
  condition: []
  action:
  - data:
      addon: a0d7b954_ssh
      input: /root/config/ha_gitpush.sh
    service: hassio.addon_stdin
  mode: single

###########

- alias: HA backup github (community example)
  trigger:
    platform: time
    at: 02:30:00
  action:
    service: shell_command.backupha

with configuration.yaml showing this to support the 2nd automation:

shell_command:
  backupha: '/root/config/ha_gitpush.sh'
1 Like