fusionpbx-install.sh/debian/resources/postgresql.sh

112 lines
3.8 KiB
Bash
Raw Normal View History

2016-04-29 21:24:22 +02:00
#!/bin/sh
2017-03-04 20:56:38 +01:00
#move to script directory so all relative paths work
cd "$(dirname "$0")"
#includes
. ./config.sh
2017-03-05 18:45:03 +01:00
. ./colors.sh
. ./environment.sh
2017-03-04 20:56:38 +01:00
2016-04-29 21:24:22 +02:00
#send a message
echo "Install PostgreSQL"
#generate a random password
password=$(dd if=/dev/urandom bs=1 count=20 2>/dev/null | base64)
2017-01-11 20:39:05 +01:00
#install message
2016-04-29 21:24:22 +02:00
echo "Install PostgreSQL and create the database and users\n"
2017-01-11 20:39:05 +01:00
#included in the distribution
2017-03-04 21:20:01 +01:00
if [ ."$database_repo" = ."system" ]; then
if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
2020-08-19 21:23:09 +02:00
apt-get install -y sudo postgresql
else
apt-get install -y sudo postgresql-client
fi
2017-03-04 21:22:57 +01:00
fi
2017-01-11 20:39:05 +01:00
2024-01-10 18:10:22 +01:00
#make sure keyrings directory exits
mkdir /etc/apt/keyrings
2017-01-11 20:39:05 +01:00
#postgres official repository
2017-03-04 21:22:57 +01:00
if [ ."$database_repo" = ."official" ]; then
2024-12-30 16:59:22 +01:00
apt install -y gpg
sh -c 'echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg
chmod 644 /etc/apt/keyrings/pgdg.gpg
2023-09-21 22:55:41 +02:00
apt-get update && apt-get upgrade -y
if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
2020-08-19 21:23:09 +02:00
if [ ."$database_version" = ."latest" ]; then
apt-get install -y sudo postgresql
2023-09-21 22:51:30 +02:00
else
apt-get install -y sudo postgresql-$database_version
fi
2020-08-19 21:23:09 +02:00
else
apt-get install -y sudo postgresql-client
2017-11-07 03:57:35 +01:00
fi
2017-03-04 21:20:01 +01:00
fi
2016-04-29 21:24:22 +02:00
2017-11-07 01:55:38 +01:00
#add PostgreSQL and 2ndquadrant repos
2017-03-04 21:22:57 +01:00
if [ ."$database_repo" = ."2ndquadrant" ]; then
if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
2020-08-19 21:23:09 +02:00
apt install -y curl
curl https://dl.2ndquadrant.com/default/release/get/deb | bash
if [ ."$os_codename" = ."buster" ]; then
sed -i /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list -e 's#buster#stretch#g'
fi
if [ ."$os_codename" = ."bullseye" ]; then
sed -i /etc/apt/sources.list.d/2ndquadrant-dl-default-release.list -e 's#bullseye#stretch#g'
fi
2020-08-19 21:23:09 +02:00
apt update
apt-get install -y sudo postgresql-bdr-9.4 postgresql-bdr-9.4-bdr-plugin postgresql-bdr-contrib-9.4
fi
2017-03-04 21:20:01 +01:00
fi
2016-06-08 17:28:47 +02:00
2017-03-05 20:20:19 +01:00
#install the database backup
2018-12-11 09:26:25 +01:00
#cp backup/fusionpbx-backup /etc/cron.daily
#cp backup/fusionpbx-maintenance /etc/cron.daily
#chmod 755 /etc/cron.daily/fusionpbx-backup
#chmod 755 /etc/cron.daily/fusionpbx-maintenance
#sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-backup
#sed -i "s/zzz/$password/g" /etc/cron.daily/fusionpbx-maintenance
2017-03-05 19:25:06 +01:00
#initialize the database
pg_createcluster $database_version main
2023-09-07 01:06:28 +02:00
#replace scram-sha-256 with md5
sed -i /etc/postgresql/$database_version/main/pg_hba.conf -e '/^#/!s/scram-sha-256/md5/g'
#systemd
if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
systemctl daemon-reload
systemctl restart postgresql
fi
#init.d
#/usr/sbin/service postgresql restart
2016-06-04 20:47:40 +02:00
#move to /tmp to prevent a red herring error when running sudo with psql
cwd=$(pwd)
cd /tmp
2017-04-23 09:54:00 +02:00
if [ ."$database_host" = ."127.0.0.1" ] || [ ."$database_host" = ."::1" ] ; then
2023-09-07 01:17:52 +02:00
#reload the config
sudo -u postgres psql -c "SELECT pg_reload_conf();"
2020-08-19 21:23:09 +02:00
# add the databases, users and grant permissions to them
sudo -u postgres psql -c "CREATE DATABASE fusionpbx;";
sudo -u postgres psql -c "CREATE DATABASE freeswitch;";
sudo -u postgres psql -c "CREATE ROLE fusionpbx WITH SUPERUSER LOGIN PASSWORD '$password';"
sudo -u postgres psql -c "CREATE ROLE freeswitch WITH SUPERUSER LOGIN PASSWORD '$password';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE fusionpbx to fusionpbx;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to fusionpbx;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE freeswitch to freeswitch;"
# ALTER USER fusionpbx WITH PASSWORD 'newpassword';
fi
2016-06-04 20:47:40 +02:00
cd $cwd
2016-04-29 21:24:22 +02:00
#set the ip address
2017-01-12 04:49:15 +01:00
#server_address=$(hostname -I)