From a4a5d7f727c1b44d6ab69653036eb822e96ef264 Mon Sep 17 00:00:00 2001
From: Tobias Reisinger <tobias@msrg.cc>
Date: Sat, 9 Dec 2023 20:51:45 +0100
Subject: [PATCH] Remove diagram script; Add postgres-update script

---
 .gitignore                |  1 +
 _utils/postgres-update.sh | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
 create mode 100644 _utils/postgres-update.sh

diff --git a/.gitignore b/.gitignore
index 8ef34d1..96c0f0f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -12,6 +12,7 @@ diagram_assets/
 /forgejo-runner/
 /gitea/
 /gitea-runner/
+/healthcheck/
 /homebox/
 /influxdb/
 /jellyfin/
diff --git a/_utils/postgres-update.sh b/_utils/postgres-update.sh
new file mode 100644
index 0000000..9a4eb1a
--- /dev/null
+++ b/_utils/postgres-update.sh
@@ -0,0 +1,38 @@
+#!/usr/bin/env sh
+# shellcheck disable=SC2162
+
+printf "\nWARNING: This script is work in progress and not tested yet. "; read
+printf "\nStep 1: Backup done? "; read
+
+printf "\nStep 2: Stop services - continue? "; read
+systemctl stop postgresql.service
+systemctl status postgresql.service --no-pager
+
+
+printf "\nStep 3: Create directories and move data - continue? "; read
+[ -d /var/lib/postgres/data ] && mv /var/lib/postgres/data /var/lib/postgres/olddata
+mkdir /var/lib/postgres/data /var/lib/postgres/tmp
+chown postgres:postgres /var/lib/postgres/data /var/lib/postgres/tmp
+cd /var/lib/postgres/tmp || exit
+
+
+printf "\nStep 4: Init new database - continue? "; read
+sudo -u postgres initdb -D /var/lib/postgres/data --locale=en_US.UTF-8 --encoding=UTF8
+
+
+printf "\nStep 5: Upgrade to new version - continue? "; read
+printf "Old version: "; read -r pg_version
+pg_path="/opt/pgsql-$pg_version/bin"
+[ -d "$pg_path" ] || exit
+sudo -u postgres pg_upgrade -b "$pg_path" -B /usr/bin -d /var/lib/postgres/olddata -D /var/lib/postgres/data
+
+
+printf "\nStep 6: Restart database - continue? "; read
+systemctl start postgresql.service
+systemctl status postgresql.service --no-pager
+
+
+printf "\nStep 7: Cleanup - continue? "; read
+sudo -u postgres vacuumdb --all --analyze-in-stages
+rm -r /var/lib/postgres/olddata /var/lib/postgres/tmp
+printf "\nDone. "; read