5 # Bootstrap the database
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # Copyright (C) 2006 The Trustees of Princeton University
10 # $Id: db,v 1.7 2007/01/31 19:53:20 mlhuang Exp $
13 # Source function library and configuration
14 . /etc/plc.d/functions
15 . /etc/planetlab/plc_config
20 # Export so that we do not have to specify -p to psql invocations
21 export PGPORT=$PLC_DB_PORT
23 # Updates the database by applying all migration scripts in
24 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
25 # current subversion. At least one of the migration scripts with the
26 # same N must update plc_db_version.subversion.
29 subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
30 "SELECT subversion FROM plc_db_version LIMIT 1" \
31 $PLC_DB_NAME 2>/dev/null || echo 0)
33 for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
34 script=$(basename $file)
36 extension=${script##*.}
37 if [ $index -gt $subversion ] ; then
38 if [ "$extension" = "sql" ] ; then
39 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
40 elif [ -x $file ] ; then
51 dump=/var/lib/pgsql/backups/$(date +"$PLC_DB_NAME.%Y-%m-%d-%H-%M.sql")
52 pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dump
54 dump=/var/lib/pgsql/backups/$(date +"drupal.%Y-%m-%d-%H-%M.sql")
55 pg_dump -U $PLC_DB_USER drupal > $dump
59 # Clean up old backups
60 function clean_dumps()
62 find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
66 if [ "$PLC_DB_ENABLED" != "1" ] ; then
72 MESSAGE=$"Bootstrapping the database"
75 # Apply schema updates
78 # Update the maintenance account username. This can't be
79 # done through the api-config script since it uses the
80 # maintenance account to access the API. The maintenance
81 # account should be person_id 1 since it is created by the
83 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
85 # Update the Drupal site_name variable
86 psql -U $PLC_DB_USER drupal <<EOF
87 DELETE FROM variable WHERE name = 'site_name';
88 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
99 MESSAGE=$"Migrating the database"
107 MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
115 MESSAGE=$"Cleaning old database dumps"
123 echo "Usage: $0 [start|migrate|dump|clean-dump]"