From df890a902fe35761c0580b2ed68be6640fbf1406 Mon Sep 17 00:00:00 2001 From: Sameer Naik Date: Wed, 4 Feb 2015 14:41:13 +0530 Subject: [PATCH] start: automatically migrate postgresql data on upgrades --- Changelog.md | 1 + start | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/Changelog.md b/Changelog.md index 25d5b5d..d443fa0 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ **latest** - use the official postgresql apt repo +- feature: automatic data migration on upgrade **9.1-1** - upgrade to sameersbn/ubuntu:20141001, fixes shellshock diff --git a/start b/start index 7087036..5704a59 100755 --- a/start +++ b/start @@ -36,6 +36,9 @@ cd ${PG_HOME} # initialize PostgreSQL data directory if [ ! -d ${PG_DATADIR} ]; then + # check if we need to perform data migration + PG_OLD_VERSION=$(find ${PG_HOME}/[0-9].[0-9]/main -maxdepth 1 -name PG_VERSION | sort -r | head -n1 | cut -d'/' -f5) + if [ ! -f "${PG_HOME}/pwfile" ]; then PG_PASSWORD=$(pwgen -c -n -1 14) echo "${PG_PASSWORD}" > ${PG_HOME}/pwfile @@ -47,6 +50,31 @@ if [ ! -d ${PG_DATADIR} ]; then --username=postgres --encoding=unicode --auth=trust >/dev/null fi +if [ -n "${PG_OLD_VERSION}" ]; then + echo "Migrating postgresql ${PG_OLD_VERSION} data..." + PG_OLD_CONFDIR="/etc/postgresql/${PG_OLD_VERSION}/main" + PG_OLD_BINDIR="/usr/lib/postgresql/${PG_OLD_VERSION}/bin" + PG_OLD_DATADIR="${PG_HOME}/${PG_OLD_VERSION}/main" + + # backup ${PG_OLD_DATADIR} to avoid data loss + PG_BKP_SUFFIX=$(date +%Y%m%d%H%M%S) + echo "Backing up ${PG_OLD_DATADIR} to ${PG_OLD_DATADIR}.${PG_BKP_SUFFIX}..." + cp -a ${PG_OLD_DATADIR} ${PG_OLD_DATADIR}.${PG_BKP_SUFFIX} + + echo "Installing postgresql-${PG_OLD_VERSION}..." + apt-get update + apt-get install postgresql-${PG_OLD_VERSION} postgresql-client-${PG_OLD_VERSION} + rm -rf /var/lib/apt/lists/* + + # migrate ${PG_OLD_VERSION} data + echo "Migration in progress. This could take a while, please be patient..." + sudo -u postgres -H ${PG_BINDIR}/pg_upgrade \ + -b ${PG_OLD_BINDIR} -B ${PG_BINDIR} \ + -d ${PG_OLD_DATADIR} -D ${PG_DATADIR} \ + -o "-c config_file=${PG_OLD_CONFDIR}/postgresql.conf" \ + -O "-c config_file=${PG_CONFDIR}/postgresql.conf" >/dev/null +fi + if [ -f ${PG_HOME}/pwfile ]; then PG_PASSWORD=$(cat ${PG_HOME}/pwfile) echo "|------------------------------------------------------------------|"