Homeassistant and Ubuntu Upgrade Script

Hi, Since I’m running my Home-Assistant in a virtual python environment and on an Ubuntu server, I’ve written a small bash-script to ease my updates.

If anyone has a solution to make it autoupdate whenever a new Home-Assistant update is available, I would love to hear from you.

upgrade_script.sh

#!/bin/bash
TEXT_RESET='\e[0m'
TEXT_YELLOW='\e[0;33m'
TEXT_RED_B='\e[1;31m'

echo ''
echo ''
echo '##########################################'
echo 'Upgrading Home Assistant to latest version'
date
echo '##########################################'
echo ''
echo ''
echo 'Stopping Home-Assistant'
service homeassistant stop
echo ''
echo 'Updating Linux'
apt-get update
echo -e $TEXT_YELLOW
echo 'APT update finished...'
echo -e $TEXT_RESET
echo 'Upgrading dist'
apt-get -y dist-upgrade
echo -e $TEXT_YELLOW
echo 'APT distributive upgrade finished...'
echo -e $TEXT_RESET
apt-get -y upgrade
echo -e $TEXT_YELLOW
echo 'APT upgrade finished...'
echo -e $TEXT_RESET
echo 'Autoremoving unused services'
apt-get -y autoremove
echo -e $TEXT_YELLOW
echo 'APT auto remove finished...'
echo -e $TEXT_RESET
echo 'Cleaning unused configurations'
apt-get clean
echo 'Purging unnessesary packets'
apt-get purge -y $(dpkg -l | awk '/^rc/ { print $2 }')

echo 'Changing directory'
cd /srv/homeassistant
echo 'Activating virtual environment'
source /srv/homeassistant/bin/activate
echo ' Updating PIP'
pip install --upgrade pip
echo ' Running PIP / Upgrading'
python3 -m pip install --upgrade homeassistant
echo 'Deactivating virtual environment'
deactivate
echo 'Restarting Home Assistant service'
service homeassistant restart
1 Like

Similarly here’s my HA downgrade script for getting back to the version that worked last quickly:

echo "Stopping HomeAssistant"
sudo systemctl stop [email protected]
read -r -p "Do you want to compile remove the HomeAssistant database?" response
if [ $response = "y" ]
    then
            echo "Removing old HomeAssistant database"
            rm /home/homeassistant/.homeassistant/home-assistant_v2.db
    else
            echo "Continuing on without removing the database..."
fi
read -r -p "What version do you want to revert back to?" version

echo "Activating Python vEnv"
source /srv/homeassistant/bin/activate
python3 -m pip install homeassistant==$version
deactivate
echo "Starting HomeAssistant"
sudo systemctl start [email protected]

Works on Debian, so should work on Debian variants like Ubuntu.

I use this script:

#!/bin/bash
bash << EOF

clear
echo "**********************************************************"
echo "*********************** Stop HA **************************"
echo "**********************************************************"
echo " "
echo "systemctl stop home-assistant@homeassistant"
echo " "
systemctl stop home-assistant@homeassistant
        sudo su -s  /bin/bash homeassistant << DOF
        sleep 1
        echo " "
        echo "********************* HA is Stopped **********************"
        echo " "
        cd  /srv/homeassistant
        source bin/activate
        python3 -m pip install --upgrade homeassistant
        sleep 2
DOF
echo "**********************************************************"
echo "********************** Start  HA *************************"
echo "**********************************************************"
echo "systemctl start home-assistant@homeassistant"
systemctl start home-assistant@homeassistant
echo " "
sleep 3
echo "**********************view HA log*************************"
echo " "
sleep 3
journalctl -f -u home-assistant@homeassistant
EOF

I just run it once i see HA update available. Maybe set an automation that if update available run script?

Bookmarking this thread!!

Good info here.

Many thanks.

Your script contains apt-get so I assume that you are running the whole bash script as sudo?
How do you then avoid that the HA upgrade part is then not mixing up permissions between your privileged user and the user running HA?

I had to go through some more trouble to script the HA upgrade and retain correct permissions.