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