2014-01-09 13:06:25 +00:00
|
|
|
#!/bin/bash
|
2011-07-27 23:32:24 +00:00
|
|
|
#
|
2013-12-13 08:36:08 +00:00
|
|
|
# postgresql-setup - Initialization and upgrade operations for PostgreSQL
|
2011-07-27 23:32:24 +00:00
|
|
|
|
2014-05-14 11:33:10 +00:00
|
|
|
test -z "$PATH" && export PATH="/sbin:/usr/sbin:/bin:/usr/bin"
|
|
|
|
|
2014-01-09 19:16:54 +00:00
|
|
|
test x"$PGSETUP_DEBUG" != x && set -x
|
|
|
|
|
2011-07-27 23:32:24 +00:00
|
|
|
# PGVERSION is the full package version, e.g., 9.0.2
|
|
|
|
# Note: the specfile inserts the correct value during package build
|
|
|
|
PGVERSION=xxxx
|
2013-12-13 08:36:08 +00:00
|
|
|
|
2014-01-23 09:56:07 +00:00
|
|
|
# PGMAJORVERSION is the major version, e.g. 9.0
|
|
|
|
PGMAJORVERSION=xxxx
|
|
|
|
|
2011-07-27 23:32:24 +00:00
|
|
|
# PGENGINE is the directory containing the postmaster executable
|
|
|
|
PGENGINE=xxxx
|
2013-12-13 08:36:08 +00:00
|
|
|
|
2011-07-27 23:32:24 +00:00
|
|
|
# PREVMAJORVERSION is the previous major version, e.g., 8.4, for upgrades
|
|
|
|
PREVMAJORVERSION=xxxx
|
2013-12-13 08:36:08 +00:00
|
|
|
|
2011-07-27 23:32:24 +00:00
|
|
|
# PREVPGENGINE is the directory containing the previous postmaster executable
|
|
|
|
PREVPGENGINE=xxxx
|
|
|
|
|
|
|
|
# Absorb configuration settings from the specified systemd service file,
|
|
|
|
# or the default "postgresql" service if not specified
|
|
|
|
SERVICE_NAME="$2"
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ x"$SERVICE_NAME" = x ]; then
|
2011-07-27 23:32:24 +00:00
|
|
|
SERVICE_NAME=postgresql
|
|
|
|
fi
|
|
|
|
|
2014-01-09 19:16:54 +00:00
|
|
|
# Pathname of the RPM distribution README
|
|
|
|
README_RPM_DIST=xxxx
|
|
|
|
|
|
|
|
USAGE_STRING=$"
|
|
|
|
Usage: $0 {initdb|upgrade} [SERVICE_NAME]
|
|
|
|
|
|
|
|
Script is aimed to help sysadmin with basic database cluster administration.
|
|
|
|
|
|
|
|
The SERVICE_NAME is used for selection of proper unit configuration file; For
|
|
|
|
more info and howto/when use this script please look at the docu file
|
|
|
|
$README_RPM_DIST. The 'postgresql'
|
|
|
|
string is used when no SERVICE_NAME is explicitly passed.
|
|
|
|
|
|
|
|
Available operation mode:
|
|
|
|
initdb Create a new PostgreSQL database cluster. This is usually the
|
|
|
|
first action you perform after PostgreSQL server installation.
|
|
|
|
upgrade Upgrade PostgreSQL database cluster to be usable with new
|
2014-01-23 09:56:07 +00:00
|
|
|
server. Use this if you upgraded your PostgreSQL server to
|
|
|
|
newer major version (currently from $PREVMAJORVERSION \
|
|
|
|
to $PGMAJORVERSION).
|
2014-01-09 19:16:54 +00:00
|
|
|
|
|
|
|
Environment:
|
2014-01-21 14:41:35 +00:00
|
|
|
PGSETUP_INITDB_OPTIONS Options carried by this variable are passed to
|
|
|
|
subsequent call of \`initdb\` binary (see man
|
|
|
|
initdb(1)). This variable is used also during
|
|
|
|
'upgrade' mode because the new cluster is actually
|
|
|
|
re-initialized from the old one.
|
|
|
|
PGSETUP_PGUPGRADE_OPTIONS Options in this variable are passed next to the
|
|
|
|
subsequent call of \`pg_upgrade\`. For more info
|
|
|
|
about possible options please look at man
|
|
|
|
pg_upgrade(1).
|
|
|
|
PGSETUP_DEBUG Set to '1' if you want to see debugging output."
|
2014-01-09 19:16:54 +00:00
|
|
|
|
2014-01-20 13:11:11 +00:00
|
|
|
# note that these options are useful at least for help2man processing
|
|
|
|
case "$1" in
|
|
|
|
--version)
|
|
|
|
echo "postgresql-setup $PGVERSION"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--help|--usage)
|
|
|
|
echo "$USAGE_STRING"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2012-03-17 16:47:37 +00:00
|
|
|
# this parsing technique fails for PGDATA pathnames containing spaces,
|
|
|
|
# but there's not much I can do about it given systemctl's output format...
|
|
|
|
PGDATA=`systemctl show -p Environment "${SERVICE_NAME}.service" |
|
2013-12-13 08:36:08 +00:00
|
|
|
sed 's/^Environment=//' | tr ' ' '\n' |
|
|
|
|
sed -n 's/^PGDATA=//p' | tail -n 1`
|
|
|
|
if [ x"$PGDATA" = x ]; then
|
2012-03-17 16:47:37 +00:00
|
|
|
echo "failed to find PGDATA setting in ${SERVICE_NAME}.service"
|
2011-07-27 23:32:24 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-03-17 16:47:37 +00:00
|
|
|
PGPORT=`systemctl show -p Environment "${SERVICE_NAME}.service" |
|
2013-12-13 08:36:08 +00:00
|
|
|
sed 's/^Environment=//' | tr ' ' '\n' |
|
|
|
|
sed -n 's/^PGPORT=//p' | tail -n 1`
|
|
|
|
if [ x"$PGPORT" = x ]; then
|
2012-03-17 16:47:37 +00:00
|
|
|
echo "failed to find PGPORT setting in ${SERVICE_NAME}.service"
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-07-27 23:32:24 +00:00
|
|
|
|
|
|
|
# Log file for initdb
|
|
|
|
PGLOG=/var/lib/pgsql/initdb.log
|
|
|
|
|
|
|
|
# Log file for pg_upgrade
|
|
|
|
PGUPLOG=/var/lib/pgsql/pgupgrade.log
|
|
|
|
|
|
|
|
export PGDATA
|
2012-03-17 16:47:37 +00:00
|
|
|
export PGPORT
|
2011-07-27 23:32:24 +00:00
|
|
|
|
|
|
|
# For SELinux we need to use 'runuser' not 'su'
|
2014-05-14 11:33:10 +00:00
|
|
|
if command -v runuser &>/dev/null; then
|
2011-07-27 23:32:24 +00:00
|
|
|
SU=runuser
|
|
|
|
else
|
|
|
|
SU=su
|
|
|
|
fi
|
|
|
|
|
|
|
|
script_result=0
|
|
|
|
|
|
|
|
# code shared between initdb and upgrade actions
|
|
|
|
perform_initdb(){
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ ! -e "$PGDATA" ]; then
|
|
|
|
mkdir "$PGDATA" || return 1
|
|
|
|
chown postgres:postgres "$PGDATA"
|
|
|
|
chmod go-rwx "$PGDATA"
|
|
|
|
fi
|
|
|
|
# Clean up SELinux tagging for PGDATA
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA"
|
|
|
|
|
|
|
|
# Create the initdb log file if needed
|
|
|
|
if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]; then
|
|
|
|
touch "$PGLOG" || return 1
|
|
|
|
chown postgres:postgres "$PGLOG"
|
|
|
|
chmod go-rwx "$PGLOG"
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Initialize the database
|
2014-01-09 08:45:02 +00:00
|
|
|
initdbcmd="$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident'"
|
|
|
|
initdbcmd+=" $PGSETUP_INITDB_OPTIONS"
|
|
|
|
|
|
|
|
$SU -l postgres -c "$initdbcmd" >> "$PGLOG" 2>&1 < /dev/null
|
2013-12-13 08:36:08 +00:00
|
|
|
|
|
|
|
# Create directory for postmaster log files
|
|
|
|
mkdir "$PGDATA/pg_log"
|
|
|
|
chown postgres:postgres "$PGDATA/pg_log"
|
|
|
|
chmod go-rwx "$PGDATA/pg_log"
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA/pg_log"
|
|
|
|
|
|
|
|
if [ -f "$PGDATA/PG_VERSION" ]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
2011-07-27 23:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initdb(){
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ -f "$PGDATA/PG_VERSION" ]; then
|
|
|
|
echo $"Data directory is not empty!"
|
|
|
|
echo
|
|
|
|
script_result=1
|
2011-07-27 23:32:24 +00:00
|
|
|
else
|
2013-12-13 08:36:08 +00:00
|
|
|
echo -n $"Initializing database ... "
|
|
|
|
if perform_initdb; then
|
|
|
|
echo $"OK"
|
|
|
|
else
|
|
|
|
echo $"failed, see $PGLOG"
|
|
|
|
script_result=1
|
|
|
|
fi
|
|
|
|
echo
|
2011-07-27 23:32:24 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
upgrade(){
|
|
|
|
# must see previous version in PG_VERSION
|
|
|
|
if [ ! -f "$PGDATA/PG_VERSION" -o \
|
2013-12-13 08:36:08 +00:00
|
|
|
x`cat "$PGDATA/PG_VERSION"` != x"$PREVMAJORVERSION" ]
|
2011-07-27 23:32:24 +00:00
|
|
|
then
|
2013-12-13 08:36:08 +00:00
|
|
|
echo
|
|
|
|
echo $"Cannot upgrade because the database in $PGDATA is not of"
|
|
|
|
echo $"compatible previous version $PREVMAJORVERSION."
|
|
|
|
echo
|
|
|
|
exit 1
|
2011-07-27 23:32:24 +00:00
|
|
|
fi
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ ! -x "$PGENGINE/pg_upgrade" ]; then
|
|
|
|
echo
|
|
|
|
echo $"Please install the postgresql-upgrade RPM."
|
|
|
|
echo
|
|
|
|
exit 5
|
2011-07-27 23:32:24 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Set up log file for pg_upgrade
|
|
|
|
rm -f "$PGUPLOG"
|
|
|
|
touch "$PGUPLOG" || exit 1
|
|
|
|
chown postgres:postgres "$PGUPLOG"
|
|
|
|
chmod go-rwx "$PGUPLOG"
|
|
|
|
[ -x /sbin/restorecon ] && /sbin/restorecon "$PGUPLOG"
|
|
|
|
|
|
|
|
# Move old DB to PGDATAOLD
|
|
|
|
PGDATAOLD="${PGDATA}-old"
|
|
|
|
rm -rf "$PGDATAOLD"
|
|
|
|
mv "$PGDATA" "$PGDATAOLD" || exit 1
|
|
|
|
|
2013-12-13 08:12:08 +00:00
|
|
|
# Create configuration file for upgrade process
|
|
|
|
HBA_CONF_BACKUP="$PGDATAOLD/pg_hba.conf.postgresql-setup.`date +%s`"
|
|
|
|
HBA_CONF_BACKUP_EXISTS=0
|
|
|
|
|
|
|
|
if [ ! -f $HBA_CONF_BACKUP ]; then
|
|
|
|
mv "$PGDATAOLD/pg_hba.conf" "$HBA_CONF_BACKUP"
|
|
|
|
HBA_CONF_BACKUP_EXISTS=1
|
|
|
|
|
|
|
|
# For fluent upgrade 'postgres' user should be able to connect
|
|
|
|
# to any database without password. Temporarily, no other type
|
|
|
|
# of connection is needed.
|
2013-12-13 08:36:08 +00:00
|
|
|
echo "local all postgres ident" > "$PGDATAOLD/pg_hba.conf"
|
2013-12-13 08:12:08 +00:00
|
|
|
fi
|
|
|
|
|
2011-07-27 23:32:24 +00:00
|
|
|
echo -n $"Upgrading database: "
|
|
|
|
|
|
|
|
# Create empty new-format database
|
2013-12-13 08:36:08 +00:00
|
|
|
if perform_initdb; then
|
|
|
|
# Do the upgrade
|
|
|
|
$SU -l postgres -c "$PGENGINE/pg_upgrade \
|
|
|
|
'--old-bindir=$PREVPGENGINE' \
|
|
|
|
'--new-bindir=$PGENGINE' \
|
|
|
|
'--old-datadir=$PGDATAOLD' \
|
|
|
|
'--new-datadir=$PGDATA' \
|
|
|
|
--link \
|
|
|
|
'--old-port=$PGPORT' '--new-port=$PGPORT' \
|
2014-01-21 14:41:35 +00:00
|
|
|
--user=postgres \
|
|
|
|
$PGSETUP_PGUPGRADE_OPTIONS" \
|
|
|
|
>> "$PGUPLOG" 2>&1 < /dev/null
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
# pg_upgrade failed
|
|
|
|
script_result=1
|
|
|
|
fi
|
2011-07-27 23:32:24 +00:00
|
|
|
else
|
2013-12-13 08:36:08 +00:00
|
|
|
# initdb failed
|
|
|
|
script_result=1
|
2011-07-27 23:32:24 +00:00
|
|
|
fi
|
|
|
|
|
2013-12-13 08:36:08 +00:00
|
|
|
# Move back the backed-up pg_hba.conf regardless of the script_result.
|
2013-12-13 08:12:08 +00:00
|
|
|
if [ x$HBA_CONF_BACKUP_EXISTS = x1 ]; then
|
|
|
|
mv -f "$HBA_CONF_BACKUP" "$PGDATAOLD/pg_hba.conf"
|
|
|
|
fi
|
|
|
|
|
2013-12-13 08:36:08 +00:00
|
|
|
if [ $script_result -eq 0 ]; then
|
2013-12-13 08:12:08 +00:00
|
|
|
echo $"OK"
|
|
|
|
echo
|
2014-01-23 09:56:07 +00:00
|
|
|
echo $"The configuration files were replaced by default configuration."
|
2013-12-13 08:12:08 +00:00
|
|
|
echo $"The previous configuration and data are stored in folder"
|
|
|
|
echo $PGDATAOLD.
|
2011-07-27 23:32:24 +00:00
|
|
|
else
|
2013-12-13 08:36:08 +00:00
|
|
|
# Clean up after failure
|
|
|
|
rm -rf "$PGDATA"
|
|
|
|
mv "$PGDATAOLD" "$PGDATA"
|
|
|
|
echo $"failed"
|
2011-07-27 23:32:24 +00:00
|
|
|
fi
|
|
|
|
echo
|
|
|
|
echo $"See $PGUPLOG for details."
|
|
|
|
}
|
|
|
|
|
|
|
|
# See how we were called.
|
|
|
|
case "$1" in
|
2013-12-13 08:36:08 +00:00
|
|
|
initdb)
|
|
|
|
initdb
|
|
|
|
;;
|
|
|
|
upgrade)
|
|
|
|
upgrade
|
|
|
|
;;
|
|
|
|
*)
|
2014-01-20 13:11:11 +00:00
|
|
|
echo >&2 "$USAGE_STRING"
|
2013-12-13 08:36:08 +00:00
|
|
|
exit 2
|
2011-07-27 23:32:24 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit $script_result
|