drupal/drupal-setup.sh

changeset 529
7d4d11d301d6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drupal/drupal-setup.sh	Tue Aug 28 18:28:45 2012 +0200
     1.3 @@ -0,0 +1,189 @@
     1.4 +#!@l_bash@
     1.5 +##
     1.6 +##  drupal-setup.sh -- Drupal RDBMS Setup Utility
     1.7 +##
     1.8 +
     1.9 +#   command line argument sanity check
    1.10 +prg="$0"
    1.11 +if [ $# -eq 0 ]; then
    1.12 +    echo "$prg:ERROR: invalid command line" 1>&2
    1.13 +    echo "$prg:USAGE: $prg install [<database-directory>]" 1>&2
    1.14 +    echo "$prg:USAGE: $prg uninstall" 1>&2
    1.15 +    echo "$prg:USAGE: $prg backup [<dump-file>]" 1>&2
    1.16 +    echo "$prg:USAGE: $prg restore [<dump-file>|<dump-number>]" 1>&2
    1.17 +    echo "$prg:USAGE: $prg edit" 1>&2
    1.18 +    exit 1
    1.19 +fi
    1.20 +
    1.21 +#   database configuration
    1.22 +db_dir="@l_prefix@/var/drupal/db"
    1.23 +db_dump="@l_prefix@/var/drupal/dump"
    1.24 +db_type="@l_dbtype@"
    1.25 +db_name="drupal"
    1.26 +db_user="drupal"
    1.27 +db_pass="drupal"
    1.28 +
    1.29 +#   determine RDBMS-specific details
    1.30 +if [ ".$db_type" = .mysql ]; then
    1.31 +    db_sname="mysql"
    1.32 +    db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
    1.33 +              sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
    1.34 +    db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
    1.35 +              sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
    1.36 +elif [ ".$db_type" = .pgsql ]; then
    1.37 +    db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
    1.38 +              sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
    1.39 +    db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
    1.40 +              sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
    1.41 +    db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
    1.42 +              sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`
    1.43 +fi
    1.44 +
    1.45 +#   dispatch operation
    1.46 +cmd="${1:-"install"}"
    1.47 +shift
    1.48 +case "$cmd" in
    1.49 +    install )
    1.50 +        ##
    1.51 +        ##  create the database
    1.52 +        ##
    1.53 +
    1.54 +        if [ $# -gt 0 ]; then
    1.55 +            db_dir="$1"
    1.56 +            shift
    1.57 +        fi
    1.58 +        if [ ".$db_type" = .mysql ]; then
    1.59 +            #   FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces
    1.60 +            @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
    1.61 +            ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
    1.62 +              echo "FLUSH PRIVILEGES;"
    1.63 +            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
    1.64 +        elif [ ".$db_type" = .pgsql ]; then
    1.65 +            ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
    1.66 +              echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
    1.67 +              echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8' TEMPLATE template0;"
    1.68 +            ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
    1.69 +        fi
    1.70 +
    1.71 +        #   activate configuration directory
    1.72 +        #   (is automatically deactivated by installer afterwards)
    1.73 +        if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
    1.74 +            chmod 777 @l_prefix@/share/drupal/sites/default
    1.75 +        fi
    1.76 +        ;;
    1.77 +
    1.78 +    uninstall )
    1.79 +        ##
    1.80 +        ##  remove the database
    1.81 +        ##
    1.82 +
    1.83 +        if [ ".$db_type" = .mysql ]; then
    1.84 +            ( echo "DROP DATABASE $db_name;"
    1.85 +            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
    1.86 +        elif [ ".$db_type" = .pgsql ]; then
    1.87 +            ( echo "DROP DATABASE $db_name;"
    1.88 +              echo "DROP TABLESPACE $db_name;"
    1.89 +              echo "DROP ROLE $db_user;"
    1.90 +            ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
    1.91 +        fi
    1.92 +
    1.93 +        #   remove the generated configuration
    1.94 +        if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then
    1.95 +            rm -f @l_prefix@/share/drupal/sites/default/settings.php
    1.96 +        fi
    1.97 +        ;;
    1.98 +
    1.99 +    backup )
   1.100 +        ##
   1.101 +        ##  backup the database
   1.102 +        ##
   1.103 +
   1.104 +        #   determine dumpfile
   1.105 +        if [ $# -gt 0 ]; then
   1.106 +            #   manually managed dumpfile
   1.107 +            dumpfile="$1"
   1.108 +        else
   1.109 +            #   automatically managed (rotated) dumpfile
   1.110 +            rm -f $db_dump/drupal-dump-9.sql.gz >/dev/null 2>&1 || true
   1.111 +            i=8
   1.112 +            while [ $i -ge 0 ]; do
   1.113 +                if [ -f $db_dump/drupal-dump-$i.sql.bz2 ]; then
   1.114 +                    mv $db_dump/drupal-dump-$i.sql.bz2 $db_dump/drupal-dump-`expr $i + 1`.sql.bz2
   1.115 +                fi
   1.116 +                i=`expr $i - 1`
   1.117 +            done
   1.118 +            dumpfile="$db_dump/drupal-dump-0.sql.bz2"
   1.119 +        fi
   1.120 +
   1.121 +        #   dump database content to dumpfile (compressed plain SQL format)
   1.122 +        umask 007
   1.123 +        if [ ".$db_type" = .mysql ]; then
   1.124 +            @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \
   1.125 +            @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
   1.126 +        elif [ ".$db_type" = .pgsql ]; then
   1.127 +            PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \
   1.128 +            @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile
   1.129 +        fi
   1.130 +        chown @l_rusr@:@l_rgrp@ $dumpfile >/dev/null 2>&1 || true
   1.131 +        chmod 640 $dumpfile >/dev/null 2>&1 || true
   1.132 +        ;;
   1.133 +
   1.134 +    restore )
   1.135 +        ##
   1.136 +        ##  restore the database
   1.137 +        ##
   1.138 +
   1.139 +        #   determine dumpfile
   1.140 +        if [ $# -gt 0 ]; then
   1.141 +            dumpfile="$1"
   1.142 +        else
   1.143 +            dumpfile="0"
   1.144 +        fi
   1.145 +        case "$dumpfile" in
   1.146 +            [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;;
   1.147 +        esac
   1.148 +        if [ ! -f $dumpfile ]; then
   1.149 +            echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2
   1.150 +            exit 1
   1.151 +        fi
   1.152 +
   1.153 +        #   optionally stop Drupal
   1.154 +        eval `@l_prefix@/bin/openpkg rc drupal status 2>/dev/null || true`
   1.155 +        if [ ".$drupal_active" = .yes ]; then
   1.156 +            @l_prefix@/bin/openpkg rc drupal stop >/dev/null 2>&1 || true
   1.157 +        fi
   1.158 +
   1.159 +        #   drop old and initialize new database
   1.160 +        DRUPAL_SETUP_RESTORE=1
   1.161 +        export DRUPAL_SETUP_RESTORE
   1.162 +        $prg uninstall || exit $?
   1.163 +        $prg install   || exit $?
   1.164 +
   1.165 +        #   restore database content from dumpfile (compressed plain SQL format)
   1.166 +        if [ ".$db_type" = .mysql ]; then
   1.167 +            @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
   1.168 +            @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null
   1.169 +        elif [ ".$db_type" = .pgsql ]; then
   1.170 +            @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \
   1.171 +            PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null
   1.172 +        fi
   1.173 +
   1.174 +        #   optionally restart Drupal
   1.175 +        if [ ".$drupal_active" = .yes ]; then
   1.176 +            @l_prefix@/bin/openpkg rc drupal start >/dev/null 2>&1 || true
   1.177 +        fi
   1.178 +        ;;
   1.179 +
   1.180 +    edit )
   1.181 +        ##
   1.182 +        ##  interactively edit the database
   1.183 +        ##
   1.184 +
   1.185 +        if [ ".$db_type" = .mysql ]; then
   1.186 +            @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name"
   1.187 +        elif [ ".$db_type" = .pgsql ]; then
   1.188 +            PGPASSWORD="$db_spass" @l_prefix@/bin/psql -U "$db_suser" -d "$db_name"
   1.189 +        fi
   1.190 +        ;;
   1.191 +esac
   1.192 +

mercurial