7 # Bootstrap the database
9 # Mark Huang <mlhuang@cs.princeton.edu>
10 # Copyright (C) 2006 The Trustees of Princeton University
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
27 for file in /usr/share/plc_api/extensions/*-up*; do
30 extension=${script##*.}
31 version=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
32 "SELECT version FROM plc_db_extensions WHERE name='$name' LIMIT 1" \
33 $PLC_DB_NAME 2>/dev/null | awk 'BEGIN { ver=0 } /^[0-9]+$/ { ver=$1 } END { print ver }')
34 if [ $version -eq 0 ]; then
35 if [ "$extension" = "sql" ] ; then
36 dialog " - $script (dbdumped)"
37 dump_planetlab_db "before-$script"
38 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
39 elif [ -x $file ] ; then
40 dialog " - $script (dbdumped)"
41 dump_planetlab_db "before-$script"
44 dialog "\nWarning: extension $file not executable"
48 for file in /usr/share/plc_api/extensions/$name/migrations/[0-9]*-up-*; do
51 extension=${script##*.}
52 if [ $index -gt $version ] ; then
53 if [ "$extension" = "sql" ] ; then
54 dialog " - $script (dbdumped)"
55 dump_planetlab_db "before-$script"
56 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
57 elif [ -x $file ] ; then
58 dialog " - $script (dbdumped)"
59 dump_planetlab_db "before-$script"
62 dialog "\nWarning: migration $file not executable"
70 # Updates the database by applying all migration scripts in
71 # /usr/share/plc_api/migrations/N-up-*, where N is greater than the
72 # current subversion. At least one of the migration scripts with the
73 # same N must update plc_db_version.subversion.
76 subversion=$(psql -U $PLC_DB_USER --quiet --tuples-only --no-align -c \
77 "SELECT subversion FROM plc_db_version LIMIT 1" \
78 $PLC_DB_NAME 2>/dev/null || echo 0)
80 for file in /usr/share/plc_api/migrations/[0-9]*-up-* ; do
81 script=$(basename $file)
83 extension=${script##*.}
84 if [ $index -gt $subversion ] ; then
85 if [ "$extension" = "sql" ] ; then
86 dialog " - $script (dbdumped)"
87 dump_planetlab_db "before-$script"
88 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
89 elif [ -x $file ] ; then
90 dialog " - $script (dbdumped)"
91 dump_planetlab_db "before-$script"
94 dialog "\nWarning: migration $file not executable"
101 function checkpoint_planetlab_db()
104 pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
108 function restore_planetlab_db()
111 if [ -n "$dumpfile" ] ; then
112 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
117 # use a single date of this script invocation for the dump_*_db functions.
118 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
120 # Dumps the database - optional argument to specify filename suffix
121 function dump_planetlab_db()
123 if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
124 dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
125 checkpoint_planetlab_db $dumpfile
128 function restore_drupal_db()
131 if [ -n "$dumpfile" ] ; then
132 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
137 function checkpoint_drupal_db()
140 pg_dump -U $PLC_DB_USER drupal > $dumpfile
144 function dump_drupal_db()
146 dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
147 checkpoint_drupal_db $dumpfile
151 # Clean up old backups
152 function clean_dumps()
154 find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
158 [ $PLC_DB_ENABLED -ne 1 ] && exit 0
161 MESSAGE=$"Bootstrapping the database"
164 # Apply schema updates
168 # Update the maintenance account username. This can't be
169 # done through the api-config script since it uses the
170 # maintenance account to access the API. The maintenance
171 # account should be person_id 1 since it is created by the
173 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
175 # Update the Drupal site_name variable
176 # also turn off drupal native user registration
177 psql -U $PLC_DB_USER drupal <<EOF
178 DELETE FROM variable WHERE name = 'site_name';
179 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
180 DELETE FROM variable WHERE name = 'user_register';
181 INSERT INTO variable (name, value) VALUES ('user_register', 's:1:"0";');
193 MESSAGE=$"Migrating the database"
201 MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
210 MESSAGE=$"Checkpointing the databases"
211 checkpoint_planetlab_db $2
212 checkpoint_drupal_db $3
216 MESSAGE=$"Restoring the databases from checkpoint files"
217 restore_planetlab_db $2
222 MESSAGE=$"Cleaning old database dumps"
230 MESSAGE="Ignoring request to stop myplc databases"
236 echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"