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