5 # Bootstrap the database
7 # Mark Huang <mlhuang@cs.princeton.edu>
8 # 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
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 dialog " - $script (dbdumped)"
40 dump_planetlab_db "before-$script"
41 psql -U $PLC_DB_USER -f $file $PLC_DB_NAME
42 elif [ -x $file ] ; then
43 dialog " - $script (dbdumped)"
44 dump_planetlab_db "before-$script"
47 dialog "\nWarning: migration $file not executable"
54 function checkpoint_planetlab_db()
57 pg_dump -U $PLC_DB_USER $PLC_DB_NAME > $dumpfile
61 function restore_planetlab_db()
64 if [ -n "$dumpfile" ] ; then
65 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER $PLC_DB_NAME < $dumpfile
70 # use a single date of this script invocation for the dump_*_db functions.
71 DATE=$(date +"%Y-%m-%d-%H-%M-%S")
73 # Dumps the database - optional argument to specify filename suffix
74 function dump_planetlab_db()
76 if [ -n "$1" ] ; then suffix="-$1" ; else suffix="" ; fi
77 dumpfile=/var/lib/pgsql/backups/$(date +"${PLC_DB_NAME}.${DATE}${suffix}.sql")
78 checkpoint_planetlab_db $dumpfile
81 function restore_drupal_db()
84 if [ -n "$dumpfile" ] ; then
85 [ -f "$dumpfile" ] && psql -a -U $PLC_DB_USER drupal < $1
90 function checkpoint_drupal_db()
93 pg_dump -U $PLC_DB_USER drupal > $dumpfile
97 function dump_drupal_db()
99 dumpfile=/var/lib/pgsql/backups/$(date +"drupal.${DATE}.sql")
100 checkpoint_drupal_db $dumpfile
104 # Clean up old backups
105 function clean_dumps()
107 find /var/lib/pgsql/backups '(' -name "$PLC_DB_NAME.*.sql" -o -name "drupal.*.sql" ')' -a -atime +15 | xargs rm -f
111 [ $PLC_DB_ENABLED -ne 1 ] && exit 0
114 MESSAGE=$"Bootstrapping the database"
117 # Apply schema updates
120 # Update the maintenance account username. This can't be
121 # done through the api-config script since it uses the
122 # maintenance account to access the API. The maintenance
123 # account should be person_id 1 since it is created by the
125 psql -U $PLC_DB_USER -c "UPDATE persons SET email='$PLC_API_MAINTENANCE_USER' WHERE person_id=1" $PLC_DB_NAME
127 # Update the Drupal site_name variable
128 psql -U $PLC_DB_USER drupal <<EOF
129 DELETE FROM variable WHERE name = 'site_name';
130 INSERT INTO variable (name, value) VALUES ('site_name', 's:${#PLC_NAME}:"$PLC_NAME";');
141 MESSAGE=$"Migrating the database"
149 MESSAGE=$"Dumping the databases in /var/lib/pgsql/backups"
158 MESSAGE=$"Checkpointing the databases"
159 checkpoint_planetlab_db $2
160 checkpoint_drupal_db $3
164 MESSAGE=$"Restoring the databases from checkpoint files"
165 restore_planetlab_db $2
170 MESSAGE=$"Cleaning old database dumps"
178 MESSAGE="Ignoring request to stop myplc databases"
184 echo "Usage: $0 [start|migrate|dump|checkpoint|restore|clean-dump|stop]"