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.5 2007/01/30 11:29:36 thierry 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 subversion column in plc_db_version by applying
24 ### all migrations scripts in /usr/share/plc_api/migrations/nnn-up-*
25 ### between the current_subversion+1 and the last one
26 ### migration indices must be contiguous
27 function migrate_db () {
29 ### check for (or create) the subversion column in plc_db_version
30 psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME > /dev/null
31 [ "$?" = 0 ] || psql -U $PLC_DB_USER -c 'ALTER TABLE plc_db_version ADD subversion integer DEFAULT 0' $PLC_DB_NAME
33 # get current subversion - OUCH, THIS IS UGLY
34 current_subversion=$(psql -U $PLC_DB_USER -c 'select (subversion) from plc_db_version' $PLC_DB_NAME | \
35 grep '^ *[0-9][0-9]*$')
36 current_subversion=$(($current_subversion))
37 upgrade_index=$current_subversion
40 upgrade_index=$(printf "%03d" $(($upgrade_index+1)))
41 upgrade_scripts=$(ls -1 /usr/share/plc_api/migrations/${upgrade_index}-up-* 2> /dev/null)
42 if [ -z "$upgrade_scripts" ] ; then
43 echo "DB current migration is $current_subversion"
44 psql -U $PLC_DB_USER -c "UPDATE plc_db_version SET subversion=$current_subversion" $PLC_DB_NAME
47 for script in $upgrade_scripts; do
48 # is this an sql script
49 if [ $(basename "$script") != $(basename $script .sql) ] ; then
50 echo "Applying SQL migration script $script"
51 psql -U $PLC_DB_USER -f "$script" $PLC_DB_NAME
52 elif [ -x "$script" ] ; then
53 echo "Running executable migration script $script"
56 echo "WARNING - script $script ignored"
59 current_subversion=$(($upgrade_index))
64 #### dumps the database for future restoration
65 # uses /root/dumps since /root is mounted on /data/root
68 dump=/root/dumps/$(date +'%Y-%m-%d-%H-%M.sql')
69 mkdir -p $(dirname $dump)
70 pg_dump --user=$PLC_DB_USER $PLC_DB_NAME > $dump
74 function clean_dumps () {
75 find /root/dumps -name '[0-9]*.sql' -atime +15 | xargs rm
79 if [ "$PLC_DB_ENABLED" != "1" ] ; then
86 MESSAGE=$"Bootstrapping the database"
89 # Update the maintenance account username. This can't be
90 # done through the api-config script since it uses the
91 # maintenance account to access the API. The maintenance
92 # account should be person_id 1 since it is created by the
94 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
96 # Update the Drupal site_name variable
97 psql -U $PLC_DB_USER drupal <<EOF
98 DELETE FROM variable WHERE name = 'site_name';
99 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
114 MESSAGE=$"Migrating the database"
123 MESSAGE=$"Dumping the database"
132 MESSAGE=$"Cleaning old database dumps"
140 echo "Usage: $0 [start|migrate|dump|clean-dump]"