Skip to content

Instantly share code, notes, and snippets.

@ProxiBlue
Last active January 23, 2025 11:04
Show Gist options
  • Save ProxiBlue/33f8d50266b845eba3a5c4a4b3eaac16 to your computer and use it in GitHub Desktop.
Save ProxiBlue/33f8d50266b845eba3a5c4a4b3eaac16 to your computer and use it in GitHub Desktop.
HAOS Immich add-on by alexbelgium db restore
#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
# ref: https://github.com/alexbelgium/hassio-addons/discussions/1467#discussioncomment-11699236
# this script will look for your immich database backup file placed in path
# /share/postgresql_immich_restore.sql
#
# it must not be gzipped.
# requires: Terminal add-on to place files, a way you knwo how to place your .sql on HAOS server.
# I used an external USB I mounted.
#
# process:
#
# assuming this is a fresh new install.
# if not, you must stop immich add-on, uninstall the postgreSQL add-on, opt to delete all data, and re-install
#
# 1. Install the needed postgreSQL addon.
# 2. After install, before you start it, edit settings to be as below.
#
#POSTGRES_DB: postgres
#POSTGRES_PASSWORD: LEAVE AS IS
#POSTGRES_USER: immich
#
# It is vital no existing immich db gets created when this is installed.
#
# 3. start the service. wait 10s. stop. start again.
# I found this works.
# just that one initial start seems to cause connection issues.
# 4. place this file on the HAOS server in path: ```/homeassistant/addons_autoscripts/immich.sh```
# 5. ensure is executable: ```chmod +x /homeassistant/addons_autoscripts/immich.sh```
# 6. Install immich add-on, or just start it if already installed.
# 7 If all goes well, you will see import happen in logs
# ensure you place all your old files in /share/immich
if [ -f /share/postgresql_immich_restore.sql ]; then
# Settings parameters
export DB_USERNAME=$(bashio::config 'DB_USERNAME')
export DB_HOSTNAME=$(bashio::config 'DB_HOSTNAME')
export DB_PASSWORD=$(bashio::config 'DB_PASSWORD')
export DB_DATABASE_NAME=$(bashio::config 'DB_DATABASE_NAME')
export DB_PORT=$(bashio::config 'DB_PORT')
export JWT_SECRET=$(bashio::config 'JWT_SECRET')
echo "Your previous database was detected at startup under /share/postgresql_immich_restore.sql and will be imported replacing existing db"
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/postgres" -c "CREATE DATABASE $DB_DATABASE_NAME"
sed -i 's|SELECT pg_catalog\.set_config('\''search_path'\'', '\'''\'''\'' false);|SELECT pg_catalog.set_config('\''search_path'\'', '\''public, pg_catalog'\'', true);|g' "/share/postgresql_immich_restore.sql"
psql "postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOSTNAME:$DB_PORT/immich" < "/share/postgresql_immich_restore.sql" || true
rm -rf /share/postgresql_immich_restore.sql
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment