__ungleich_postgresql/manifest
2020-05-01 15:36:20 +02:00

128 lines
3.8 KiB
Bash

#!/bin/sh
#
# 2013-2020 ungleich glarus ag (cdist at ungleich.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
user="$(cat "$__object/parameter/user")"
os=$(cat $__global/explorer/os)
osversion=$(cat "$__global/explorer/os_version")
case "$os" in
alpine)
postgres_lib="postgresql-dev"
postgres_server="postgresql"
;;
debian|devuan)
postgres_lib=libpq-dev
case $osversion in
6*)
postgres_version=8.4
;;
7*)
postgres_version=9.1
;;
8*)
postgres_version=9.4
;;
9*|ascii/ceres|ascii)
postgres_version=9.6
;;
10*|11*|beowulf/ceres|beowulf)
postgres_version=11
;;
*)
echo "Unsupported OS Version: $osversion" >&2
exit 1
;;
esac
# Allow version override
if [ -f "$__object/parameter/version" ]; then
postgres_version=$(cat "$__object/parameter/version")
fi
postgres_server="postgresql-${postgres_version}"
;;
ubuntu)
postgres_lib=libpq-dev
case $osversion in
20.04)
postgres_version=12
;;
*)
echo "Unsupported OS Version: $osversion" >&2
exit 1
;;
esac
postgres_server="postgresql-${postgres_version}"
;;
*)
echo "Unsupported OS: $os" >&2
exit 1
;;
esac
__package "$postgres_server" --state present
__package "$postgres_lib" --state present
require="__package/$postgres_server __package/$postgres_lib" __process postgres --start "service postgresql start"
################################################################################
# Configure PostgreSQL
#
require="__process/postgres" __postgres_role "$user" \
--state present --login --createdb --superuser
require="__postgres_role/$user" __postgres_database \
"$user" --state present --owner "$user"
require="__package/$postgres_server" __start_on_boot postgresql
################################################################################
# Create backups
#
if [ ! -f "$__object/parameter/no-backup" ]; then
require="__package/$postgres_server" __directory /var/backups/postgres \
--owner postgres --group postgres --parents
require="__package/$postgres_server __directory/var/backups/postgres" __cron \
pg_dumpall_under_day \
--user postgres \
--hour 8,9,10,11,12,14,15,16,17,18 --minute 0 \
--command 'nice pg_dumpall -U postgres | gzip > /var/backups/postgres/dumpall-$(date +\%H\%M).sql.gz'
require="__package/$postgres_server __directory/var/backups/postgres" __cron \
pg_dumpall_archive \
--user postgres \
--hour 2 --minute 0 \
--command 'nice pg_dumpall -U postgres | gzip > /var/backups/postgres/dumpall-$(date +\%Y\%m\%d-\%H\%M).sql.gz'
# Delete local backups after 7 days
require="__package/$postgres_server __directory/var/backups/postgres" __cron \
pg_dump_cleanup \
--user postgres \
--hour 1 --minute 0 \
--command 'find /var/backups/postgres -type f -mtime +7 -delete'
fi