Backing up to Gitea

I am trying to make an automation that will back up to my private Gitea instance and I have it mostly working.

So I have the following setup:

configuration.yml

shell_command:
  backup_ha: /config/scripts/backup-ha.sh

/config/scripts/backup-ha.sh

#!/bin/bash

# Change directory to /config
cd /config

# Git add all the files
git add .

# Commit a message to the change - also add date and time.
git commit -m "Config files on `date +'%d-%m-%Y %H:%M:%S'`"

# Push the changes to Gitea
git push origin master

automation:

alias: Backup Config
description: Pushes any changes made to Home Assistant to Gitea every hour
trigger:
  - platform: time_pattern
    hours: '1'
condition: []
action:
  - service: script.backup_ha_to_gitea
mode: single

I say that it is mostly working because it is calling the script and firing the steps, but it is doing everything but actually pushing the changes to my Gitea instance. I can confirm this by using ‘git status’ using an SSH session. I am also able to call the sh file in the same SSH session and it runs the script all the way through including the push so I know it is not a communication system between the 2 programs.

Does anyone know of anything that I can try to get this automated?

Thanks

Did you actually tried the script in a docker session of HA, ie. via docker exec -it homeassistant /bin/bash ?
That’s where the script is executed.

As there is no provision for keys and password in your script, I doubt it.

I am not using a Dockerized version of HA. I am using HA OS running on a Raspberry Pi4.

The authorization between HA and Gitea is being handled over SSH to I wouldn’t have to pass through my username and password on Gitea.

Does the user which is used to run home assistant has a private key and is this key allowed to push to the origin repository?

If you execute it on your own it will use your user and key from your home or ssh-agent.

If it’s executed from home assistant it will do this as the user that home assistant is using.

HA runs inside a container in HA OS as well.
The only officially supported installation method which doesn’t is “Core”, i.e. a python venv.

Works fine only as long as you have your private key in “~/.ssh/id
”, which, in the container is “/root/.ssh/
”.
Unless you did something about it, it definitely won’t work out-of-the-box.

I have it set up via the git config file to use the key pair that is located in /config/.ssh. If I understand this, it should be using that key pair from that config no matter what user is calling the script.

[core]
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  sshCommand = ssh -i /config/.ssh/privateGitea -F /dev/null
[remote "origin"]
  url = [email protected]:ltm/home-assistant.git
  fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
  remote = origin
  merge = refs/heads/master
[user]
  email = [REDACTED]
  name = Home Assistant

The domain used is a domain that I use within my LAN. It is routed correctly to be used like a real domain.

Mmm
 Where did you get that, exactly :wink: ?
I’m pretty sure the home of root is not /config inside the HA container.

It could also be related to the owner/permissions of the private key.
Openssh refuses to use the key if it doesn’t belong to the user and if there are read permissions for group or other. So ensure that the permissions are set to 600.

https://thehomeautomator.com.au/automatically-backup-home-assistant-to-gitea-github/

Overall, I give up on this. I have tried everything I have seen in threads, I have tried different paths including adding /root/, permissions seems to be fine
 I just don’t understand why it will do everything but push to Gitea. I can run the script manually and it is fine. I just can’t automate it.

That’s the part from the tutorial where you specify what private key is used to access gitea.

But generally, your confusion comes from the fact that you don’t grasp that when you do it “manually”, you are running it from a separate docker container, the “SSH & Web terminal” addon.

When you execute it as a shell command, it runs in a completely different context : The Home Assistant container.

What you want to do is insert the “git config” command in of your bash script, before the “push”, as you cannot do it easily directly in HA (and you would have to redo it each time you upgrade, anyway).

1 Like

I added this to the bash script and it still is not pushing. The bash script is now:

#!/bin/bash

# Change directory to /config
cd /config

# Git add all the files
git add .

# Commit a message to the change - also add date and time. 
git commit -m "Config files on `date +'%d-%m-%Y %H:%M:%S'`"

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

# Push the changes to Gitea
git push origin master

Why “root/config/.ssh
”? It’s wrong


I tried it with /root/config/.ssh/privateGitea and /config/.ssh/privateGitea. Just forgot to delete the /root before posting it.

And that’s actually the full path of your private key?

Yes, that is correct.

Do you use a SSH key with passphrase? When using that it didn’t work for me either.
Now I have made a new SSH key without a passphrase and it works using the following bash script.

#!/bin/bash

# Change directory to /config
cd /config

# Git add all the files
git add .

# Commit a message to the change with date and time
git commit -am "Changes on $(date +'%d-%m-%Y %H:%M:%S')"

# Source https://blog.ceard.tech/2020/05/home-assistant-docker-and-using-ssh.html
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new -i /ssl/.gitea/gitea_key_no_pp -F /dev/null'

# Push the changes to Gitea
git push origin master

And just to be sure

needs to be

action:
  - service: shell_command.backup_ha_to_gitea

No passphrase in the key file.

Still not able to get it to work.

backup-ha.sh

#!/bin/bash

# Change directory to /config
cd /config

# Git add all the files
git add .

# Commit a message to the change with date and time
git commit -am "Config files on `date +'%d-%m-%Y %H:%M:%S'`"

# Source https://blog.ceard.tech/2020/05/home-assistant-docker-and-using-ssh.html
git config core.sshCommand 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=accept-new -i /config/.ssh/privateGitea -F /dev/null'

# Push the changes to Gitea
git push origin master

Automation:

alias: Push HA Config to Gitea
description: Pushs any changes made to Home Assistant to Gitea
trigger:
  - platform: time_pattern
    hours: '1'
condition: []
action:
  - service: shell_command.backup_ha_to_gitea
mode: single

I pretty much just gave up on this and do manual backups.

I had the same problem and adding the

git config core.sshCommand...

solved the problem.

In your case LilTrublMakr you need to change the
- service: shell_command.backup_ha_to_gitea
for
- service: shell_command.backup_ha
which is the name you are using in your config yml