3 # Configure postgresql database for GForge
6 # Simple function to know if a db exists
9 su -s /bin/sh postgres -c "psql $1 >/dev/null 2>&1 </dev/null"
12 # this function export in var pg_hba_dir the directory of postgresql
13 # exit from this script if it do not find the directory of postgresql
14 # TODO : try to use function getdist from novaforge
15 function get_pg_hba_dir() {
16 # postgresql on debian
17 if [ -d /etc/postgresql/${pg_version}/main/ ]
19 export pg_hba_dir=/etc/postgresql/${pg_version}/main/
22 # postgresql on redhat, fedora, centos
23 if [ -d /var/lib/pgsql/data/ ]
25 export pg_hba_dir=/var/lib/pgsql/data/
28 echo "No pg_hba_dir found"
29 echo "exiting without error, but gforge db will not work"
33 # this function export in var pg_version the version of postgresql
34 # exit from this script if it do not find the version of postgresql
35 # TODO : try to use function getdist from novaforge
36 function get_pg_version() {
37 # postgresql on debian
38 if [ -f /usr/bin/pg_lsclusters ]
40 pg_version=`/usr/bin/pg_lsclusters | grep 5432 | grep online | cut -d' ' -f1`
43 # postgresql on redhat, fedora, centos
44 if [ -f /var/lib/pgsql/data/PG_VERSION ]
46 pg_version=`cat /var/lib/pgsql/data/PG_VERSION`
49 echo "No database found online on port 5432"
50 echo "Couldn't initialize or upgrade gforge database."
51 echo "Please see postgresql documentation"
52 echo "and run dpkg-reconfigure -plow gforge-db-postgresql"
53 echo "once the problem is solved"
54 echo "exiting without error, but gforge db will not work"
62 if [ $(id -u) != 0 ] ; then
63 echo "You must be root to run this, please enter passwd"
76 # We are with new postgresql working with clusters
77 # This is probably not te most elegant way to deal with database
78 # I install or upgrade on the default cluster if it is online
79 # or I quit gently with a big message
85 echo "Usage: $0 {configure-files|configure|purge|purge-files|dump|restore}"
89 # Tell PostgreSQL to let us use the database
90 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
91 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
92 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
93 pattern=$(basename $0).XXXXXX
95 # PostgreSQL configuration for versions from 7.3 on
96 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
97 cur=$(mktemp /tmp/$pattern)
98 if ! grep -q 'BEGIN GFORGE BLOCK -- DO NOT EDIT' ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
99 # Make sure our configuration is inside a delimited BLOCK
100 if grep -q "^host.*gforge_passwd$" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
101 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^host.*gforge_passwd\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
102 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
103 elif grep -q "^### Next line inserted by GForge install" ${pg_hba_dir}/pg_hba.conf.gforge-new ; then
104 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^### Next line inserted by GForge install\nhost $db_name $db_user [0-9. ]+ password/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
105 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
107 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^host $db_name $db_user.*password\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\n### END GFORGE BLOCK -- DO NOT EDIT/s; print \$l;" > $cur
108 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
111 echo "### BEGIN GFORGE BLOCK -- DO NOT EDIT" > $cur
112 echo "### END GFORGE BLOCK -- DO NOT EDIT" >> $cur
113 cat ${pg_hba_dir}/pg_hba.conf.gforge-new >> $cur
114 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
117 cur=$(mktemp /tmp/$pattern)
118 perl -e "open F, \"${pg_hba_dir}/pg_hba.conf.gforge-new\" or die \$!; undef \$/; \$l=<F>; \$l=~ s/^### BEGIN GFORGE BLOCK -- DO NOT EDIT.*### END GFORGE BLOCK -- DO NOT EDIT\$/### BEGIN GFORGE BLOCK -- DO NOT EDIT\nlocal $db_name $db_user md5\nlocal $db_name gforge_nss trust\nlocal $db_name gforge_mta md5\n### END GFORGE BLOCK -- DO NOT EDIT/ms; print \$l;" > $cur
119 cat $cur > ${pg_hba_dir}/pg_hba.conf.gforge-new
124 # Create the appropriate database user
125 db_passwd=$(grep ^db_password= /etc/gforge/gforge.conf | cut -d= -f2-)
126 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
127 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
128 pattern=$(basename $0).XXXXXX
129 tmp1=$(mktemp /tmp/$pattern)
130 tmp2=$(mktemp /tmp/$pattern)
131 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
132 SET LC_MESSAGES = 'C' ;
133 CREATE USER $db_user WITH PASSWORD '$db_passwd' ;
138 echo "Cannot create PostgreSQL user... This shouldn't have happened."
139 echo "Maybe a problem in your PostgreSQL configuration?"
140 echo "Please report a bug to the Debian bug tracking system"
141 echo "Please include the following output:"
142 echo "CREATE USER's STDOUT:"
144 echo "CREATE USER's STDERR:"
149 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
150 SET LC_MESSAGES = 'C' ;
151 CREATE USER gforge_nss WITH PASSWORD 'gforge_nss' ;
156 echo "Cannot create PostgreSQL user... This shouldn't have happened."
157 echo "Maybe a problem in your PostgreSQL configuration?"
158 echo "Please report a bug to the Debian bug tracking system"
159 echo "Please include the following output:"
160 echo "CREATE USER's STDOUT:"
162 echo "CREATE USER's STDERR:"
167 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<-EOF
168 SET LC_MESSAGES = 'C' ;
169 CREATE USER gforge_mta WITH PASSWORD 'gforge_mta' ;
174 echo "Cannot create PostgreSQL user... This shouldn't have happened."
175 echo "Maybe a problem in your PostgreSQL configuration?"
176 echo "Please report a bug to the Debian bug tracking system"
177 echo "Please include the following output:"
178 echo "CREATE USER's STDOUT:"
180 echo "CREATE USER's STDERR:"
186 # Create the appropriate database
187 tmp1=$(mktemp /tmp/$pattern)
188 tmp2=$(mktemp /tmp/$pattern)
189 if ! exist_db $db_name ; then
190 if su -s /bin/sh postgres -c "/usr/bin/psql template1" 1> $tmp1 2> $tmp2 <<EOF \
191 && [ "$(tail -n +2 $tmp1 | head -1)" = 'CREATE DATABASE' ] ;
192 SET LC_MESSAGES = 'C' ;
193 CREATE DATABASE $db_name WITH ENCODING 'UNICODE' ;
200 echo "Cannot create PostgreSQL database... This shouldn't have happened."
201 echo "Maybe a problem in your PostgreSQL configuration?"
202 echo "Please report a bug to the Debian bug tracking system"
203 echo "Please include the following output:"
204 echo "CREATE DATABASE's STDOUT:"
206 echo "CREATE DATABASE's STDERR:"
213 # Enable plpgsql language
214 if [ -f /usr/bin/createlang ]
216 if [ `su -s /bin/sh postgres -c "/usr/bin/createlang -l $db_name | grep plpgsql | wc -l"` != 1 ]
218 su -s /bin/sh postgres -c "/usr/bin/createlang plpgsql $db_name"
220 echo "Procedural language on $db_name already enabled"
223 echo "No way found to enable plpgsql on $db_name here"
226 # Install/upgrade the database contents (tables and data)
227 su -s /bin/sh gforge -c /usr/share/gforge/bin/db-upgrade.pl 2>&1 | grep -v ^NOTICE:
229 if [ $p != 0 ] ; then
234 cp -a ${pg_hba_dir}/pg_hba.conf ${pg_hba_dir}/pg_hba.conf.gforge-new
235 perl -pi -e "BEGIN { undef \$/; } s/^### BEGIN GFORGE BLOCK -- DO NOT EDIT.*### END GFORGE BLOCK -- DO NOT EDIT\n//ms;" ${pg_hba_dir}/pg_hba.conf.gforge-new
238 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
239 db_user=$(grep ^db_user= /etc/gforge/gforge.conf | cut -d= -f2-)
240 su -s /bin/sh postgres -c "dropdb $db_name" > /dev/null 2>&1 || true
241 su -s /bin/sh postgres -c "dropuser $db_user" > /dev/null 2>&1 || true
244 # only only for migrating users of the sourceforge package
247 if [ -e /etc/sourceforge/local.pl ] ; then
248 db_name=$(perl -e'require "/etc/sourceforge/local.pl"; print "$sys_dbname\n";')
249 elif [ -e /etc/gforge/gforge.conf ] ; then
250 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
254 if [ "x$2" != "x" ] ;then
257 DUMPFILE=/var/lib/gforge/dumps/db_dump
259 if [ "x$3" != "x" ] ;then
264 echo "Dumping $DB database in $DUMPFILE"
265 su -s /bin/sh $DB -c /usr/lib/postgresql/bin/pg_dump $DB > $DUMPFILE
268 # only only for migrating users of the sourceforge package
271 pg_name=postgresql-$pg_version
272 db_name=$(grep ^db_name= /etc/gforge/gforge.conf | cut -d= -f2-)
273 pattern=$(basename $0).XXXXXX
274 newpg=$(mktemp /tmp/$pattern)
275 localtrust="local all all trust"
276 echo "### Next line inserted by GForge restore" > $newpg
277 echo "$localtrust" >> $newpg
278 #echo "host all 127.0.0.1 255.255.255.255 trust" >> $newpg
279 cat ${pg_hba_dir}/pg_hba.conf >> $newpg
280 mv $newpg ${pg_hba_dir}/pg_hba.conf
281 chmod 644 ${pg_hba_dir}/pg_hba.conf
282 invoke-rc.d ${pg_name} restart
283 if [ "x$2" != "x" ] ;then
286 RESTFILE=/var/lib/gforge/dumps/db_dump
288 echo "Restoring $RESTFILE"
289 su -s /bin/sh postgres -c "dropdb $db_name" || true
290 su -s /bin/sh postgres -c "createdb --encoding=UNICODE $db_name" || true
291 su -s /bin/sh postgres -c "/usr/bin/psql -f $RESTFILE $db_name"
292 perl -pi -e "s/### Next line inserted by GForge restore\n//" ${pg_hba_dir}/pg_hba.conf
293 perl -pi -e "s/$localtrust\n//" ${pg_hba_dir}/pg_hba.conf
294 invoke-rc.d ${pg_name} reload