drupal/drupal-setup.sh

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.

michael@529 1 #!@l_bash@
michael@529 2 ##
michael@529 3 ## drupal-setup.sh -- Drupal RDBMS Setup Utility
michael@529 4 ##
michael@529 5
michael@529 6 # command line argument sanity check
michael@529 7 prg="$0"
michael@529 8 if [ $# -eq 0 ]; then
michael@529 9 echo "$prg:ERROR: invalid command line" 1>&2
michael@529 10 echo "$prg:USAGE: $prg install [<database-directory>]" 1>&2
michael@529 11 echo "$prg:USAGE: $prg uninstall" 1>&2
michael@529 12 echo "$prg:USAGE: $prg backup [<dump-file>]" 1>&2
michael@529 13 echo "$prg:USAGE: $prg restore [<dump-file>|<dump-number>]" 1>&2
michael@529 14 echo "$prg:USAGE: $prg edit" 1>&2
michael@529 15 exit 1
michael@529 16 fi
michael@529 17
michael@529 18 # database configuration
michael@529 19 db_dir="@l_prefix@/var/drupal/db"
michael@529 20 db_dump="@l_prefix@/var/drupal/dump"
michael@529 21 db_type="@l_dbtype@"
michael@529 22 db_name="drupal"
michael@529 23 db_user="drupal"
michael@529 24 db_pass="drupal"
michael@529 25
michael@529 26 # determine RDBMS-specific details
michael@529 27 if [ ".$db_type" = .mysql ]; then
michael@529 28 db_sname="mysql"
michael@529 29 db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
michael@529 30 sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
michael@529 31 db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
michael@529 32 sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
michael@529 33 elif [ ".$db_type" = .pgsql ]; then
michael@529 34 db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
michael@529 35 sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
michael@529 36 db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
michael@529 37 sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
michael@529 38 db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
michael@529 39 sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`
michael@529 40 fi
michael@529 41
michael@529 42 # dispatch operation
michael@529 43 cmd="${1:-"install"}"
michael@529 44 shift
michael@529 45 case "$cmd" in
michael@529 46 install )
michael@529 47 ##
michael@529 48 ## create the database
michael@529 49 ##
michael@529 50
michael@529 51 if [ $# -gt 0 ]; then
michael@529 52 db_dir="$1"
michael@529 53 shift
michael@529 54 fi
michael@529 55 if [ ".$db_type" = .mysql ]; then
michael@529 56 # FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
michael@529 57 @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
michael@529 58 ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
michael@529 59 echo "FLUSH PRIVILEGES;"
michael@529 60 ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
michael@529 61 elif [ ".$db_type" = .pgsql ]; then
michael@529 62 ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
michael@529 63 echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
michael@529 64 echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8' TEMPLATE template0;"
michael@529 65 ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
michael@529 66 fi
michael@529 67
michael@529 68 # activate configuration directory
michael@529 69 # (is automatically deactivated by installer afterwards)
michael@529 70 if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
michael@529 71 chmod 777 @l_prefix@/share/drupal/sites/default
michael@529 72 fi
michael@529 73 ;;
michael@529 74
michael@529 75 uninstall )
michael@529 76 ##
michael@529 77 ## remove the database
michael@529 78 ##
michael@529 79
michael@529 80 if [ ".$db_type" = .mysql ]; then
michael@529 81 ( echo "DROP DATABASE $db_name;"
michael@529 82 ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
michael@529 83 elif [ ".$db_type" = .pgsql ]; then
michael@529 84 ( echo "DROP DATABASE $db_name;"
michael@529 85 echo "DROP TABLESPACE $db_name;"
michael@529 86 echo "DROP ROLE $db_user;"
michael@529 87 ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
michael@529 88 fi
michael@529 89
michael@529 90 # remove the generated configuration
michael@529 91 if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
michael@529 92 rm -f @l_prefix@/share/drupal/sites/default/settings.php
michael@529 93 fi
michael@529 94 ;;
michael@529 95
michael@529 96 backup )
michael@529 97 ##
michael@529 98 ## backup the database
michael@529 99 ##
michael@529 100
michael@529 101 # determine dumpfile
michael@529 102 if [ $# -gt 0 ]; then
michael@529 103 # manually managed dumpfile
michael@529 104 dumpfile="$1"
michael@529 105 else
michael@529 106 # automatically managed (rotated) dumpfile
michael@529 107 rm -f $db_dump/drupal-dump-9.sql.gz >/dev/null 2>&1 || true
michael@529 108 i=8
michael@529 109 while [ $i -ge 0 ]; do
michael@529 110 if [ -f $db_dump/drupal-dump-$i.sql.bz2 ]; then
michael@529 111 mv $db_dump/drupal-dump-$i.sql.bz2 $db_dump/drupal-dump-`expr $i + 1`.sql.bz2
michael@529 112 fi
michael@529 113 i=`expr $i - 1`
michael@529 114 done
michael@529 115 dumpfile="$db_dump/drupal-dump-0.sql.bz2"
michael@529 116 fi
michael@529 117
michael@529 118 # dump database content to dumpfile (compressed plain SQL format)
michael@529 119 umask 007
michael@529 120 if [ ".$db_type" = .mysql ]; then
michael@529 121 @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
michael@529 122 @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
michael@529 123 elif [ ".$db_type" = .pgsql ]; then
michael@529 124 PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \
michael@529 125 @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
michael@529 126 fi
michael@529 127 chown @l_rusr@:@l_rgrp@ $dumpfile >/dev/null 2>&1 || true
michael@529 128 chmod 640 $dumpfile >/dev/null 2>&1 || true
michael@529 129 ;;
michael@529 130
michael@529 131 restore )
michael@529 132 ##
michael@529 133 ## restore the database
michael@529 134 ##
michael@529 135
michael@529 136 # determine dumpfile
michael@529 137 if [ $# -gt 0 ]; then
michael@529 138 dumpfile="$1"
michael@529 139 else
michael@529 140 dumpfile="0"
michael@529 141 fi
michael@529 142 case "$dumpfile" in
michael@529 143 [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;;
michael@529 144 esac
michael@529 145 if [ ! -f $dumpfile ]; then
michael@529 146 echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
michael@529 147 exit 1
michael@529 148 fi
michael@529 149
michael@529 150 # optionally stop Drupal
michael@529 151 eval `@l_prefix@/bin/openpkg rc drupal status 2>/dev/null || true`
michael@529 152 if [ ".$drupal_active" = .yes ]; then
michael@529 153 @l_prefix@/bin/openpkg rc drupal stop >/dev/null 2>&1 || true
michael@529 154 fi
michael@529 155
michael@529 156 # drop old and initialize new database
michael@529 157 DRUPAL_SETUP_RESTORE=1
michael@529 158 export DRUPAL_SETUP_RESTORE
michael@529 159 $prg uninstall || exit $?
michael@529 160 $prg install || exit $?
michael@529 161
michael@529 162 # restore database content from dumpfile (compressed plain SQL format)
michael@529 163 if [ ".$db_type" = .mysql ]; then
michael@529 164 @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
michael@529 165 @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null
michael@529 166 elif [ ".$db_type" = .pgsql ]; then
michael@529 167 @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
michael@529 168 PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null
michael@529 169 fi
michael@529 170
michael@529 171 # optionally restart Drupal
michael@529 172 if [ ".$drupal_active" = .yes ]; then
michael@529 173 @l_prefix@/bin/openpkg rc drupal start >/dev/null 2>&1 || true
michael@529 174 fi
michael@529 175 ;;
michael@529 176
michael@529 177 edit )
michael@529 178 ##
michael@529 179 ## interactively edit the database
michael@529 180 ##
michael@529 181
michael@529 182 if [ ".$db_type" = .mysql ]; then
michael@529 183 @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name"
michael@529 184 elif [ ".$db_type" = .pgsql ]; then
michael@529 185 PGPASSWORD="$db_spass" @l_prefix@/bin/psql -U "$db_suser" -d "$db_name"
michael@529 186 fi
michael@529 187 ;;
michael@529 188 esac
michael@529 189

mercurial