michael@300: #!/bin/sh
michael@300: ##
michael@300: ## pg_migrate -- PostgreSQL Database Migration Utility
michael@300: ## Copyright (c) 2000-2007 OpenPKG Foundation e.V.
michael@300: ## Copyright (c) 2000-2007 Ralf S. Engelschall
michael@300: ##
michael@300: ## Permission to use, copy, modify, and distribute this software for
michael@300: ## any purpose with or without fee is hereby granted, provided that
michael@300: ## the above copyright notice and this permission notice appear in all
michael@300: ## copies.
michael@300: ##
michael@300: ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
michael@300: ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
michael@300: ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@300: ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
michael@300: ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@300: ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@300: ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
michael@300: ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@300: ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
michael@300: ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
michael@300: ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@300: ## SUCH DAMAGE.
michael@300: ##
michael@300:
michael@300: # configuration
michael@300: l_prefix="@l_prefix@"
michael@300: l_rusr="@l_rusr@"
michael@300: l_rgrp="@l_rgrp@"
michael@300:
michael@300: # load superuser information
michael@300: l_pgdata=""
michael@300: l_pguser=""
michael@300: l_pgpass=""
michael@300: if [ ! -r $l_prefix/var/postgresql/db/pg_superuser.conf ]; then
michael@300: echo "$0:ERROR: unable to read file \"$l_prefix/var/postgresql/db/pg_superuser.conf\" (use pg_passwd(1) to create)" 1>&2
michael@300: exit 1
michael@300: fi
michael@300: eval `. $l_prefix/var/postgresql/db/pg_superuser.conf; \
michael@300: echo l_pgdata=\"$superuser_database\"; \
michael@300: echo l_pguser=\"$superuser_username\"; \
michael@300: echo l_pgpass=\"$superuser_password\"`
michael@300:
michael@300: # establish sane environment
michael@300: LC_CTYPE=C
michael@300: export LC_CTYPE
michael@300: umask 022
michael@300:
michael@300: # check command line
michael@300: if [ $# -ne 1 -a $# -ne 2 ]; then
michael@300: echo "$0:ERROR: invalid command line" 1>&2
michael@300: echo "$0:USAGE: $0 dump|restore []" 1>&2
michael@300: exit 1
michael@300: fi
michael@300: cmd="$1"; shift
michael@300: if [ $# -eq 1 ]; then
michael@300: l_pgpass="$1"; shift
michael@300: fi
michael@300:
michael@300: # dispatch into commands
michael@300: case $cmd in
michael@300: dump )
michael@300: echo "++ enforcing full-superuser access policy"
michael@300: cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
michael@300: $l_prefix/var/postgresql/db/pg_hba.conf.orig
michael@300: ( echo "local all all trust"
michael@300: echo "host all all 127.0.0.1/32 trust"
michael@300: ) >$l_prefix/var/postgresql/db/pg_hba.conf
michael@300:
michael@300: ( eval `${l_prefix}/bin/openpkg rc postgresql status 2>/dev/null`
michael@300: echo "postgresql_active=\"$postgresql_active\""
michael@300: ) 2>/dev/null
michael@300: if [ ".$postgresql_active" = .yes ]; then
michael@300: echo "++ reloading already running database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql reload
michael@300: sleep 2
michael@300: epilog=reload
michael@300: else
michael@300: echo "++ temporarily starting database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql start
michael@300: sleep 4
michael@300: epilog=stop
michael@300: fi
michael@300:
michael@300: echo "++ rotating dump files $l_prefix/var/postgresql/db.dump*.sql.bz2"
michael@300: i=9
michael@300: rm -f $l_prefix/var/postgresql/db.dump.$i.sql.bz2 >/dev/null 2>&1 || true
michael@300: while [ $i -gt 0 ]; do
michael@300: j=$i
michael@300: i=`expr $i - 1`
michael@300: if [ $i -eq 0 ]; then
michael@300: prev="$l_prefix/var/postgresql/db.dump.sql.bz2"
michael@300: next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
michael@300: else
michael@300: prev="$l_prefix/var/postgresql/db.dump.$i.sql.bz2"
michael@300: next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
michael@300: fi
michael@300: if [ -f $prev ]; then
michael@300: mv $prev $next
michael@300: fi
michael@300: done
michael@300:
michael@300: echo "++ dumping all databases into $l_prefix/var/postgresql/db.dump.sql.bz2"
michael@300: PGPASSWORD="$l_pgpass" \
michael@300: $l_prefix/bin/pg_dumpall \
michael@300: -U "$l_pguser" -o |\
michael@300: $l_prefix/lib/openpkg/bzip2 -9 \
michael@300: >$l_prefix/var/postgresql/db.dump.sql.bz2
michael@300: chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db.dump.sql.bz2
michael@300: chmod 700 $l_prefix/var/postgresql/db.dump.sql.bz2
michael@300:
michael@300: echo "++ restoring original access policy"
michael@300: cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
michael@300: $l_prefix/var/postgresql/db/pg_hba.conf
michael@300: rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
michael@300:
michael@300: if [ ".$epilog" = .reload ]; then
michael@300: echo "++ reloading already running database engine (again)"
michael@300: $l_prefix/bin/openpkg rc postgresql reload
michael@300: sleep 2
michael@300: else
michael@300: echo "++ stopping temporarily started database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql stop
michael@300: sleep 4
michael@300: fi
michael@300: ;;
michael@300:
michael@300: restore )
michael@300: if [ ".`$l_prefix/bin/openpkg rc postgresql status 2>&1 | grep 'is running'`" != . ]; then
michael@300: echo "++ stopping already running database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql stop
michael@300: sleep 2
michael@300: epilog=start
michael@300: else
michael@300: epilog=none
michael@300: fi
michael@300:
michael@300: echo "++ rotating database directories $l_prefix/var/postgresql/db.old*/"
michael@300: i=9
michael@300: rm -rf $l_prefix/var/postgresql/db.old.$i >/dev/null 2>&1 || true
michael@300: while [ $i -gt 0 ]; do
michael@300: j=$i
michael@300: i=`expr $i - 1`
michael@300: if [ $i -eq 0 ]; then
michael@300: prev="$l_prefix/var/postgresql/db"
michael@300: next="$l_prefix/var/postgresql/db.old.$j"
michael@300: else
michael@300: prev="$l_prefix/var/postgresql/db.old.$i"
michael@300: next="$l_prefix/var/postgresql/db.old.$j"
michael@300: fi
michael@300: if [ -d $prev ]; then
michael@300: mv $prev $next
michael@300: fi
michael@300: done
michael@300:
michael@300: echo "++ creating new database directory $l_prefix/var/postgresql/db/"
michael@300: mkdir $l_prefix/var/postgresql/db
michael@300: chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db
michael@300: chmod 700 $l_prefix/var/postgresql/db
michael@300:
michael@300: su - ${l_rusr} -c \
michael@300: "LC_CTYPE=C; export LC_CTYPE; umask 022; \
michael@300: echo $l_pgpass >$l_prefix/var/postgresql/run/pw; \
michael@300: $l_prefix/bin/pg_initdb \
michael@300: -U $l_pguser --pwfile=$l_prefix/var/postgresql/run/pw \
michael@300: -D $l_prefix/var/postgresql/db; \
michael@300: rm -f $l_prefix/var/postgresql/run/pw" 2>&1 |\
michael@300: $l_prefix/lib/openpkg/shtool prop \
michael@300: -p "++ creating new database data"
michael@300:
michael@300: echo "++ restoring database configurations"
michael@300: for conf in pg_hba.conf pg_ident.conf postgresql.conf; do
michael@300: cp -p $l_prefix/var/postgresql/db.old.1/$conf \
michael@300: $l_prefix/var/postgresql/db/
michael@300: done
michael@300:
michael@300: echo "++ enforcing full-superuser access policy"
michael@300: cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
michael@300: $l_prefix/var/postgresql/db/pg_hba.conf.orig
michael@300: ( echo "local all all trust"
michael@300: echo "host all all 127.0.0.1/32 trust"
michael@300: ) >$l_prefix/var/postgresql/db/pg_hba.conf
michael@300:
michael@300: if [ ".$epilog" = .start ]; then
michael@300: echo "++ starting database engine"
michael@300: else
michael@300: echo "++ temporarily starting database engine"
michael@300: fi
michael@300: $l_prefix/bin/openpkg rc postgresql start
michael@300: sleep 4
michael@300:
michael@300: echo "++ restoring all databases from $l_prefix/var/postgresql/db.dump.sql.bz2"
michael@300: $l_prefix/lib/openpkg/bzip2 -c -d \
michael@300: $l_prefix/var/postgresql/db.dump.sql.bz2 |\
michael@300: $l_prefix/bin/psql -U "$l_pguser" -d "$l_pgdata" 2>&1 |\
michael@300: tee $l_prefix/var/postgresql/db.log |\
michael@300: $l_prefix/lib/openpkg/shtool prop \
michael@300: -p "++ restoring data (see $l_prefix/var/postgresql/db.log)"
michael@300:
michael@300: echo "++ restoring original access policy"
michael@300: cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
michael@300: $l_prefix/var/postgresql/db/pg_hba.conf
michael@300: rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
michael@300:
michael@300: if [ ".$epilog" = .start ]; then
michael@300: echo "++ reloading already running database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql reload
michael@300: sleep 2
michael@300: else
michael@300: echo "++ stopping temporarily started database engine"
michael@300: $l_prefix/bin/openpkg rc postgresql stop
michael@300: sleep 4
michael@300: fi
michael@300: ;;
michael@300: * )
michael@300: echo "$0:ERROR: unknown command \"$cmd\"" 1>&2
michael@300: exit 1
michael@300: ;;
michael@300: esac
michael@300: