GitHub automated backup

Hi All,
I am trying to automate a github backup with a shell script.
I can run my shell script file with no issues from a terminal (both with ssh and in the ‘Terminal & SSH’ addon). but, when I run through an automation I receive an error ’ Error running command: bash ha_gitpush.sh, return code: 128’

#ha_gitpush.sh

# Go to /config folder or 
# Change this to your Home Assistant config folder if it is different
cd /config

# Add all files to the repository with respect to .gitignore rules
git add .

# Commit changes with message with current date stamp
git commit -m "config files on `date +'%d-%m-%Y %H:%M:%S'`"

# Push changes towards GitHub
git push -u origin master

Error code 128 means that email and password isn’t configured correctly.

Thanks - I am stuck at why would the shell script run properly from terminal and not from the automation?

I set up a ssh key that authenticates properly from the terminal.

So can you try adding auth to the script?

that is a great idea -
I tried adding this but same result:

git config core.sshCommand "ssh -i /config/.ssh/id_rsa -F /dev/null"

Okay, so can you make a plain venv, and execute your sh file from there?

sorry KTibow - i’m not sure how to do that

Google how to use venv

sure I can , i don’t believe I have python installed since i have HA running through the Home Assistant image.

What is your thought of trying the script with the venv?

Just on the PC you use to write this. Just to make sure no auth is cached.

OK - could you please help me think through this? The public SSH key is located on my Home Assistant (RPi). Are you asking me to set up this script (setup venv, setup a new ssh key, run the script, etc.) from another computer with venv?

What do you mean the SSH key is located? Basically on another computer that hasn’t been set up with Git auth / credentials, or a venv, try running your script.

what i mean is that I am using an SSH key (not username / password) for authentication with github.
I’m trying to replicate this.

Sorry I haven’t done more research. How did you store your SSH keys?

thanks much for your help - I generated an SSH and then saved the public key on github:

mkdir .ssh 
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Then copied my public key to github

then:

git config core.sshCommand "ssh -i /config/.ssh/id_rsa -F /dev/null"

huh - i set this up again and it seems to run now… not sure what I did correctly /incorrectly - but it works!

I’ve exactly the same problem.
Have you ever figured out how to solve the problem?

I also set it up a second time and reboot serval times but I always get error 128 :frowning:

Hi Marius82 - the one thing i noticed is that i need to work through the terminal interface add-on vs. secure shelling through putty,etc.

This helped me a lot.

When you SSH into HA you’re actually ssh’ing into an add-on which is a different docker container from HA itself. Each docker container has its own filesystem and installed packages, successfully running it while ssh’ed really doesn’t tell you anything about whether it will work when run from HA.

all above was right, but I had to open a console of HA execute the following command (thanks @VDRainer!):

docker exec -it homeassistant bash

then run the shell script (which was already working in the regular terminal)

bash /config/ha_gitpush.sh

the system then asks me to put github.com to the list of konwn hosts.
After that, the shell script worked again, while starting to a HA-automation.

But it wasn’t resistant because of:

Normally you just go through this once and then you are good to go, it never prompts you to verify authenticity again. However with HA this is another gotcha. The problem is that by default the known_hosts file is stored in /root/.ssh . Which means if you stop here your sensor will appear to work but will break next update when /root is wiped clean.

in order to set the proposed ssh option -o UserKnownHostsFile=... to git. I used this command:

git config core.sshCommand 'ssh -o UserKnownHostsFile=/config/.ssh/known_hosts -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa -F /dev/null'
5 Likes

thanks!. exactly what i needed!