upgrade_pg.sh 1.96 KB
Newer Older
1
2
3
4
5
#!/bin/bash
set -e

## Set the old version that we want to upgrade from.
TO_VERSION=$(pacman -Q postgresql | grep -Po '(?<=postgresql )[0-9]+\.[0-9]')
6
7
8

to_major=${TO_VERSION%%.*}
if (( $to_major != 12 )); then
9
10
	# NOTE: When this happens you should check the changelog and add any
	# necessary changes here. Then bump the version check.
11
	echo "ERROR: major upgrade detected, aborting..."
12
13
14
	exit 1
fi

15
16
17
FROM_VERSION="$(cat /var/lib/postgres/data/PG_VERSION)"
if [[ $FROM_VERSION == $TO_VERSION ]]; then
	echo "ERROR: from and to versions are equal. Have you already updated?! Aborting..."
18
19
20
	exit 1
fi

21
22
# free space check
used_space=$(df --local --output=pcent /var/lib/postgres/ | grep -Po '[0-9]{1,3}(?=%)')
23
if (( $used_space >= 50 )); then
24
25
26
27
	echo "ERROR: not enough free space for upgrade with backup, aborting..."
	exit 2
fi

28
29
if [[ -d /var/lib/postgres/data-$FROM_VERSION ]]; then
	echo "ERROR: backup data-$FROM_VERSION directory already exists, aborting..."
30
31
	exit 3
fi
32
33
34
35

pacman -S --needed postgresql-old-upgrade
chown postgres:postgres /var/lib/postgres/
su - postgres -c "mv /var/lib/postgres/data /var/lib/postgres/data-$FROM_VERSION"
36
su - postgres -c 'mkdir /var/lib/postgres/data'
37
su - postgres -c 'chattr -f +C /var/lib/postgres/data' || :
38
39
40
41
42
su - postgres -c 'initdb --locale en_US.UTF-8 -E UTF8 -D /var/lib/postgres/data'
vimdiff /var/lib/postgres/{data,data-$FROM_VERSION}/pg_hba.conf
vimdiff /var/lib/postgres/{data,data-$FROM_VERSION}/postgresql.conf
cp -avxt /var/lib/postgres/data /var/lib/postgres/data-$FROM_VERSION/{fullchain,chain,privkey}.pem

43
systemctl stop postgresql.service
44
45
su - postgres -c "pg_upgrade -b /opt/pgsql-$FROM_VERSION/bin/ -B /usr/bin/ \
	-d /var/lib/postgres/data-$FROM_VERSION -D /var/lib/postgres/data"
46
47
systemctl daemon-reload
systemctl start postgresql.service
48
49

su - postgres -c /var/lib/postgres/analyze_new_cluster.sh
50
51
# We probably want to manually delete things for now in case this script misses
# some stuff
52
#su - postgres -c /var/lib/postgres/delete_old_cluster.sh