postgresql/pg_migrate

Mon, 28 Jan 2013 17:37:18 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Mon, 28 Jan 2013 17:37:18 +0100
changeset 758
a2c6460cfb16
permissions
-rw-r--r--

Correct socket error reporting improvement with IPv6 portable code,
after helpful recommendation by Saúl Ibarra Corretgé on OSips devlist.

     1 #!/bin/sh
     2 ##
     3 ##  pg_migrate -- PostgreSQL Database Migration Utility
     4 ##  Copyright (c) 2000-2007 OpenPKG Foundation e.V. <http://openpkg.net/>
     5 ##  Copyright (c) 2000-2007 Ralf S. Engelschall <http://engelschall.com/>
     6 ##
     7 ##  Permission to use, copy, modify, and distribute this software for
     8 ##  any purpose with or without fee is hereby granted, provided that
     9 ##  the above copyright notice and this permission notice appear in all
    10 ##  copies.
    11 ##
    12 ##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    13 ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    14 ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    15 ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    16 ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    17 ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    18 ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    19 ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    20 ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    21 ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    22 ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    23 ##  SUCH DAMAGE.
    24 ##
    26 #   configuration
    27 l_prefix="@l_prefix@"
    28 l_rusr="@l_rusr@"
    29 l_rgrp="@l_rgrp@"
    31 #   load superuser information
    32 l_pgdata=""
    33 l_pguser=""
    34 l_pgpass=""
    35 if [ ! -r $l_prefix/var/postgresql/db/pg_superuser.conf ]; then
    36     echo "$0:ERROR: unable to read file \"$l_prefix/var/postgresql/db/pg_superuser.conf\" (use pg_passwd(1) to create)" 1>&2
    37     exit 1
    38 fi
    39 eval `. $l_prefix/var/postgresql/db/pg_superuser.conf; \
    40       echo l_pgdata=\"$superuser_database\"; \
    41       echo l_pguser=\"$superuser_username\"; \
    42       echo l_pgpass=\"$superuser_password\"`
    44 #   establish sane environment
    45 LC_CTYPE=C
    46 export LC_CTYPE
    47 umask 022
    49 #   check command line
    50 if [ $# -ne 1 -a $# -ne 2 ]; then
    51     echo "$0:ERROR: invalid command line" 1>&2
    52     echo "$0:USAGE: $0 dump|restore [<password>]" 1>&2
    53     exit 1
    54 fi
    55 cmd="$1"; shift
    56 if [ $# -eq 1 ]; then
    57     l_pgpass="$1"; shift
    58 fi
    60 #   dispatch into commands
    61 case $cmd in
    62     dump )
    63         echo "++ enforcing full-superuser access policy"
    64         cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
    65               $l_prefix/var/postgresql/db/pg_hba.conf.orig
    66         ( echo "local all all trust"
    67           echo "host all all 127.0.0.1/32 trust"
    68         ) >$l_prefix/var/postgresql/db/pg_hba.conf
    70         ( eval `${l_prefix}/bin/openpkg rc postgresql status 2>/dev/null`
    71           echo "postgresql_active=\"$postgresql_active\""
    72         ) 2>/dev/null
    73         if [ ".$postgresql_active" = .yes ]; then
    74             echo "++ reloading already running database engine"
    75             $l_prefix/bin/openpkg rc postgresql reload
    76             sleep 2
    77             epilog=reload
    78         else
    79             echo "++ temporarily starting database engine"
    80             $l_prefix/bin/openpkg rc postgresql start 
    81             sleep 4
    82             epilog=stop
    83         fi
    85         echo "++ rotating dump files $l_prefix/var/postgresql/db.dump*.sql.bz2"
    86         i=9
    87         rm -f $l_prefix/var/postgresql/db.dump.$i.sql.bz2 >/dev/null 2>&1 || true
    88         while [ $i -gt 0 ]; do
    89             j=$i
    90             i=`expr $i - 1`
    91             if [ $i -eq 0 ]; then
    92                 prev="$l_prefix/var/postgresql/db.dump.sql.bz2"
    93                 next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
    94             else
    95                 prev="$l_prefix/var/postgresql/db.dump.$i.sql.bz2"
    96                 next="$l_prefix/var/postgresql/db.dump.$j.sql.bz2"
    97             fi
    98             if [ -f $prev ]; then
    99                 mv $prev $next
   100             fi
   101         done
   103         echo "++ dumping all databases into $l_prefix/var/postgresql/db.dump.sql.bz2"
   104         PGPASSWORD="$l_pgpass" \
   105         $l_prefix/bin/pg_dumpall \
   106             -U "$l_pguser" -o |\
   107             $l_prefix/lib/openpkg/bzip2 -9 \
   108                 >$l_prefix/var/postgresql/db.dump.sql.bz2
   109         chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db.dump.sql.bz2
   110         chmod 700 $l_prefix/var/postgresql/db.dump.sql.bz2
   112         echo "++ restoring original access policy"
   113         cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
   114               $l_prefix/var/postgresql/db/pg_hba.conf
   115         rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
   117         if [ ".$epilog" = .reload ]; then
   118             echo "++ reloading already running database engine (again)"
   119             $l_prefix/bin/openpkg rc postgresql reload
   120             sleep 2
   121         else
   122             echo "++ stopping temporarily started database engine"
   123             $l_prefix/bin/openpkg rc postgresql stop 
   124             sleep 4
   125         fi
   126         ;;
   128     restore )
   129         if [ ".`$l_prefix/bin/openpkg rc postgresql status 2>&1 | grep 'is running'`" != . ]; then
   130             echo "++ stopping already running database engine"
   131             $l_prefix/bin/openpkg rc postgresql stop
   132             sleep 2
   133             epilog=start
   134         else
   135             epilog=none
   136         fi
   138         echo "++ rotating database directories $l_prefix/var/postgresql/db.old*/"
   139         i=9
   140         rm -rf $l_prefix/var/postgresql/db.old.$i >/dev/null 2>&1 || true
   141         while [ $i -gt 0 ]; do
   142             j=$i
   143             i=`expr $i - 1`
   144             if [ $i -eq 0 ]; then
   145                 prev="$l_prefix/var/postgresql/db"
   146                 next="$l_prefix/var/postgresql/db.old.$j"
   147             else
   148                 prev="$l_prefix/var/postgresql/db.old.$i"
   149                 next="$l_prefix/var/postgresql/db.old.$j"
   150             fi
   151             if [ -d $prev ]; then
   152                 mv $prev $next
   153             fi
   154         done
   156         echo "++ creating new database directory $l_prefix/var/postgresql/db/"
   157         mkdir $l_prefix/var/postgresql/db
   158         chown ${l_rusr}:${l_rgrp} $l_prefix/var/postgresql/db
   159         chmod 700 $l_prefix/var/postgresql/db
   161         su - ${l_rusr} -c \
   162             "LC_CTYPE=C; export LC_CTYPE; umask 022; \
   163             echo $l_pgpass >$l_prefix/var/postgresql/run/pw; \
   164             $l_prefix/bin/pg_initdb \
   165                 -U $l_pguser --pwfile=$l_prefix/var/postgresql/run/pw \
   166                 -D $l_prefix/var/postgresql/db; \
   167             rm -f $l_prefix/var/postgresql/run/pw" 2>&1 |\
   168         $l_prefix/lib/openpkg/shtool prop \
   169             -p "++ creating new database data"
   171         echo "++ restoring database configurations"
   172         for conf in pg_hba.conf pg_ident.conf postgresql.conf; do
   173             cp -p $l_prefix/var/postgresql/db.old.1/$conf \
   174                   $l_prefix/var/postgresql/db/
   175         done
   177         echo "++ enforcing full-superuser access policy"
   178         cp -p $l_prefix/var/postgresql/db/pg_hba.conf \
   179               $l_prefix/var/postgresql/db/pg_hba.conf.orig
   180         ( echo "local all all trust"
   181           echo "host all all 127.0.0.1/32 trust"
   182         ) >$l_prefix/var/postgresql/db/pg_hba.conf
   184         if [ ".$epilog" = .start ]; then
   185             echo "++ starting database engine"
   186         else
   187             echo "++ temporarily starting database engine"
   188         fi
   189         $l_prefix/bin/openpkg rc postgresql start
   190         sleep 4
   192         echo "++ restoring all databases from $l_prefix/var/postgresql/db.dump.sql.bz2"
   193         $l_prefix/lib/openpkg/bzip2 -c -d \
   194             $l_prefix/var/postgresql/db.dump.sql.bz2 |\
   195                 $l_prefix/bin/psql -U "$l_pguser" -d "$l_pgdata" 2>&1 |\
   196                     tee $l_prefix/var/postgresql/db.log |\
   197                         $l_prefix/lib/openpkg/shtool prop \
   198                             -p "++ restoring data (see $l_prefix/var/postgresql/db.log)"
   200         echo "++ restoring original access policy"
   201         cp -p $l_prefix/var/postgresql/db/pg_hba.conf.orig \
   202               $l_prefix/var/postgresql/db/pg_hba.conf
   203         rm -f $l_prefix/var/postgresql/db/pg_hba.conf.orig
   205         if [ ".$epilog" = .start ]; then
   206             echo "++ reloading already running database engine"
   207             $l_prefix/bin/openpkg rc postgresql reload
   208             sleep 2
   209         else
   210             echo "++ stopping temporarily started database engine"
   211             $l_prefix/bin/openpkg rc postgresql stop 
   212             sleep 4
   213         fi
   214         ;;
   215     * )
   216         echo "$0:ERROR: unknown command \"$cmd\"" 1>&2
   217         exit 1
   218         ;;
   219 esac

mercurial