michael@529: #!@l_bash@ michael@529: ## michael@529: ## drupal-setup.sh -- Drupal RDBMS Setup Utility michael@529: ## michael@529: michael@529: # command line argument sanity check michael@529: prg="$0" michael@529: if [ $# -eq 0 ]; then michael@529: echo "$prg:ERROR: invalid command line" 1>&2 michael@529: echo "$prg:USAGE: $prg install []" 1>&2 michael@529: echo "$prg:USAGE: $prg uninstall" 1>&2 michael@529: echo "$prg:USAGE: $prg backup []" 1>&2 michael@529: echo "$prg:USAGE: $prg restore [|]" 1>&2 michael@529: echo "$prg:USAGE: $prg edit" 1>&2 michael@529: exit 1 michael@529: fi michael@529: michael@529: # database configuration michael@529: db_dir="@l_prefix@/var/drupal/db" michael@529: db_dump="@l_prefix@/var/drupal/dump" michael@529: db_type="@l_dbtype@" michael@529: db_name="drupal" michael@529: db_user="drupal" michael@529: db_pass="drupal" michael@529: michael@529: # determine RDBMS-specific details michael@529: if [ ".$db_type" = .mysql ]; then michael@529: db_sname="mysql" michael@529: db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\ michael@529: sed -e 's;^user[^=]*= *;;' -e 's; *$;;'` michael@529: db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\ michael@529: sed -e 's;^password[^=]*= *;;' -e 's; *$;;'` michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: db_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\ michael@529: sed -e 's;^ *superuser_database="\(.*\)".*;\1;'` michael@529: db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\ michael@529: sed -e 's;^ *superuser_username="\(.*\)".*;\1;'` michael@529: db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\ michael@529: sed -e 's;^ *superuser_password="\(.*\)".*;\1;'` michael@529: fi michael@529: michael@529: # dispatch operation michael@529: cmd="${1:-"install"}" michael@529: shift michael@529: case "$cmd" in michael@529: install ) michael@529: ## michael@529: ## create the database michael@529: ## michael@529: michael@529: if [ $# -gt 0 ]; then michael@529: db_dir="$1" michael@529: shift michael@529: fi michael@529: if [ ".$db_type" = .mysql ]; then michael@529: # FIXME: MySQL 5.0 still doesn't allow easy relocation of tablespaces michael@529: @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name" michael@529: ( echo "GRANT ALL PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';" michael@529: echo "FLUSH PRIVILEGES;" michael@529: ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname" michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;" michael@529: echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';" michael@529: echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name ENCODING 'UTF8' TEMPLATE template0;" michael@529: ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f- michael@529: fi michael@529: michael@529: # activate configuration directory michael@529: # (is automatically deactivated by installer afterwards) michael@529: if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then michael@529: chmod 777 @l_prefix@/share/drupal/sites/default michael@529: fi michael@529: ;; michael@529: michael@529: uninstall ) michael@529: ## michael@529: ## remove the database michael@529: ## michael@529: michael@529: if [ ".$db_type" = .mysql ]; then michael@529: ( echo "DROP DATABASE $db_name;" michael@529: ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname" michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: ( echo "DROP DATABASE $db_name;" michael@529: echo "DROP TABLESPACE $db_name;" michael@529: echo "DROP ROLE $db_user;" michael@529: ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f- michael@529: fi michael@529: michael@529: # remove the generated configuration michael@529: if [ ".$DRUPAL_SETUP_RESTORE" = . ]; then michael@529: rm -f @l_prefix@/share/drupal/sites/default/settings.php michael@529: fi michael@529: ;; michael@529: michael@529: backup ) michael@529: ## michael@529: ## backup the database michael@529: ## michael@529: michael@529: # determine dumpfile michael@529: if [ $# -gt 0 ]; then michael@529: # manually managed dumpfile michael@529: dumpfile="$1" michael@529: else michael@529: # automatically managed (rotated) dumpfile michael@529: rm -f $db_dump/drupal-dump-9.sql.gz >/dev/null 2>&1 || true michael@529: i=8 michael@529: while [ $i -ge 0 ]; do michael@529: if [ -f $db_dump/drupal-dump-$i.sql.bz2 ]; then michael@529: mv $db_dump/drupal-dump-$i.sql.bz2 $db_dump/drupal-dump-`expr $i + 1`.sql.bz2 michael@529: fi michael@529: i=`expr $i - 1` michael@529: done michael@529: dumpfile="$db_dump/drupal-dump-0.sql.bz2" michael@529: fi michael@529: michael@529: # dump database content to dumpfile (compressed plain SQL format) michael@529: umask 007 michael@529: if [ ".$db_type" = .mysql ]; then michael@529: @l_prefix@/bin/mysqldump --user="$db_suser" --password="$db_spass" "$db_name" | \ michael@529: @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: PGPASSWORD="$db_spass" @l_prefix@/bin/pg_dump -U "$db_suser" -S "$db_suser" "$db_name" | \ michael@529: @l_prefix@/lib/openpkg/bzip2 -9 >$dumpfile michael@529: fi michael@529: chown @l_rusr@:@l_rgrp@ $dumpfile >/dev/null 2>&1 || true michael@529: chmod 640 $dumpfile >/dev/null 2>&1 || true michael@529: ;; michael@529: michael@529: restore ) michael@529: ## michael@529: ## restore the database michael@529: ## michael@529: michael@529: # determine dumpfile michael@529: if [ $# -gt 0 ]; then michael@529: dumpfile="$1" michael@529: else michael@529: dumpfile="0" michael@529: fi michael@529: case "$dumpfile" in michael@529: [0-9] ) dumpfile="$db_dump/drupal-dump-$dumpfile.sql.bz2" ;; michael@529: esac michael@529: if [ ! -f $dumpfile ]; then michael@529: echo "drupal-setup:restore:ERROR: no such dump file: $dumpfile" 1>&2 michael@529: exit 1 michael@529: fi michael@529: michael@529: # optionally stop Drupal michael@529: eval `@l_prefix@/bin/openpkg rc drupal status 2>/dev/null || true` michael@529: if [ ".$drupal_active" = .yes ]; then michael@529: @l_prefix@/bin/openpkg rc drupal stop >/dev/null 2>&1 || true michael@529: fi michael@529: michael@529: # drop old and initialize new database michael@529: DRUPAL_SETUP_RESTORE=1 michael@529: export DRUPAL_SETUP_RESTORE michael@529: $prg uninstall || exit $? michael@529: $prg install || exit $? michael@529: michael@529: # restore database content from dumpfile (compressed plain SQL format) michael@529: if [ ".$db_type" = .mysql ]; then michael@529: @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \ michael@529: @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" >/dev/null michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: @l_prefix@/lib/openpkg/bzip2 -d -c $dumpfile | \ michael@529: PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_name" -f- >/dev/null michael@529: fi michael@529: michael@529: # optionally restart Drupal michael@529: if [ ".$drupal_active" = .yes ]; then michael@529: @l_prefix@/bin/openpkg rc drupal start >/dev/null 2>&1 || true michael@529: fi michael@529: ;; michael@529: michael@529: edit ) michael@529: ## michael@529: ## interactively edit the database michael@529: ## michael@529: michael@529: if [ ".$db_type" = .mysql ]; then michael@529: @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" michael@529: elif [ ".$db_type" = .pgsql ]; then michael@529: PGPASSWORD="$db_spass" @l_prefix@/bin/psql -U "$db_suser" -d "$db_name" michael@529: fi michael@529: ;; michael@529: esac michael@529: