File: //var/tmp0/brainy/ssh/postgres/postgresql.sh
#! /bin/bash
a=$1
ver=`cat /etc/brainy/globals.php | grep 'OS_VERSION' | tr -d '\n'\\$GLOBALS['OS_VERSION']=\\"\\'\\ \\;\\:`
if [[ $a == "install" ]];then
if [ $ver == "debian8" ];then
#
else
# v2 detected
v2=`cat /etc/brainy/globals.php | grep 'v2' | tr -d '\n'\\$GLOBALS['v2']=\\"\\'\\ \\;\\:`
if [ "$v2" == "yes" ];then
yum -y install --disablerepo=* --enablerepo=myrepo-centos,app_221,glib_v21,python3,perl postgresql-server postgresql-contrib postgresql-libs postgresql-devel
else
yum -y install --disablerepo=* --enablerepo=myrepo-centos postgresql-server postgresql-contrib postgresql-libs postgresql-devel
#yum -y install postgresql-server postgresql-contrib postgresql-libs postgresql-devel
fi
fi
#postgresql-setup initdb
service postgresql initdb
sleep 5
sed -i "s@.*listen_addresses.*@listen_addresses = '*'@g" /var/lib/pgsql/data/postgresql.conf
service postgresql start
if [ -f '/etc/brainy/tmp/pg_hba.conf' ]; then
rm -rf /var/lib/pgsql/data/pg_hba.conf
cp -f /etc/brainy/tmp/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf
chown postgres:postgres /var/lib/pgsql/data/pg_hba.conf
if [ -z $(cat /var/lib/pgsql/data/pg_hba.conf | grep -Ei "(local)+[ ]{1,}(all)+[ ]{1,}(postgres)") ]; then
if [ $ver == "centos7" ];then
sed -i 2i'local all postgres peer' /var/lib/pgsql/data/pg_hba.conf
fi
if [ $ver == "centos6" ];then
sed -i 2i'local all postgres ident' /var/lib/pgsql/data/pg_hba.conf
fi
fi
if [ $ver == "centos6" ];then
if [ -n $(cat /var/lib/pgsql/data/pg_hba.conf | grep peer) ]; then
sed -i "s@peer@ident@g" /var/lib/pgsql/data/pg_hba.conf
fi
fi
if [ -z $(cat /var/lib/pgsql/data/pg_hba.conf | grep -Ei "(host)+[ ]{1,}(all)+[ ]{1,}(all)+[ ]{1,}(::1/32)") ]; then
if [ $(wc -l /var/lib/pgsql/data/pg_hba.conf | awk '{print $1}') -gt 3 ]; then
sed -i 4i'host all all ::1/32 md5' /var/lib/pgsql/data/pg_hba.conf
else
echo 'host all all ::1/32 md5' >> /var/lib/pgsql/data/pg_hba.conf
fi
fi
else
echo '# TYPE DATABASE USER ADDRESS METHOD' > /var/lib/pgsql/data/pg_hba.conf
if [ $ver == "centos7" ];then
echo 'local all postgres peer' >> /var/lib/pgsql/data/pg_hba.conf
fi
if [ $ver == "centos6" ];then
echo 'local all postgres ident' >> /var/lib/pgsql/data/pg_hba.conf
fi
echo 'host all all 127.0.0.1/32 md5' >> /var/lib/pgsql/data/pg_hba.conf
echo 'host all all ::1/32 md5' >> /var/lib/pgsql/data/pg_hba.conf
fi
#востановлеение из дампа
if [ -f '/etc/brainy/tmp/bacup_db_postgres.sql' ]; then
sudo -S -u postgres psql < /etc/brainy/tmp/bacup_db_postgres.sql
fi
service postgresql restart
service postgresql enable
echo 'Complete'
fi
if [ "$a" == "delete" ];then
#делаем дамп
sudo -S -u postgres pg_dumpall > /etc/brainy/tmp/bacup_db_postgres.sql
cp -f /var/lib/pgsql/data/pg_hba.conf /etc/brainy/tmp/pg_hba.conf
rpm -e --nodeps postgresql-contrib postgresql-devel postgresql-server
rm -rf /var/lib/pgsql
#rpm -qa | grep postgresql | xargs rpm -e --nodeps
echo 'Succesfully deleted'
fi